Spring - XML方式p名称空间方法注入 & 模块化配置(06)

概述

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]]
     * */
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值