自动装配是Spring知足bean依赖的一种方式!java
Spring会在上下文中自动寻找,并自动给bean装配属性!spring
在Spring中有三种装配的方式app
在xml中显示的配置ide
在java中显示配置函数
隐式的自动装配bean【重要】测试
搭建环境ui
第一步:编写实体类this
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主配置文件atom
第四步:编写测试类spa
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的耦合。这样代码看起就比较优雅。