Spring框架学习记录之构造注入
其他
2018-06-26 11:13:32
阅读次数: 3
Spring框架学习记录之构造注入
概念(图片来源于网络):
Spring 框架为有参构造的参数赋值
<?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="dao.impl.UserDaoImpl"></bean> <bean id="userService" class="service.impl.UserServiceImpl"> <!--通过constructor-arg元素为构造方法里的参数赋值--> <constructor-arg> <ref bean="userDao"/> </constructor-arg> </bean> </beans> |
Spring框架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-3.2.xsd"> <!-- 使用p命名空间注入属性值 --> <bean id="user" class="entity.User" p:username="张三" p:age="23" p:email="[email protected]" /> <bean id="userDao" class="dao.impl.UserDaoImpl" /> <bean id="userService" class="service.impl.UserServiceImpl" p:dao-ref="userDao" /> </beans> |
Spring框架 各种数据类型的注入方式
<?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-3.2.xsd"> <bean id="entity" class="entity.TestEntity"> <!-- 使用<![CDATA[]]>标记处理XML特殊字符 --> <property name="specialCharacter1"> <value><![CDATA[P&G]]></value> </property> <!-- 把XML特殊字符替换为实体引用 --> <property name="specialCharacter2"> <value>P&G</value> </property> <!-- 定义内部Bean --> <property name="innerBean"> <bean class="entity.User"> <property name="username"> <value>Mr. Inner</value> </property> </bean> </property> <!-- 注入List类型 --> <property name="list"> <list> <!-- 定义List中的元素 --> <value>足球</value> <value>篮球</value> </list> </property> <!-- 注入数组类型 --> <property name="array"> <list> <!-- 定义数组中的元素 --> <value>足球</value> <value>篮球</value> </list> </property> <!-- 注入Set类型 --> <property name="set"> <set> <!-- 定义Set或数组中的元素 --> <value>足球</value> <value>篮球</value> </set> </property> <!-- 注入Map类型 --> <property name="map"> <map> <!-- 定义Map中的键值对 --> <entry> <key> <value>football</value> </key> <value>足球</value> </entry> <entry> <key> <value>basketball</value> </key> <value>篮球</value> </entry> </map> </property> <!-- 注入Properties类型 --> <property name="props"> <props> <!-- 定义Properties中的键值对 --> <prop key="football">足球</prop> <prop key="basketball">篮球</prop> </props> </property> <!-- 注入空字符串值 --> <property name="emptyValue"> <value></value> </property> <!-- 注入null值 --> <property name="nullValue"> <null/> </property> </bean> </beans>
|
转载自blog.csdn.net/qq_42056595/article/details/80792789