Spring IOC容器【p名稱空間注入屬性值 】

鬆門一枝花發表於2016-05-09

# p 名稱空間給物件的屬性注入值(spring3.0以上版本才支援)

需要引入:

xmlns:p="http://www.springframework.org/schema/p"


舉例 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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- ###############物件屬性賦值############### -->
	
	<!-- 
		給物件屬性注入值:
			# p 名稱空間給物件的屬性注入值
			 (spring3.0以上版本才支援)
	 -->
	 <bean id="userDao" class="c_property.UserDao"></bean>
	 
	 <bean id="userService" class="c_property.UserService" p:userDao-ref="userDao"></bean>
	 
	 <bean id="userAction" class="c_property.UserAction" p:userService-ref="userService"></bean>
	
	
	<!-- 傳統的注入: 
	 <bean id="user" class="c_property.User" >
	 	<property name="name" value="xxx"></property>
	 </bean>
	-->
	<!-- p名稱空間優化後 -->
	<bean id="user" class="c_property.User" p:name="Jack0001"></bean>
	 
</beans>   


相關文章