Bean的作用域和自动装配
-
Spring Bean的作用域主要有五种
- Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,singleton作用域是Spring中的缺省作用域(默认的作用域)。
- prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。
-
spring中提供了向Bean中自动注入依赖的功能,这个过程就是自动装配,当向bean中注入的内容非常多的时候,自动注入依赖的功能将极大的节省注入程序的时间。
-
Spring自动装配有两类:
- 基于xml文件的自动装配:byType(类型),byName(名称), constructor(根据构造函数)
- 基于注解的自动装配:@Autowired,@Resource,@Value
-
举例实现
- byname
<!--手动注入--> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cat" class="com.kuang.pojo.Cat"/> <bean id="dog" class="com.kuang.pojo.Dog"/> <bean id="people" class="com.kuang.pojo.Peopel"> <property name="name" value="张三"/> <property name="cat" ref="cat"/> <property name="dog" ref="dog"/> </bean> </beans> <!--自动装配--> <bean id="people" class="com.kuang.pojo.Peopel" autowire="byName"> <property name="name" value="张三"/> </bean> <!--Spring会根据class属性找到实体类,然后查询实体类中所有setter方法的名字,根据setter方法后面的名字(例如SetDog,则setter方法后面的名字为dog)再到配置文件中寻找一个与该名字相同id的Bean,注入进来-->
- bytype
<!--自动装配--> <bean id="people" class="com.kuang.pojo.Peopel" autowire="byType"> <property name="name" value="张三"/> </bean> <!--设置autowire属性为byType,那么Spring会自动寻找一个与该属性类型相同的Bean,注入进来。必须保证配置文件中所有bean的class属性的值是唯一的-->
-
-
注解实现自动装配:注解是通过反射来实现的,可以没有set方法
- spring的xml文件中加相应的约束,配置开启注解的方式
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> </beans>
- 得到干净的配置文件
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="cat" class="com.kuang.pojo.Cat"/> <bean id="dog" class="com.kuang.pojo.Dog"/> <bean id="people" class="com.kuang.pojo.Peopel"> <property name="name" value="张三"/> </bean> </beans>
- 在实体类的属性或者set方法写添加@Autowired,实现自动装配
public class Peopel { @Autowired private Cat cat; @Autowired private Dog dog; private String name; ... }
-
spring会默认优先根据属性类型(byType)去容器中找对应的组件(bean),找到就赋值;若找到多个相同类型的组件,再将属性的名称作为组件(bean)的id去容器中查找。
-
当ioc容器根据属性类型去容器中找找到多个相同类型的组件,配合使用@Qualifier注解:@Qualifier指定需要装配的组件的id,而不是使用属性名。
-
Resource注解:@Resource同样可以实现自动装配功能,但它默认是按照组件名称进行装配的,按照组件名称找不到在根据属性类型去查找,再找不到就报错, @Resource是java规范。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)