DI:Dependency Injection
共有三种方式
在前面IOC容器创建对象的方式中已经提到,无参构造器和有参构造器都可以。
依赖注入:本质是Set注入
依赖:
bean对象的创建依赖于容器
注入:
bean对象中的所有属性由容器来注入
复杂类型
Address实体类:
package xyz.luck1y.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
真实测试环境
Student实体类:
package xyz.luck1y.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String, String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
xml配置:
<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="student" class="xyz.luck1y.pojo.Student">
<property name="name" value="刘子"/>
bean>
beans>
测试环境:
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xyz.luck1y.pojo.Student;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
}
xml配置:
<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="address" class="xyz.luck1y.pojo.Address">
<property name="address" value="南京"/>
bean>
<bean id="student" class="xyz.luck1y.pojo.Student">
<property name="name" value="刘子"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>《红楼梦》value>
<value>《水浒传》value>
<value>《三国演义》value>
<value>《西游记》value>
array>
property>
<property name="hobbys">
<list>
<value>听歌value>
<value>健身value>
<value>敲代码value>
list>
property>
<property name="card">
<map>
<entry key="身份证" value="222303220222122222"/>
<entry key="学生证" value="202241803119"/>
map>
property>
<property name="games">
<set>
<value>LOLvalue>
<value>永劫无间value>
<value>瓦罗兰特value>
set>
property>
<property name="wife">
<null/>
property>
<property name="info">
<props>
<prop key="driver">jdbcDriverprop>
<prop key="url">https://www.baidu.comprop>
<prop key="username">rootprop>
<prop key="password">123456prop>
props>
property>
bean>
beans>
测试
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xyz.luck1y.pojo.Student;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
/*
Student{
name='刘子',
address=Address{address='南京'},
books=[《红楼梦》, 《水浒传》, 《三国演义》, 《西游记》],
hobbys=[听歌, 健身, 敲代码],
card={身份证=222303220222122222, 学生证=202241803119},
games=[LOL, 永劫无间, 瓦罗兰特],
wife='null',
info={password=123456,
driver=jdbcDriver,
url=https://www.baidu.com,
username=root}
}
*/
}
}
P命名空间和C命名空间在配置中都需要导入约束。一个无参,一个有参。其实p就是property,c就是constructor-arg
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
实体类:
package xyz.luck1y.pojo;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
配置xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="xyz.luck1y.pojo.User" p:age="21" p:name="刘子">
bean>
beans>
测试:
@Test
public void test2(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
// 如果显式地声明类型则不用强转
Object user = context.getBean("user", User.class);
System.out.println(user);
}
实体类需要增加有参构造:
package xyz.luck1y.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
xml配置:
<bean id="user2" class="xyz.luck1y.pojo.User" c:age="22" c:name="刘同学">
bean>
测试:
@Test
public void test2(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
// 如果显式地声明类型则不用强转
Object user = context.getBean("user2", User.class);
System.out.println(user);
}
这两种拓展命名方式需要导入相应的约束~
单例模式(Spring默认机制)
每一次getBean得到的对象都是同一个,hashcode都是相同的
<bean id="user2" class="xyz.luck1y.pojo.User" c:age="22" c:name="刘同学" scope="singleton">
bean>
原型模式
每一次getBean得到的对象不一样,hashCode不同
<bean id="user2" class="xyz.luck1y.pojo.User" c:age="22" c:name="刘同学" scope="prototype">
bean>
其余的request、session、application、websocket只会在web开发中使用到。
在Spring中有三种装配方式:
一个人有两只宠物:一只猫,一只狗
实体类:
People
package xyz.luck1y.pojo;
public class People {
private Cat cat;
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
Dog
package xyz.luck1y.pojo;
public class Dog {
public void shout(){
System.out.println("汪汪汪~");
}
}
Cat
package xyz.luck1y.pojo;
public class Cat {
public void shout(){
System.out.println("喵喵喵~");
}
}
配置bean.xml
<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="xyz.luck1y.pojo.Cat"/>
<bean id="dog" class="xyz.luck1y.pojo.Dog"/>
<bean id="people" class="xyz.luck1y.pojo.People">
<property name="name" value="刘子"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
bean>
beans>
测试
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xyz.luck1y.pojo.People;
public class MyTest {
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getDog().shout();
people.getCat().shout();
}
}
结果:
byName:会自动在容器上下文查找,和自己属性set方法后面的值对应的beanID
比如cat属性的set方法为setCat,就会自动寻找id为cat的bean进行ref引用,Spring会将setCat方法的Cat自动转换为小写cat,byName只能取到id为小写的,不能取到id为大写的,并且需要保证id唯一。
<bean id="people" class="xyz.luck1y.pojo.People" autowire="byName">
<property name="name" value="刘子"/>
bean>
byType:会自动在容器上下文查找,和自己属性set方法参数的类型相同的beanID
但是如果有两个Cat属性的Bean 就会失败报错,必须保证类型全局唯一,甚至可以省略id进行装配
<bean id="people2" class="xyz.luck1y.pojo.People" autowire="byType">
<property name="name" value="刘子"/>
bean>
小结:
JDK从1.5开始支持注解,Spring从2.5开始支持注解。
使用注解:
在配置文件中导入约束:
xmlns:context="http://www.springframework.org/schema/context"
配置开启注解的支持:
<context:annotation-config/>
<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>
在属性上使用也可以在set方法上使用
@Autowired
private Cat cat;
@Autowired
private Dog dog;
使用@Autowired
注解后,我们甚至可以不用编写set方法,前提是要自动装配的属性在IOC(Spring)容器中存在,并且符合ByName
其实这个注解的实现默认就是ByType,当ByType查找不到或当ByType的满足个数大于1的时候就会使用ByName
科普:
@Nullable 属性标记了这个注解,说明这个属性可以为null
进入@Autowired可以看到是有一个属性的:
public @interface Autowired {
boolean required() default true;
}
如果显示的定义了required属性为false,说明这个属性可以为null(默认不能为空),可以不用在bean中装配。一般来说包装类默认可以是null,不需要声明,但是一些基本属性想要默认为null的时候,可以这样声明。
@Autowired(required = false)
private Cat cat;
如果@Autowired
自动装配的环境比较复杂,自动装配无法通过@Autowired
一个注解来完成,我们可以通过组合@Autowired
和@Qualifier
,指定具体的一个bean:如果同时有两个Dog类的bean,一个id为dog111,一个为dog222,这个时候byType查不到了,因为有两个,所以就去ByName,但是发现两个bean的id也跟setDog方法的“dog”不对应,这时候就不知道要用哪一个,会报错!
然后我们就可以通过@Qualifier
指定ByName使用。
@Autowired
@Qualifier(value = "dog222")
private Dog dog;
拓展:
除了使用Spring的自动装配注解外,Java的jdk也提供了一个默认的装配注解:@Resource
,但是jdk11取消了这个注解,因此存在版本兼容问题。这个注解默认是按照ByName进行匹配,匹配不到用ByType进行匹配。@Resource(name=“cat2”)
,同样的,这个注解也可以指定匹配一个bean。
@Resource和@Autowired的区别:
都是用来自动装配的,都可以放在属性或者set方法之前
@Resource:
@Autowired:
默认使用ByType匹配,匹配不到直接报错!匹配数大于1或匹配到多个,再使用ByName进行匹配。
可以指定属性是否为空:@Autowired(required=false),false:可以空,true:不能为空
可以结合@Qualifier通过byName匹配给出的bean:
@Autowired
@Qualifier(value="")
如果同时配置了@Qualifier(value=“”),就会去寻找配置的这个值,这时候找不到就直接报错,会自动屏蔽ByType匹配,如果找到了,会判断type,只有type也匹配才会成功。