概述
p名称空间注入是为了简化之前的配置而存在的。在之前的两种注入中,如果有十个属性,就得写十个<constructor-arg id=""></constructor-arg>
或者<property id=""></property>
。但是对于一些简单的基本数据类型或者引用数据类型,写那么多配置得不偿失,所以Spring在2.5版本的时候加入了p名称空间
注入。
引入p名称空间
之所以叫做p名称空间
是因为在约束中需要引入p名称空间
约束。 在Spring配置文件的根元素中加入xmlns:p="http://www.springframework.org/schema/p"
。同时p名称空间
也需要setters方法。
注入基本类型
public class UserDaoImpl implements UserDao{
private String str;
public void setStr(String str) {
this.str = str;
}
public String toString() {
return "UserDaoImpl [str=" + str + "]";
}
public void save() { }
}
<bean id="userDao" p:str="hello" class="com.spring.firstday.UserDaoImpl"></bean>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao ud = (UserDao)ac.getBean("userDao");
System.out.println(ud);
}
/*Console:
* UserDaoImpl [str=hello]
* */
}
注入引用类型
public class UserDaoImpl implements UserDao{
private String str;
public void setStr(String str) {
this.str = str;
}
public String toString() {
return "UserDaoImpl [str=" + str + "]";
}
public void save() { }
}
public class UserServiceImpl implements UserService{
private UserDao ud;
public void setUd(UserDao ud) {
this.ud = ud;
}
public String toString() {
return "UserServiceImpl [ud=" + ud + "]";
}
public void save() { }
}
<bean id="userDao" p:str="hello" class="com.spring.firstday.UserDaoImpl"></bean>
<bean id="userService" class="com.spring.firstday.UserServiceImpl" p:ud-ref="userDao"></bean>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService)ac.getBean("userService");
System.out.println(us);
}
/*Console:
* UserServiceImpl [ud=UserDaoImpl [str=hello]]
* */
}
模块化配置
模块化配置指的是把配置文件拆开,不同的配置文件保存不同的信息。可以有两种方法,但一般使用后者。
- 加载多个配置文件
public class UserDaoImpl implements UserDao{
private String str;
public void setStr(String str) {
this.str = str;
}
public String toString() {
return "UserDaoImpl [str=" + str + "]";
}
public void save() { }
}
public class UserServiceImpl implements UserService{
private UserDao ud;
public void setUd(UserDao ud) {
this.ud = ud;
}
public String toString() {
return "UserServiceImpl [ud=" + ud + "]";
}
public void save() { }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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="userService" class="com.spring.firstday.UserServiceImpl" p:ud-ref="userDao"></bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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="userDao" p:str="hello" class="com.spring.firstday.UserDaoImpl"></bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml", "applicationContext2.xml");
UserService us = (UserService)ac.getBean("userService");
System.out.println(us);
}
/*Console:
* UserServiceImpl [ud=UserDaoImpl [str=hello]]
* */
}
- 在一个配置文件种导入其他的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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="userDao" p:str="hello" class="com.spring.firstday.UserDaoImpl"></bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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="userService" class="com.spring.firstday.UserServiceImpl" p:ud-ref="userDao"></bean>
<import resource="applicationContext2.xml"/>
</beans>
public class UserDaoImpl implements UserDao{
private String str;
public void setStr(String str) {
this.str = str;
}
public String toString() {
return "UserDaoImpl [str=" + str + "]";
}
public void save() { }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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="userService" class="com.spring.firstday.UserServiceImpl" p:ud-ref="userDao"></bean>
<import resource="applicationContext2.xml"/>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService)ac.getBean("userService");
System.out.println(us);
}
/*Console:
* UserServiceImpl [ud=UserDaoImpl [str=hello]]
* */
}