Spring中的注入@Autowired,@Resource,@Qualifier

帖子中主要是讲了Spring中的注入方式,总结如下:

A:未使用注解我们是怎样注入属性的

类的实现 写道
public class UserManagerImpl implements UserManager {

privateUserDaouserDao;

publicvoidsetUserDao(UserDaouserDao){

this.userDao=userDao;

}

...

}

XML配置文件的实现 写道
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">

<propertyname="userDao"ref="userDao"/>

</bean>

<beanid="userDao"class="com.kedacom.spring.annotation.persistence.UserDaoImpl">

<propertyname="sessionFactory"ref="mySessionFactory"/>

</bean>

B: 引入@Autowired注解(不推荐使用,建议使用@Resource)

类的实现(对成员变量进行标注) 写道
public class UserManagerImpl implements UserManager {

@Autowired

privateUserDaouserDao;

...

}

(对方法进行标注) 写道
public class UserManagerImpl implements UserManager {

privateUserDaouserDao;

@Autowired

publicvoidsetUserDao(UserDaouserDao){

this.userDao=userDao;

}

...

}

XML配置 写道
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl" />

<beanid="userDao"class="com.kedacom.spring.annotation.persistence.UserDaoImpl">

<propertyname="sessionFactory"ref="mySessionFactory"/>

</bean>

相关推荐