一、环境搭建
1.tomcat:Apache Tomcat® - Welcome!
2.源码、jar包链接:https://pan.baidu.com/s/1a_BjXdAYXz7s-k7BTBgv_Q
提取码:4567
--来自百度网盘超级会员V2的分享
二、简介
接上篇set注入,本篇则演示特殊类型的注入
三、项目基本搭建,jar包导入
四、代码展示
1.pojo
Person.java(此类中就是本次注入的特殊类型)
package cn.lexed.pojo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Person {
private String name;
private int age;
private double money;
private List<String> list;
private Set<String> set;
private Map<String, String> map;
private String[] array;
private User user;
private String empty;//空字符串
private String nulll;//null
public Person() {
super();
}
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;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getEmpty() {
return empty;
}
public void setEmpty(String empty) {
this.empty = empty;
}
public String getNulll() {
return nulll;
}
public void setNulll(String nulll) {
this.nulll = nulll;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", money=" + money + ", list=" + list + ", set=" + set
+ ", map=" + map + ", array=" + Arrays.toString(array) + ", user=" + user + ", empty=" + empty
+ ", nulll=" + nulll + "]";
}
}
User.java(呼应Person的类属性)
package cn.lexed.pojo;
public class User {
private String username;
private String password;
public User() {
super();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
2.resources
applicationContext.xml
进行set注入:
<?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="per" class="cn.lexed.pojo.Person">
<property name="name" value="Tom"></property>
<property name="age" value="99"></property>
<property name="money" value="6.4"></property>
<!-- 对List集合类型进行注入 -->
<property name="list">
<list>
<value>qq</value>
<value>pp</value>
<value>qp</value>
</list>
</property>
<!-- 对Set集合类型进行注入 -->
<property name="set">
<set>
<value>哈哈哈哈</value>
<value>呵呵呵呵</value>
</set>
</property>
<!-- 对Map集合类型进行注入 -->
<property name="map">
<map>
<entry>
<!-- 指定k -->
<key>
<value>k1</value>
</key>
<!-- 指定value -->
<value>v1</value>
</entry>
<entry>
<!-- 指定k -->
<key>
<value>k2</value>
</key>
<!-- 指定value -->
<value>v2</value>
</entry>
<entry key="k3" value="v3"></entry>
</map>
</property>
<!-- 对数组进行注入 -->
<property name="array">
<list>
<value>arr</value>
<value>arrr</value>
</list>
</property>
<!-- 对对象类型进行注入 -->
<property name="user" ref="u"></property>
<!-- 注入空字符串 -->
<property name="empty">
<value></value>
</property>
<!-- 注入null值 -->
<property name="nulll">
<null/>
</property>
</bean>
<bean id="u" class="cn.lexed.pojo.User">
<property name="username" value="7758"></property>
<property name="password" value="2222"></property>
</bean>
</beans>
五、结果
test
建立test测试类
package cn.lexed.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.lexed.pojo.Person;
public class TestSpring2 {
@Test
public void test() {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Person p=(Person)ac.getBean("per");
System.out.println(p);
}
}
运行结果
显示赋值成功