1.DI
概述:DI(Dependency Injection)依赖注入,在Spring创建对象的同时,为其属性赋值,称之为依赖注入。
1.1构造函数注入
顾名思义,就是使用类中的构造函数,给成员变量赋值。注意,赋值的操作不是我们自己做的,而是通过配置的方式,让spring框架来为我们注入。具体代码如下:
1.1.1service
package com.by.service;
import com.by.dao.UserDao;
import com.by.dao.UserDaoImpl;
public class UserServiceImpl implements UserService{
private UserDao userDao;
private String msg;
public UserServiceImpl() {
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void setMsg(String msg) {
this.msg = msg;
}
public UserServiceImpl(UserDao userDao, String msg) {
this.userDao = userDao;
this.msg = msg;
}
@Override
public void adduser() {
System.out.println(userDao+"============"+msg);
userDao.adduser();
}
}
1.1.2applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
2、把对象交给spring来创建
id:给对象在容器中提供一个唯一标识。用于获取对象
class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数
-->
<bean id="userDao" class="com.by.dao.UserDaoImpl"></bean>
<bean id="userService" class="com.by.service.UserServiceImpl">
<!-- <bean id="userService" class="com.by.service.UserServiceImpl" scope="prototype"-->
<!-- init-method="init" destroy-method="destroy" autowire="byType">-->
<!-- 构造器注入
要求:类中需要提供一个对应参数列表的构造函数。
标签:constructor-arg
==给谁赋值:==
index:指定参数在构造函数参数列表的索引位置
name:指定参数在构造函数中的名称
==赋什么值:==
value:它能赋的值是基本数据类型和String类型
ref:它能赋的值是其他bean类型,也就是说,必须得是在配置文件中配置过的bean
-->
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
<constructor-arg index="1" value="我真帅"></constructor-arg>
</bean>
</beans>
1.1.3测试
package com.by.web;
import com.by.dao.UserDao;
import com.by.service.UserService;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ContextResource;
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
* 测试di
*/
UserService userService = context.getBean("userService", UserService.class);
userService.adduser();
}
}
1.1.4结果
1.2set方法注入
顾名思义,就是在类中提供需要注入成员的set方法。具体代码如下:
1.2.1service
package com.by.service;
import com.by.dao.UserDao;
import com.by.dao.UserDaoImpl;
public class UserServiceImpl implements UserService{
private UserDao userDao;
private String msg;
public UserServiceImpl() {
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void setMsg(String msg) {
this.msg = msg;
}
public UserServiceImpl(UserDao userDao, String msg) {
this.userDao = userDao;
this.msg = msg;
}
@Override
public void adduser() {
System.out.println(userDao+"============"+msg);
userDao.adduser();
}
}
1.2.2applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
2、把对象交给spring来创建
id:给对象在容器中提供一个唯一标识。用于获取对象
class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数
-->
<bean id="userDao" class="com.by.dao.UserDaoImpl"></bean>
<bean id="userService" class="com.by.service.UserServiceImpl">
<!-- set注入
==给谁赋值:==
name:找的是类中set方法后面的部分
==赋什么值:==
value:它能赋的值是基本数据类型和String类型
ref:它能赋的值是其他bean类型,也就是说,必须得是在配置文件中配置过的bean
-->
<property name="userDao" ref="userDao"></property>
<property name="msg" value="我今年发大财"></property>
</bean>
</beans>
1.2.3测试
package com.by.web;
import com.by.dao.UserDao;
import com.by.service.UserService;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ContextResource;
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
* 测试di
*/
UserService userService = context.getBean("userService", UserService.class);
userService.adduser();
}
}
1.2.4结果
1.3自动注入
不用在配置中 指定为哪个属性赋值,由spring自动根据某个 "原则" ,在工厂中查找一个bean并为属性注入值。具体代码如下:
1.3.1service
package com.by.service;
import com.by.dao.UserDao;
import com.by.dao.UserDaoImpl;
public class UserServiceImpl implements UserService{
private UserDao userDao;
private String msg;
public UserServiceImpl() {
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void setMsg(String msg) {
this.msg = msg;
}
public UserServiceImpl(UserDao userDao, String msg) {
this.userDao = userDao;
this.msg = msg;
}
@Override
public void adduser() {
System.out.println(userDao+"============"+msg);
userDao.adduser();
}
}
1.3.2applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
2、把对象交给spring来创建
id:给对象在容器中提供一个唯一标识。用于获取对象
class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数
-->
<bean id="userDao" class="com.by.dao.UserDaoImpl"></bean>
<bean id="userService" class="com.by.service.UserServiceImpl" autowire="byType">
</bean>
</beans>
1.3.3测试
package com.by.web;
import com.by.dao.UserDao;
import com.by.service.UserService;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ContextResource;
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
* 测试di
*/
UserService userService = context.getBean("userService", UserService.class);
userService.adduser();
}
}
1.3.4结果
1.4注入集合类型的属性
顾名思义,就是给类中的集合成员传值,它用的也是set方法注入的方式,只不过变量的数据类型都是集合。我们这里介绍注入数组,List,Set,Map。具体代码如下:
1.4.1service
package com.zhy.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;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + 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;
}
}
1.4.2applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--1、注意:要导入schema约束-->
<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="com.zhy.pojo.Address">
<property name="address" value="驻马店"></property>
</bean>
<bean id="student" class="com.zhy.pojo.Student">
<!-- 第一种!普通注入,value-->
<property name="name" value="赵玉真"></property>
<!-- 第二种Bean注入-->
<property name="address" ref="address"></property>
<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="111111222222333333"></entry>
<entry key="银行卡" value="444444444444777777"></entry>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BoB</value>
</set>
</property>
<property name="wife">
<null></null>
</property>
<property name="info">
<props>
<prop key="sex">男</prop>
<prop key="ID">20231228</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
<prop key="role">懂事早</prop>
</props>
</property>
</bean>
</beans>
1.4.3测试
import com.zhy.pojo.Student;
import com.zhy.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
/**
* Student{name='赵玉真', address=Address{address='驻马店'},
* books=[西游记, 水浒传, 红楼梦, 三国演义],
* hobbys=[听歌, 看电影, 敲代码],
* card={身份证=111111222222333333, 银行卡=444444444444777777},
* games=[LOL, COC, BoB],
* wife='null',
* info={ID=20231228, password=123456, sex=男, username=root, role=懂事早}}
*/
}
}