Spring中的各种注入方式

版权声明:如需转载,请注明出处! https://blog.csdn.net/qq_41172416/article/details/83691619

 1、设值注入

<?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"
    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">
        
        <beans>
        	
        	<bean id="user" class="com.bdqn.pojo.User">
        		<property name="userCode" value="bdqn"/>
        	</bean>
        	
         </beans>
        
</beans>

注意:属性值如果包含特殊字符 (“&” 、“<” 、“>” 、“、”) 

方式一:<!--- 使用 <![CDATA[]]> 标记处理XML特殊字符-->

方式二: <!--- 使用 XML特殊字符(&amp;)替换为实体引用-->

<?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"
    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">
        
        <beans>
        	<!--- 使用 <![CDATA[]]> 标记处理XML特殊字符-->
        	<bean id="user" class="com.bdqn.pojo.User">
        		<property name="userCode">
        			<value><![CDATA[P&G]]></value>
        		</property>
        	</bean>
                
                <!--- 使用 XML特殊字符替换为实体引用-->
        	<bean id="user" class="com.bdqn.pojo.User">
        		<property name="userCode">
        			<value>P&amp;G</value>
        		</property>
        	</bean>
        	
         </beans>
        
</beans>

  2、构造注入

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

 3、使用 p 命名空间实现属性注入

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

 语法:

p:属性名=“属性值”

p:属性名-ref="Bean 的 id"

<?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"
    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">
        
        <beans>
        	<bean id="user" class="com.bdqn.pojo.User"
        		p:userName="张三" p:userCode="zhangsan" 
                 />
        	
        </beans>
        
</beans>

猜你喜欢

转载自blog.csdn.net/qq_41172416/article/details/83691619