首先查看 @Autowired为何
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})
public @interface Autowired {
/**
* Declares whether the annotated dependency is required.
* <p>Defaults to <code>true</code>.
*/
boolean required() default true;
}
通过源码我们知道他是spring的一个注解接口,有一个方法
boolean required() default true;
使用的时候必须满足如下条件:
1.spring的配置文件必须加入能够识别注解的东东
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
或者使用xml标注如下(注意版本)
xmlns:context
<context:component-scan base-package="org.javaeye.*"/>
支持4种注解分别为@Component, @Serivce, @Controller, @Repository
@Controller:控制层
@Serivce:业务逻辑层
@Repository:持久层
2.有注解存在
@Autowired
UserService userService;
3.有对应的setter方法
public void setUserService(UserService userService) {
this.userService = userService;
}
4.如果是接口或者抽象类的话那么需要实现类唯一,否则创建实例出错
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type
[com.sohu.suc.splatform.service.UserService] is defined:
expected single matching bean but found 2:
[userServiceHibernateImpl, userServiceImp]
5.接口的实现必须让spring认识,以bean的方式配置或者加注解让spring认识
@Service
public class UserServiceImpl implements UserService {
。。。。。。。
}
综上可得spring只管理他认识的bean,有2中方式让spring知道bean的存在
1.注解方式
2.bean配置
分享到:
相关推荐
通过@Bean 和spring的factoryBean注入的bean. 以及对应@service注解注入的类 通过@Autowired 注入对象的时间是现根据类型在根据beanName获取的案例集合
Spring@Autowired注解与自动装配.rar
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235292
Spring Ioc源码分析系列--@Autowired注解的实现原理.doc
下面小编就为大家带来一篇基于Spring@Autowired注解与自动装配详谈。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring注解@Resource和@Autowired.doc
Spring框架中 @Autowired 和 @Resource 注解的区别 在 spring 框架中,除了使用其特有的注解外,使用基于 JSR-250 的注解,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释。 首先,咱们简单了解 @...
@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。如果我们想使用按照名称(byName)来装配,可以结合@Qualifier注解一起...
本篇文章主要介绍最重要的三个Spring注解,也就是@Autowired、@Resource和@Service,具有很好的参考价值。下面跟着小编一起来看下吧
Spring boot @Aspect简介使用场景列子1 (演示基本过程切点表达式切点复合运算切点匹配方法通知注解切面类执行顺序 简介 在 Spring实现 AOP面向切面编程, 是通过 @Aspect注解来实现切面的 使用场景 常见用于记录日志,...
主要介绍了Spring注解@Resource和@Autowired区别对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @...
NULL 博文链接:https://ysj5125094.iteye.com/blog/2054257
autowired spring autowired注解完整版 使用Spring2.5的新特性——Autowired可以实现快速的自动注入,而无需在xml文档里面添加bean的声明,大大减少了xml文档的维护。(偶喜欢这个功能,因为偶对xml不感冒)。
在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@Repository、@Service、@Controller和@Autowired。这些注解简化了配置过程,减少了样板代码,并使得组件之间的耦合度降低,更有利于单元...
背景注解可以减少代码的开发量,Spring 提供了丰富的注解功能。我们可能会被问到,Spring 的注解到底是怎么触发的呢?今天以 Spring 最常使用的一个
3)在CustomerService实现类中添加成员变量,并添加@AutoWired注解让spring自动完成该service的注入。 @Autowired public IAccountService accountService; 4)代码中调用 accountService.getAccount(name)