自动装配是Spring满足bean依赖的一种方式!
Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种装配的方式
在xml中显示的配置
在java中显示配置
隐式的自动装配bean【重要】
搭建环境
第一步:编写实体类
public class Cat {
?
public void bark(){
System.out.println("猫叫~喵喵喵");
}
}
public class Dog {
public void bark(){
System.out.println("狗叫~汪汪汪");
}
}
public class People {
?
private Cat cat;
private Dog dog;
private String name;
}
第二步:编写spring主配置文件
第四步:编写测试类
public class TestPeople {
?
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
?
People people = context.getBean("people", People.class);
?
people.getCat().bark();
people.getDog().bark();
?
}
}
--------------------------------测试成功---------------------------------
ByName自动装配的原理
ByName会在上下文查找对应的bean的id是否与set注入的属性id一致
ByName是通过bean标签中autowire属性决定的 下面是xml配置
实现方式简单讲就是下面第一个bean中id就是cat 实体类中set方法属性也是cat对应上了,所以spring为我们自动装配上了
<!--byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid!-->
<bean id="cat" class="com.xuan.pojo.Cat"/>
<bean id="dog" class="com.xuan.pojo.Dog"/>
?
<bean id="people" class="com.xuan.pojo.People" autowire="byName">
<property name="name" value="jieshen"/>
</bean>
ByType自动装配的原理:
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean。
byType是通过bean标签中autowire属性决定的 下面是xml配置
实现方式简单点讲就是下面第一个bean中Class属性直接指定了唯一的Cat类与实体类注入的类型一致且唯一基于此spring才能我们装配上
<!--byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean!-->
<bean id="cat" class="com.xuan.pojo.Cat"/>
<bean id="dog" class="com.xuan.pojo.Dog"/>
?
<bean id="people" class="com.xuan.pojo.People" autowire="byType">
<property name="name" value="jieshen"/>
</bean>
首先根据挂网我们要使用注解的话得先导入context上下文约束以及开启注解支持
第一步:导入context上下文约束以及开启注解支持
第二步:在实体类的属性上加上@Autowired注解
public class People {
?
----------------------------------------测试成功------------------------------------
跟进并查看该注解
package org.springframework.beans.factory.annotation;
?
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
?
跟进并查看该注解
package org.springframework.beans.factory.annotation;
?
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//作用可以在字段上也就是传参上
//Qualifier的作用是在多个bean对象中指定要找的那一个
测试代码
public class People {
?
<bean id="dog" class="com.xuan.pojo.Dog"/>
<bean id="dog1" class="com.xuan.pojo.Dog"/>
<bean id="dog2" class="com.xuan.pojo.Dog"/>
还可以@Qualifier
在各个构造函数参数或方法参数上指定注释
下面是官网的例子
public class MovieRecommender {
?
private MovieCatalog movieCatalog;
?
private CustomerPreferenceDao customerPreferenceDao;
?
The bean with the main
qualifier value is wired with the constructor argument that is qualified with the same value.
具有action
限定符值的Bean与限定有相同值的构造函数参数连接。
spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。
@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。
所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
@Resource装配顺序
如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;
区别:
@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。
@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使
@Resource(这个注解属于J2EE的),默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
推荐使用:@Resource注解在字段上,这样就不用写setter方法了,并且这个注解是属于J2EE的,减少了与spring的耦合。这样代码看起就比较优雅。
自动装配是Spring满足bean依赖的一种方式!
Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种装配的方式
在xml中显示的配置
在java中显示配置
隐式的自动装配bean【重要】
搭建环境
第一步:编写实体类
public class Cat {
?
public void bark(){
System.out.println("猫叫~喵喵喵");
}
}
public class Dog {
public void bark(){
System.out.println("狗叫~汪汪汪");
}
}
public class People {
?
private Cat cat;
private Dog dog;
private String name;
}
第二步:编写spring主配置文件
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
?
?
<bean id="cat" class="com.xuan.pojo.Cat"/>
<bean id="dog" class="com.xuan.pojo.Dog"/>
?
<bean id="people" class="com.xuan.pojo.People">
<property name="name" value="jieshen"/>
<property name="dog" ref="dog"/>
<property name="cat" ref="cat"/>
</bean>
?
?
</beans>
第四步:编写测试类
public class TestPeople {
?
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
?
People people = context.getBean("people", People.class);
?
people.getCat().bark();
people.getDog().bark();
?
}
}
--------------------------------测试成功---------------------------------
ByName自动装配的原理
ByName会在上下文查找对应的bean的id是否与set注入的属性id一致
ByName是通过bean标签中autowire属性决定的 下面是xml配置
实现方式简单讲就是下面第一个bean中id就是cat 实体类中set方法属性也是cat对应上了,所以spring为我们自动装配上了
<!--byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid!-->
<bean id="cat" class="com.xuan.pojo.Cat"/>
<bean id="dog" class="com.xuan.pojo.Dog"/>
?
<bean id="people" class="com.xuan.pojo.People" autowire="byName">
<property name="name" value="jieshen"/>
</bean>
ByType自动装配的原理:
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean。
byType是通过bean标签中autowire属性决定的 下面是xml配置
实现方式简单点讲就是下面第一个bean中Class属性直接指定了唯一的Cat类与实体类注入的类型一致且唯一基于此spring才能我们装配上
<!--byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean!-->
<bean id="cat" class="com.xuan.pojo.Cat"/>
<bean id="dog" class="com.xuan.pojo.Dog"/>
?
<bean id="people" class="com.xuan.pojo.People" autowire="byType">
<property name="name" value="jieshen"/>
</bean>
首先根据挂网我们要使用注解的话得先导入context上下文约束以及开启注解支持
第一步:导入context上下文约束以及开启注解支持
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
?
<!--开启注解支持-->
<context:annotation-config/>
?
<bean id="cat" class="com.xuan.pojo.Cat"/>
<bean id="dog" class="com.xuan.pojo.Dog"/>
<bean id="people" class="com.xuan.pojo.People"/>
?
</beans>
第二步:在实体类的属性上加上@Autowired注解
public class People {
?
private Cat cat;
private Dog dog;
private String name;
}
----------------------------------------测试成功------------------------------------
跟进并查看该注解
package org.springframework.beans.factory.annotation;
?
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
?
ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) ({
RetentionPolicy.RUNTIME) (
public @interface Autowired {
?
/**
* Declares whether the annotated dependency is required.
* <p>Defaults to {@code true}.
* 这个required属性默认是true 如果设置位false表明允许@Autowired标注的属性等为空
*/
boolean required() default true;
?
}
跟进并查看该注解
package org.springframework.beans.factory.annotation;
?
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//作用可以在字段上也就是传参上
//Qualifier的作用是在多个bean对象中指定要找的那一个
ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE}) ({
RetentionPolicy.RUNTIME) (
public @interface Qualifier {
?
String value() default "";
?
}
测试代码
public class People {
?
private Cat cat;
value = "dog2")//可以指定要找确切的哪一个id (
private Dog dog;
private String name;
}
<bean id="dog" class="com.xuan.pojo.Dog"/>
<bean id="dog1" class="com.xuan.pojo.Dog"/>
<bean id="dog2" class="com.xuan.pojo.Dog"/>
还可以@Qualifier
在各个构造函数参数或方法参数上指定注释
下面是官网的例子
public class MovieRecommender {
?
private MovieCatalog movieCatalog;
?
private CustomerPreferenceDao customerPreferenceDao;
?
public void prepare( ("main") MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
?
// ...
}
<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 class="example.SimpleMovieCatalog">
<qualifier value="main"/>
?
<!-- inject any dependencies required by this bean -->
</bean>
?
<bean class="example.SimpleMovieCatalog">
<qualifier value="action"/>
?
<!-- inject any dependencies required by this bean -->
</bean>
?
<bean id="movieRecommender" class="example.MovieRecommender"/>
?
</beans>
The bean with the main
qualifier value is wired with the constructor argument that is qualified with the same value.
具有action
限定符值的Bean与限定有相同值的构造函数参数连接。
spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。
@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。
所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
@Resource装配顺序
如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;
区别:
@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。
@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使
@Resource(这个注解属于J2EE的),默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
推荐使用:@Resource注解在字段上,这样就不用写setter方法了,并且这个注解是属于J2EE的,减少了与spring的耦合。这样代码看起就比较优雅。
原文:https://www.cnblogs.com/xuan-study/p/13325368.html