Spring的学习(三,Di)
Posted 韶光不负
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring的学习(三,Di)相关的知识,希望对你有一定的参考价值。
当我们学习完Spring的ioc控制反转(创建对象后),下面我们就需要来学习一下Di依赖注入的学习。
IOC的学习https://blog.csdn.net/weixin_47514459/article/details/125048675
目录
什么叫DI(依赖注入)
初始化成员变量,给成员变量进行赋值。
J2se给对象赋值
通过构造函数进行赋值
//设置构造函数
public Students(int id, String name, int age)
this.id = id;
this.name = name;
this.age = age;
//通过构造函数赋值
@Test
public void testStudent()
Students students = new Students(1,"小罗",18);
System.out.println(students);
通过Set方法
//设置Set方法给对象赋值
public void setId(int id)
this.id = id;
public void setName(String name)
this.name = name;
public void setAge(int age)
this.age = age;
import com.lsf.ssm.ioc.Students;
import org.junit.Test;
import static org.junit.Assert.*;
public class StudentTest
@Test
public void testStudent01()
Students students = new Students();
students.setName("小罗");
students.setId(1);
students.setAge(18);
System.out.println(students);
Spring方式给对象赋值(di)
构造注入
1,搭建ioc Spring的环境
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>ssm02</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 添加DI需要的BCCE依赖-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<!-- 添加测试需要的依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2,添加配置(配置文件(名称可以不同,但是不建议修改):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:p="http://www.springframework.org/schema/p"
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="students" class="com.lsf.ssm.ioc.Students">
<!-- 通过索引去使用 -->
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="小罗"></constructor-arg>
<constructor-arg index="2" value="18"></constructor-arg>
</bean>
</beans>
3,添加代码
import com.lsf.ssm.ioc.Students;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DiTest
@Test
public void testStudent()
//加载spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取对象
Students students = ac.getBean("students", Students.class);
System.out.println(students);
Set注入
1,环境搭建(与构造注入相同)
2,添加配置(配置文件(名称可以不同,但是不建议修改):applicationContext.xml)
调用的是对象当中的Set方法,去掉SET第一个字母小写(比如 :SetId =>id )
<!-- 先创建一个对象 -->
<bean id="students" class="com.lsf.ssm.ioc.Students">
<!-- 通过调用Set去赋值 -->
<property name="id" value="1"></property>
<property name="name" value="小罗"></property>
<property name="age" value="18"></property>
</bean>
3,添加代码
import com.lsf.ssm.ioc.Students;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DiTest
@Test
public void testStudent()
//加载spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取对象
Students students = ac.getBean("students", Students.class);
System.out.println(students);
DI(依赖注入)细节
构造注入
通过在bean—>constructor-arg子标签来实现,该子标签里有两个属性:
index:表示构造函数参数的索引
type:表示构造函数参数的类型
如果构造函数里的形参类型不一样,可以通过类型和索引都可以赋值;但是如果构造函数里的形参类型有一样,只能通过索引赋值。
当属性为基本类型时,使用value,当是对象类型时,使用rel赋值
//基本数据类型+String类型赋值
<constructor-arg index="" value=""></constructor-arg>
//对象赋值
<property name="" ref=""></property>
注入null值与空
//注入为null
<property name="name" >
<null></null>
</property>
//注入为空
<property name="name" value=""></property>
处理属性
//设置属性
private Properties properties;
public Properties getProperties()
return properties;
public void setProperties(Properties properties)
this.properties = properties;
//配置xml
<bean id="students" class="com.lsf.ssm.ioc.Students">
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key1">value1</prop>
<prop key="key1">value1</prop>
</props>
</property>
</bean>
//遍历
import com.lsf.ssm.ioc.Students;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Enumeration;
import java.util.Properties;
public class DiTest
@Test
public void testStudent()
//加载spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取对象
Students students = ac.getBean("students", Students.class);
Properties properties = students.getProperties();
Enumeration e = properties.propertyNames();
while (e.hasMoreElements())
String key = (String) e.nextElement();
String value = properties.getProperty(key);
System.out.println(key + " = " +value);
处理集合类型或容器类型
//类增加属性,设置set与get方法
private int[] arrays;
public int[] getArrays()
return arrays;
public void setArrays(int[] arrays)
this.arrays = arrays;
//xml配置
<bean id="students" class="com.lsf.ssm.ioc.Students">
<property name="arrays">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
//遍历数组
import com.lsf.ssm.ioc.Students;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Enumeration;
import java.util.Properties;
public class DiTest
@Test
public void testStudent()
//加载spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取对象
Students students = ac.getBean("students", Students.class);
int[] arrays = students.getArrays();
for (int enty:
arrays)
System.out.println(enty);
当集合中是对象形式
//配置对象set与get方法
private List<Date> dates;
public List<Date> getDates()
return dates;
public void setDates(List<Date> dates)
this.dates = dates;
//配置xml
<bean id="date1" class="java.util.Date"></bean>
<bean id="date2" class="java.util.Date"></bean>
<bean id="date3" class="java.util.Date"></bean>
<!-- 先创建一个对象 -->
<bean id="students" class="com.lsf.ssm.ioc.Students">
<property name="arrays">
<list>
<ref bean="date1"></ref>
<ref bean="date2"></ref>
<ref bean="date3"></ref>
</list>
</property>
</bean>
//遍历
import com.lsf.ssm.ioc.Students;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
public class DiTest
@Test
public void testStudent()
//加载spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取对象
Students students = ac.getBean("students", Students.class);
List<Date> dates = students.getDates();
for (Date enty:
dates)
System.out.println(enty);
Set注入
调用的是对象当中的Set方法,去掉SET第一个字母小写(比如 :SetId =>id )
set注入先创建对象,再进行赋值(没有无参构造会报错)
当在类当中创建了,有构造方法,无参构造就不会自动生成,需要自己手动配置。不配置就会报错(就需要自己手动添加无参构造)
以上是关于Spring的学习(三,Di)的主要内容,如果未能解决你的问题,请参考以下文章