一、p空间注入
名称空间(通俗一点):相同的属性不能再标签里出现两次,为了避免重复用名称空间在区分
1、实现步骤
(1)先引入一个名称空间
image.png(2)在bean的配置里加p:属性名="值"
image.png
2、代码
Person.java
package work.zhangdoudou.Property;
public class Person {
private String pname;
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
}
配置文件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-3.0.xsd">
<!-- p名称空间注入 -->
<bean id="person" class="work.zhangdoudou.Property.Person" p:pname="zhangsan"></bean>
</beans>
测试类 TestPerson.java
package work.zhangdoudou.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import work.zhangdoudou.Property.Person;;
public class TestPerson {
@Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person= (Person)context.getBean("person");
System.out.println(person.getPname());
}
}
3、运行结果
image.png二、复杂数据注入
1、数组类型注入
UserArray.java
package work.zhangdoudou.Property;
public class UserArray {
private String[] user;
public void setUser(String[] user) {
this.user = user;
}
public String[] getUser() {
return user;
}
}
配置文件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-3.0.xsd">
<!-- 注入数组 -->
<bean id="userArray" class="work.zhangdoudou.Property.UserArray">
<property name="user">
<list>
<value>zhangsan</value>
<value>lisi</value>
<value>wangwu</value>
</list>
</property>
</bean>
</beans>
测试类 TestArray .java
package work.zhangdoudou.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import work.zhangdoudou.Property.UserArray;
public class TestArray {
@Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserArray userArray= (UserArray)context.getBean("userArray");
String [] user=userArray.getUser();
for (int i = 0; i < user.length; i++) {
System.out.println(user[i]);
}
}
}
运行结果
2、list类型注入
UserList.java
package work.zhangdoudou.Property;
import java.util.List;
public class UserList {
private List<String> user;
public void setUser(List<String> user) {
this.user = user;
}
public List<String> getUser() {
return user;
}
}
配置文件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-3.0.xsd">
<!-- 注入list -->
<bean id="userList" class="work.zhangdoudou.Property.UserList">
<property name="user">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>
</bean>
</beans>
测试类TestList.java
package work.zhangdoudou.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import work.zhangdoudou.Property.UserList;
public class TestList {
@Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserList userList= (UserList)context.getBean("userList");
List<String> user=userList.getUser();
for (String username : user) {
System.out.println(username);
}
}
}
运行结果
3、map类型注入
UserMap.java
package work.zhangdoudou.Property;
import java.util.Map;
public class UserMap {
private Map<String, String> user;
public void setUser(Map<String, String> user) {
this.user = user;
}
public Map<String, String> getUser() {
return user;
}
}
配置文件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-3.0.xsd">
<!-- 注入map -->
<bean id="userMap" class="work.zhangdoudou.Property.UserMap">
<property name="user">
<map>
<entry key="姓名" value="张三"></entry>
<entry key="性别" value="男"></entry>
<entry key="年龄" value="100"></entry>
</map>
</property>
</bean>
</beans>
测试类TestMap.java
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import work.zhangdoudou.Property.UserMap;
public class TestMap {
@Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserMap userMap= (UserMap)context.getBean("userMap");
Map<String, String> user=userMap.getUser();
Set<String> key=user.keySet();
for (String k : key) {
System.out.println(k+":"+user.get(k));
}
}
}
运行结果
4、properties类型注入
UserProperties.java
package work.zhangdoudou.c;
import java.util.Properties;
public class UserProperties {
private Properties user;
public void setUser(Properties user) {
this.user = user;
}
public Properties getUser() {
return user;
}
}
配置文件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-3.0.xsd">
<!-- 注入 Properties -->
<bean id="userProperties" class="work.zhangdoudou.Property.UserProperties">
<property name="user">
<props>
<prop key="username">manman</prop>
<prop key="password">520</prop>
</props>
</property>
</bean>
</beans>
测试类TestProperties.java
package work.zhangdoudou.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import work.zhangdoudou.Property.UserProperties;
public class TestProperties {
@Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserProperties userProperties= (UserProperties)context.getBean("userProperties");
System.out.println(userProperties.getUser());
}
}
运行结果
网友评论