XML注入其他类型属性
字面量
什么是字面量?
基本数据类型,决定值的对象都是字面量
null值
<property name="address">
<null/>
</property>
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"> <!-- 配置对象的创建 --> <bean id="book" class="com.test.Book"> <!-- 使用property完成属性注入 name:类里面属性名称 value:向属性注入的值 --> <property name="name" value="第一本书"/> <property name="author" value="第一个作者"/> <!-- 向属性中设置空值 --> <property name="address"> <null/> </property> </bean> </beans>
book
package com.test; /** * 演示使用色图方法进行属性的注入 */ public class Book { private String name; private String author; private String address; public void setAuthor(String author) { this.author = author; } public void setName(String name) { this.name = name; } public void setAddress(String address) { this.address = address; } public void testDemo(){ System.out.println(name + "::" + author + "::" + address); } }
测试方法
package com.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.junit.Assert.*; public class BookTest { @Test public void testBook(){ //1.加载spring配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); //2.获取配置创建的对象 Book book = context.getBean("book", Book.class); System.out.println("book = " + book); book.testDemo(); } }
book = com.test.Book@43a0cee9
第一本书::第一个作者::null
属性值包含特殊符号
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"> <!-- 配置对象的创建 --> <bean id="book" class="com.test.Book"> <!-- 使用property完成属性注入 name:类里面属性名称 value:向属性注入的值 --> <property name="name" value="第一本书"/> <property name="author" value="第一个作者"/> <!-- 向属性中设置空值 --> <!--<property name="address"> <null/> </property>--> <!-- 属性值包含特殊符号 例如<南京>--> <!-- 第一种方式 转义 --> <!--<property name="address" value="<南京>"/>--> <!-- 第二种方式 把带特殊符号的内容写到CDATA中 --> <property name="address"> <value> <![CDATA[<南京>]]> </value> </property> </bean> </beans>
注入属性-注入外部bean
通过service层调用到层这样的过程就叫注入外部bean
第一步创建两个类service类和dao类
在service调用dao里面的方法
在spring配置文件中进行配置
service
package com.test.service; import com.test.dao.UserDao; public class UserService { /** * 创建UserDao的对象 * 原始方式 * private UserDao userDao = new UserDaoImpl(); */ //创建UserDao属性,生成set方法 private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add(){ System.out.println("add...."); userDao.update(); } }
dao
package com.test.dao; public interface UserDao { public void update(); }
daoImpl
package com.test.dao.impl; import com.test.dao.UserDao; public class UserDaoImpl implements UserDao { @Override public void update() { System.out.println("dao update"); } }
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"> <!-- service和dao对象创建 --> <bean id="userService" class="com.test.service.UserService"> <!-- 注入userDao对象 name:类里面属性名称 value:属性的值 ref: 创建userDao对象bean标签的id值 --> <property name="userDao" ref="userDao"/> </bean> <bean id="userDao" class="com.test.dao.impl.UserDaoImpl"/> </beans>
测试
package com.test.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserServiceTest { @Test public void add() { //1、加载spring配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); //获取配置创建的对象 UserService userService = context.getBean("userService", UserService.class); System.out.println("userService = " + userService); userService.add(); } }
userService = com.test.service.UserService@150c158
add....
dao update
注入属性-内部bean和级联赋值
一对多关系
部门和员工是典型的一对多关系,一个部门里面有多个员工,一个员工属于某一个部门,部门是一,员工是多
在实体类之间表示一对多关系
package com.test.entity; //部门类 public class Dept { private String name; public void setName(String name) { this.name = name; } @Override public String toString() { return "Dept{" + "name='" + name + '\'' + '}'; } }
package com.test.entity; //员工类 public class Emp { private String name; private String gender; //员工属于某一部门,使用对象形式表示 private Dept dept; public void setDept(Dept dept) { this.dept = dept; } public void setName(String name) { this.name = name; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Emp{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", dept=" + dept + '}'; } public void testEmp(){ this.toString(); } }
<?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 --> <bean id="emp" class="com.test.entity.Emp"> <!-- 设置两个普通的属性 --> <property name="name" value="张三"/> <property name="gender" value="男"/> <!-- 设置对象类型属性 --> <!-- 方法一 引入外部bean --> <!--<property name="dept" ref="dept"/>--> <!-- 方法二 内部bean --> <property name="dept"> <bean id="dept" class="com.test.entity.Dept"> <property name="name" value="研发部"/> </bean> </property> </bean> <!--<bean id="dept" class="com.test.entity.Dept"/>--> </beans>
package com.test.entity; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.junit.Assert.*; public class EmpTest { @Test public void testEmp() { //加载spring配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("spring1.xml"); Emp emp = context.getBean("emp", Emp.class); System.out.println("emp = " + emp); emp.testEmp(); } }
级联赋值
第一种写法
<?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 --> <bean id="emp" class="com.test.entity.Emp"> <!-- 设置两个普通的属性 --> <property name="name" value="张三"/> <property name="gender" value="男"/> <!-- 级联赋值 --> <property name="dept" ref="dept"/> </bean> <bean id="dept" class="com.test.entity.Dept"> <property name="name" value="研发部"/> </bean> </beans>
@Test public void testEm1() { //加载spring配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("spring2.xml"); Emp emp = context.getBean("emp", Emp.class); System.out.println("emp = " + emp); emp.testEmp(); }
第二种写法
<?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 --> <bean id="emp" class="com.test.entity.Emp"> <!-- 设置两个普通的属性 --> <property name="name" value="张三"/> <property name="gender" value="男"/> <!-- 级联赋值 要有dept的get方法 --> <property name="dept.name" value="研发部"/> </bean> <bean id="dept" class="com.test.entity.Dept"/> </beans>
package com.test.entity; //员工类 public class Emp { private String name; private String gender; //员工属于某一部门,使用对象形式表示 private Dept dept; public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public void setName(String name) { this.name = name; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Emp{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", dept=" + dept + '}'; } public void testEmp(){ this.toString(); } }
package com.test.entity; //部门类 public class Dept { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Dept{" + "name='" + name + '\'' + '}'; } }
xml注入集合属性
注入数组类型属性
注入List集合属性
注入Map集合类型属性
注入Set集合类型属性
package com.test1; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; public class Stu { //数组类型属性 private String[] course; //list集合类型属性 private List<String> list; //map集合类型属性 private Map<String,String> map; //set集合类型属性 private Set<String> set; public void setSet(Set<String> set) { this.set = set; } public void setList(List<String> list) { this.list = list; } public void setMap(Map<String, String> map) { this.map = map; } public void setCourse(String[] course) { this.course = course; } public void test(){ System.out.println(Arrays.toString(course)); System.out.println(list); System.out.println(map); System.out.println(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="stu" class="com.test1.Stu"> <!-- 数组类型属性注入 --> <property name="course"> <!-- 也可以用list --> <array> <value>Java</value> <value>Mysql</value> </array> </property> <!-- list 类型属性注入 --> <property name="list"> <list> <value>张三</value> <value>李四</value> </list> </property> <!-- map 类型属性注入 --> <property name="map"> <map> <entry key="第一个" value="第一个值"/> <entry key="第二个" value="第二个值"/> </map> </property> <!-- set类型属性注入 --> <property name="set"> <set> <value>set1</value> <value>set2</value> </set> </property> </bean> </beans>
package com.test1; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.junit.Assert.*; public class StuTest { @Test public void test1() { ApplicationContext context = new ClassPathXmlApplicationContext("spring4.xml"); Stu stu = context.getBean("stu", Stu.class); System.out.println(stu); stu.test(); } }
com.test1.Stu@768b970c
[Java, Mysql]
[张三, 李四]
{第一个=第一个值, 第二个=第二个值}
[set1, set2]
在集合里面设置对象类型值
<?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="stu" class="com.test1.Stu"> <!-- 数组类型属性注入 --> <property name="course"> <!-- 也可以用list --> <array> <value>Java</value> <value>Mysql</value> </array> </property> <!-- list 类型属性注入 --> <property name="list"> <list> <value>张三</value> <value>李四</value> </list> </property> <!-- map 类型属性注入 --> <property name="map"> <map> <entry key="第一个" value="第一个值"/> <entry key="第二个" value="第二个值"/> </map> </property> <!-- set类型属性注入 --> <property name="set"> <set> <value>set1</value> <value>set2</value> </set> </property> <!-- 注入list集合类型对象 值是对象 --> <property name="courseList"> <list> <ref bean="course1"/> <ref bean="course2"/> <ref bean="course3"/> </list> </property> </bean> <!-- 创建多个course对象 --> <bean id="course1" class="com.test1.Course"> <property name="cname" value="第一个"/> </bean> <bean id="course2" class="com.test1.Course"> <property name="cname" value="第2个"/> </bean> <bean id="course3" class="com.test1.Course"> <property name="cname" value="第3个"/> </bean> </beans>
把集合注入部分提取出来
在spring配置文件中引入名称空间util
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/beans/spring-util.xsd"> </beans>
使用util标签完成list集合注入提取
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/beans/spring-util.xsd"> <!-- 提取list集合类型属性注入 --> <util:list id="bookList"> <value>第一本书</value> <value>第2本书</value> <value>第3本书</value> </util:list> <!-- 提取list集合类型属性注入使用 --> <bean id="book" class="com.test1.Book"> <property name="list" ref="bookList"/> </bean> </beans>
package com.test1; import java.util.List; public class Book { private List<String> list; public void setList(List<String> list) { this.list = list; } public void test(){ System.out.println("list = " + list); } }