概述
IOC非常重要的一点是值注入。特别常用于一些初始化工作,例如数据库连接的ip、端口等信息。此外list、map等这类集合的注入也比起基本类型的注入麻烦一些,值得实践和记录一下。
依赖注入
可以通过XML或注解方式进行注入下面分别展示两种方式
XML方式
创建Home类,代表每个学生对应的家庭:
public class Home {
private int id;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Student类需要包含一个属性是Home:
package com.sadoshi.springtest.ioc.entity;
public class Student {
private int id;
private String name;
private Home home;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Home getHome() {
return home;
}
public void setHome(Home home) {
this.home = home;
}
}
然后创建xml文件,这里命名为spring.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.sadoshi.springtest.ioc.entity.Student">
<!-- 值注入 -->
<property name="name" value="jack"/>
<!-- 对象注入 -->
<property name="home" ref="home"/>
</bean>
<bean id="home" class="com.sadoshi.springtest.ioc.entity.Home">
<property name="address" value="beijing"/>
</bean>
</beans>
这里分别用了值注入和对象注入,然后验证:
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Student student = context.getBean("student", Student.class);
System.out.println("值注入:"+student.getName());
System.out.println("对象注入:"+student.getHome().getAddress());
}
}
控制台输出,可以看到jack通过值注入,被注入到name属性中。而home也作为对象注入,能获取到其address的值:
、
注解方式
注解方式通过@Component注解其为一个容器中的一个bean。@Value是值注入
@Component("home")
public class Home {
private int id;
@Value("shanghai")
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
接着student类要使用@Resource注解,指定注入对象的id:
@Component
public class Student {
private int id;
@Value("rose")
private String name;
@Resource(name="home")
private Home home;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Home getHome() {
return home;
}
public void setHome(Home home) {
this.home = home;
}
}
在使用@Resourece对象时,还需要添加以下包,才能使其生效:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
之后可进行验证,和xml方式雷同,这里就不展示了。
集合的依赖注入
关于集合的依赖注入不难,但比起一般的值注入还是有点不一样,要展示一下。依然是分xml方式和注解方式
XML方式
定义MyCollection类,包括list、set、map这三种常见集合:
public class MyCollection {
private List<String> list;
private Set<Integer> set;
private Map<String, Long> map;
public Set<Integer> getSet() {
return set;
}
public void setSet(Set<Integer> set) {
this.set = set;
}
public Map<String, Long> getMap() {
return map;
}
public void setMap(Map<String, Long> map) {
this.map = map;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
通过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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myCollection" class="com.sadoshi.springtest.ioc.entity.MyCollection">
<property name="list">
<list>
<value>a</value>
<value>b</value>
<value>c</value>
</list>
</property>
<property name="set">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
<property name="map">
<map>
<entry key="one" value="1" />
<entry key="two" value="2" />
<entry key="three" value="3" />
</map>
</property>
</bean>
</beans>
之后进行验证:
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
MyCollection Col = context.getBean("myCollection", MyCollection.class);
System.out.println("list注入:"+Col.getList());
System.out.println("set注入:"+Col.getSet());
System.out.println("map注入:"+Col.getMap());
}
}
控制台输出,显示已成功注入:
注解方式
注解方式需要使用@Value,注解里面使用的是SpEL表达式,后面文章会讲讲
@Component
public class MyCollection {
@Value("#{'1,2,3,4,5'.split(',')}")
private List<String> list;
@Value("#{'6,7,8,9,0'.split(',')}")
private Set<Integer> set;
@Value("#{{'one':1, 'two':2, 'three':3}}")
private Map<String, Long> map;
public Set<Integer> getSet() {
return set;
}
public void setSet(Set<Integer> set) {
this.set = set;
}
public Map<String, Long> getMap() {
return map;
}
public void setMap(Map<String, Long> map) {
this.map = map;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
验证部分雷同,不进行展示。