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>
相关推荐
杜鲁门 2020-11-05
与卿画眉共浮生 2020-10-14
lukezhong 2020-10-14
tangxiong0 2020-09-03
YangHuiLiang 2020-08-06
Sweetdream 2020-08-03
编程点滴 2020-07-29
smalllove 2020-07-27
iconhot 2020-07-05
XGQ 2020-07-04
MicroBoy 2020-07-04
itjavashuai 2020-07-04
zmysna 2020-07-04
willluckysmile 2020-06-29
CoderBoy 2020-06-28
爱莲说 2020-06-26
itjavashuai 2020-06-25
HappyHeng 2020-06-21
smalllove 2020-06-14