上一篇博客讲述了为什么使用spring依赖注入,怎么注入,详见
spring-bean依赖注入-01(等你来点击)


废话不多说,开始使用p命名空间进行set注入
使用另外一种注入方式是这样的(具体实现依赖注入-01),延续上一篇的注入配置的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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="userDao" class="com.hao.dao.impl.UserDaoImpl"/>
    <bean id="userService" class="com.hao.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
</beans>

使用p命名空间后

<?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="userDao" class="com.hao.dao.impl.UserDaoImpl"/>
    <bean id="userService" class="com.hao.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
    
</beans>

其他具体操作详见链接

posted on 2020-11-12 20:44  凸凸大军的一员  阅读(115)  评论(0编辑  收藏  举报