一、基于Bean 的自动装配方式
- 自动装配是Spring满足Bean依赖的一种方式!
- Spring会在上下文中自动寻找,并自动给Bean装配属性!
在Spring中有三种装配的方式
1、在xml 中显示配置
2、在Java中显示配置
3、隐式的自动装配Bean【重要】
<bean id="cat" class="com.ZQQQ.pojo.Cat"/>
<bean id="dog" class="com.ZQQQ.pojo.Dog"/>
<!--byName 的方式自动装配 -->
<bean name="person" class="com.ZQQQ.pojo.Person" autowire="byName"/>
</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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.ZQQQ.pojo.Cat"/>
<bean id="dog" class="com.ZQQQ.pojo.Dog"/>
<!--byName 的方式自动装配 -->
<bean name="person" class="com.ZQQQ.pojo.Person" autowire="byType"/>
</beans>
-
小结:
- byName的时候,需要保证所有Bean的id唯一,并且这个Bean需要和自动注入的属性set方法的值一致!
- byType 的时候,需要保证所有Bean的class唯一,并且这个Bean需要和自动注入的属性类型一致!
二、基于注解实现自动装配方式
- 概述:JDK1.5 就支持注解了。Spring 2.5 就支持注解了
- 使用注解自动装配需要导入的依赖如下:
<?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
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/>
</beans>
完整版案例:
- pojo类:
public class Cat {
public void shot(){
System.out.println(“喵喵~”);
}
}
public class Dog {
public void shot(){
System.out.println(“汪汪~~”);
}
}
package com.ZQQQ.pojo;
import org.springframework.beans.factory.annotation.Autowired;
/*
* 自动装配Bean 学习
* 在上下文配置中dog、cat 是引用类型,所以使用ref
*/
public class Person {
@Autowired
private Dog dog;
@Autowired
private Cat cat;
private String name;
/**
* 使用@Autowired 实现自动装配Bean 不需要提供set 方法,只需get方法即可!!
* /
public Dog getDog() {
return dog;
}
public Cat getCat() {
return cat;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Person{" +
"dog=" + dog +
", cat=" + cat +
", name='" + name + '\'' +
'}';
}
}
- Applicationcontext.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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="cat" class="com.ZQQQ.pojo.Cat"/>
<bean id="dog" class="com.ZQQQ.pojo.Dog"/>
<!--byName 的方式自动装配 -->
<bean name="person" class="com.ZQQQ.pojo.Person" autowire="byName"/>
<!--开启基于注解的自动装配的方式 -->
<context:annotation-config/>
</beans>
- 测试用例
import com.ZQQQ.pojo.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void Person(){
ApplicationContext context = new ClassPathXmlApplicationContext(“Applicationcontext.xml”);
// 如下注意类型要和上下文中的类型名称保持一致
Person bean = context.getBean(“person”, Person.class);
bean.getCat().shot();
bean.getDog().shot();
}
}
打印结果:
@Autowired 使用
-
@Autowired 直接在属性上使用即可,也可以在set 方法上使用。
-
使用@Autowired 可以省略Set方法,前提是这个自动装配的的属性在IOC(Spring容器)中存在,且符合命名字:ByName!
@Nullable使用
- @Nullable
字段标记了这个注解,说明这个字段可以为null
@Autowired与@Qualifier(value = “xxxx”) 组合使用
- @Autowired
@Qualifier(value = “dog222”)
Autowired和Qualifier 二者组合使用可以显示的指定唯一的id,实现自动装配。
在使用的时候,在各自对应的属性上加上注解不然会报错。谨记!!!
@Resource 使用
1.使用方法:在使用的时候也是在属性上面加上此注解也可以实现自动装配Bean的功能!
2.@Resource(name = “Xxxxx”) 也可以指定唯一id,实现自动装配。但需要在resources下的Bean容器中的Id 和你在属性类中的名称保持一致。
小结:
1.都可以用来实现自动装配Bean 的功能,都可以放在属性字段上。
2.@Autowired 通过bytype 的方式实现,而且必须要求这个对象存在。
3.@Resource 默认通过byname 的方式实现,如果找不到名字,则通过byType 的方式实现!如果两个都找不到的情况下就报错!
执行顺序:
@Autowired通过bytype的方式实现
@Resource 默认通过byName的方式实现
使用基于注解实现自动装配需要导入对应的约束。
-----------------------------------------要克服生活的焦虑和沮丧,得先学会做自己的主人-----------------------------------------