Spring基础18——通过注解配置bean之间的关联关系
1.组件装配
<context:component-scan>元素还会自动注册AutowiredAnnotaionBeanPostProcessor实例,这是一个bean的后置处理器,该实例可以自动装配具有@Autuwired和@Resource、@Inject注解的属性(前提是在容器当中要自动装配的bean已经存在),其中@Autowired是最常用的。
2.@Autowired注解
@Autowired注解自动装配具有兼容类型的单个Bean属性,构造器,普通字段(即使是非public),一切具有参数的方法都可以应用@Autowired注解放在构造器上也是没有问题的。
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@AutowiredIOC使用时应该注意以下几点:
1)使用@Autowired,Spring如果如果有这个bean直接拿,如果没有则会报错,注意@Autowired是按照类型注入的。
2)默认情况下,所有使用autowired注解的属性都需要被设置,当spring找不到配置的bean装配属性时,会抛出异常,某一属性允许不被设置,可以设置
3)@Autowired注解的required属性为false
4)默认情况下,当IOC容器里存在多个类型兼容的bean(同一类型或子类)时,通过类型自动装配将无法工作,此时可以在@Qualifiter注解里提供Bean的名称,如果是在方法中使用可以加在方法参数前面,Spring允许对方法的入参标注@Qualifiter已指定注入bean的名称。
5)@Autowired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的bean进行装配。
6)@Autowired注解也可以应用在集合属性上,此时spring读取集合的类型信息,然后自动装配所有与之兼容的bean
7)@Autowired注解在java.util.Map上时,若该Map的键值为String,那么Spring将自动装配与之Map值类型兼容的Bean,此时Bean的名称成为键值。
8)如果@Autowired注解自动装配时有多个同类型的bean那么就会按照名称去找。
下面我们对@Autowired注解进行应用,在我们项目当中,一般都是Controller类中会注入Service,Service类会注入Respository,我们就用上一篇文章中的代码,对各个类按照这个顺序进行注入,并且调用注入bean的方法。
UserController类:注入UserService类
@Controller
public class UserController {
private UserService userService;
@Autowired
public void setUserService(@Qualifier("userService") UserService userService) {
this.userService = userService;
} public void execute() {
System.out.println("UserController execute....");
userService.add();
}
}
UserService类:注入UserRepositoryImpl类
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public void add() {
System.out.println("UserService add...");
userRepository.save();
}
}
UserRepositoryImpl类
@Repository(value = "userRepository")
public class UserRepositoryImpl implements UserRepository { public void save() {
System.out.println("UserRepository Save。。。。");
}
}
运行测试类:
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
UserController userController = (UserController) ctx.getBean("userController");
System.out.println(userController);
UserService userService = (UserService) ctx.getBean("userService");
System.out.println(userService);
UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
System.out.println(userRepository);
TestObject testObject = (TestObject) ctx.getBean("testObject");
System.out.println(testObject);
}
}
输出结果:我们可以发现可以成功的输出四个对象,证明我们的注入式成功的。

3.使用@Resource或@Inject自动装配Bean
Spring还支持@Resource和@Inject注解,这两个注解和@Autowired注解的功能类似
@Resource:注解要求提供一个Bean名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为Bean的名称,并且它并不是Spring提供的注解而是import javax.annotation.Resource中的注解。
@Inject和:@Autowired注解一样是按类型匹配注入的Bean,但是没有required属性。
最后建议使用@Autowired注解。
Spring基础18——通过注解配置bean之间的关联关系的更多相关文章
- Spring基础——在 IOC 容器中 Bean 之间的关系
一.在 Spring IOC 容器中 Bean 之间存在继承和依赖关系. 需要注意的是,这个继承和依赖指的是 bean 的配置之间的关系,而不是指实际意义上类与类之间的继承与依赖,它们不是一个概念. ...
- 【Spring 基础】通过注解注入Bean
原课程:通过注解注入Bean 注入bean知识点思维导图 Spring 4.x推荐使用基于构造器的方式进行bean注入7.4.1 Dependency Injection spring为什么推荐使用构 ...
- IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置
1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...
- Spring IOC机制之使用注解配置bean
一. 通过注解配置bean 1.1 概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2 ...
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等
实验1:配置通过静态工厂方法创建的bean [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...
- Spring(十五):通过注解配置 Bean
在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...
- Spring框架入门之基于Java注解配置bean
Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...
- Spring 注解配置Bean
一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...
随机推荐
- Spring Data Jpa (三)定义查询方法
本章详细讲解如何利用方法名定义查询方法(Defining Query Methods) (1)定义查询方法的配置方法 由于Spring JPA Repository的实现原理是采用动态代理的机制,所以 ...
- sqli-lab(8)
布尔型单引号GET盲注 发现加个单引号跟没加显示不一样,加了单引号连you are in都不显示了,没有报错,所以只能用盲注判断了 0X01爱之先了解 盲注需要掌握一些MySQL的相关函数:lengt ...
- 【2019个推开发者节】亿级日活APP都在用的个推SDK, 现在全部免费!
1024程序员节来了 双11近了 各路满减.折扣.领券.秒杀.集赞 营销玩法猛于虎,一看优惠两毛五 日常拼命赶"需求" 修"Bug"的开发者们 想找个好用又不贵 ...
- [LeetCode]-DataBase-Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- swagger2简单使用
1.引入jar <!--引入swagger--> <dependency> <groupId>io.springfox</groupId> <ar ...
- 前端面试题-CSS选择器
一.CSS选择器作用 CSS 选择器用于定位我们想要给予样式的 HTML 元素,但不只是在 CSS 中,JavaScript 对 CSS 的选择器也是支持的,比如 document.document. ...
- Linux小记 -- apt-get install build-essential和yum groupinstall "Development Tools"
Ubuntu的apt install build-essential 作用:配置Debian系统编译环境,就是下载安装支持编译Debian包的依赖/包,比如gcc等. 直接执行 #apt instal ...
- IFG以太网帧间隙
交换机的线速 描述交换机性能可以使用“线速”这个概念,那它是什么意思呢?所谓的线速是指经过交换机处理的理想状态下最大数据率.描述数据率可以用bps(bit per second)和mpps(milli ...
- 代码测试:unsigned char*图像数据转换成OpenCV中Mat类型
直接使用Mat的构造函数,把指针的位置赋给下面中的data就OK了: Mat(int rows, int cols, int type, void* data, size_t step=AUTO_ST ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...