spring之注解

1.@Autowired

可以对成员变量、方法和构造函数进行自动配置(根据类型进行自动装配)

public class UserImpl implements User {    
    @Autowired    
    private User user;//自动配置user
... } 

等同于XML中的配置:

<property name="user" ref="user" />  

在XML可以只配置bean,不需要对property的配置

<bean id="userImpl" class="com.user.userImpl" />
<!--<property name="user" ref="user" />-->

2.@Qualifier

在上面的例子中,如果当Spring上下文中存在不止一个User类型的bean或者不存在User类型的bean时,就会抛出BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题。
@Autowired    
public void setUserDao(@Qualifier("userDao") UserDao userDao) {    
    this.userDao = userDao;    
}  

这样,Spring会找到id为userDao的bean进行装配。 

3.@Component、@Repository、@Service、@Controller 

可以对进行自动配置

@Component("userDao")     
public class UserImpl  implements User {    
    ...    
}   

等同于XML中的配置:

<bean id="userImpl" class="com.user.userImpl" />

但是尽量不要使用@Component而要使用@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean

4.@Scope

 定义Bean的作用范围 

@Component
@Scope("prototype")
public class UserImpl  implements User {    
    ...    
}   

 等同于XML中的

<bean  id="userImpl" class="com.user.userImpl" scope="singleton"/>



原文地址:https://www.cnblogs.com/corolcorona/p/6703432.html