a) 默认按类型by type
b) 如果想用byName,使用@Qulifier
c) 写在private field(第三种注入形式)(不建议,破坏封装)
d) 如果写在set上,@qualifier需要写在参数上
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="u1" class="com.bjsxt.dao.impl.UserDAOImpl"/>
<bean id="u2" class="com.bjsxt.dao.impl.UserDAOImpl"/>
<bean id="userService" class="com.bjsxt.service.UserService" />
</beans>
@Autowired
public void setUserDAO(@Qualifier("u1") UserDAO userDAO) {
this.userDAO = userDAO;
}
<!--EndFragment-->
分享到:
相关推荐
通过@Bean 和spring的factoryBean注入的bean. 以及对应@service注解注入的类 通过@Autowired 注入对象的时间是现根据类型在根据beanName获取的案例集合
Spring@Autowired注解与自动装配.rar
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235292
Spring Ioc源码分析系列--@Autowired注解的实现原理.doc
主要介绍了使用@Autowired注解警告Field injection is not recommended的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Spring框架中 @Autowired 和 @Resource 注解的区别 在 spring 框架中,除了使用其特有的注解外,使用基于 JSR-250 的注解,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释。 首先,咱们简单了解 @...
下面小编就为大家带来一篇基于Spring@Autowired注解与自动装配详谈。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring注解@Resource和@Autowired.doc
@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。如果我们想使用按照名称(byName)来装配,可以结合@Qualifier注解一起...
Spring boot @Aspect简介使用场景列子1 (演示基本过程切点表达式切点复合运算切点匹配方法通知注解切面类执行顺序 简介 在 Spring实现 AOP面向切面编程, 是通过 @Aspect注解来实现切面的 使用场景 常见用于记录日志,...
NULL 博文链接:https://ysj5125094.iteye.com/blog/2054257
主要介绍了Spring注解@Resource和@Autowired区别对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @...
3)在CustomerService实现类中添加成员变量,并添加@AutoWired注解让spring自动完成该service的注入。 @Autowired public IAccountService accountService; 4)代码中调用 accountService.getAccount(name)
本篇文章主要介绍最重要的三个Spring注解,也就是@Autowired、@Resource和@Service,具有很好的参考价值。下面跟着小编一起来看下吧
在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@Repository、@Service、@Controller和@Autowired。这些注解简化了配置过程,减少了样板代码,并使得组件之间的耦合度降低,更有利于单元...
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
背景注解可以减少代码的开发量,Spring 提供了丰富的注解功能。我们可能会被问到,Spring 的注解到底是怎么触发的呢?今天以 Spring 最常使用的一个