Spring注入(XML)
getBean 时可以通过class 也可以通过 id
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" //下面这行开启P命名空间 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" class="com.zhan.dao.impl.UserDaoImpl" scope="prototype" init-method="init" destroy-method="destory"></bean> --> <!-- //静态工厂注入 class的值不是写User对象的全路径 而是写静态工厂的全路径 factory-method的值写要调用的方法 <bean id="userDao" class="com.zhan.factory.StaticFactory" factory-method="getUserDao"></bean> --> <!-- //实例工厂注入 需要先创建DynamicFactory对象,再通过DynamicFactory对象进行调用 <bean id="dynamicFactory" class="com.zhan.factory.DynamicFactory"></bean> <bean id="userDao" factory-bean="dynamicFactory" factory-method="getUserDao"></bean> --> <!-- //属性对象注入 <bean id="userDao" class="com.zhan.dao.impl.UserDaoImpl"></bean> <bean id="userService" class="com.zhan.dao.impl.UserServiceImpl" p:userDao-ref="userDao"></bean> --> <bean id="user1" class="com.zhan.domain.User" p:addr="北京" p:name="leon"></bean> <bean id="user2" class="com.zhan.domain.User" p:addr="南京" p:name="jack"></bean> <bean id="userDao" class="com.zhan.dao.impl.UserDaoImpl"> //List属性注入 <property name="strList"> <list> <value>"a"</value> <value>"b"</value> <value>"c"</value> </list> </property> //Map属性注入 <property name="userMap"> <map> <entry key="u1" value-ref="user1"></entry> <entry key="u2" value-ref="user2"></entry> </map> </property> //Properties注入 <property name="properties"> <props> <prop key="p1">ppp1</prop> <prop key="p2">ppp2</prop> <prop key="p3">ppp3</prop> </props> </property> </bean> <bean id="userService" class="com.zhan.dao.impl.UserServiceImpl"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean> </beans>
Nice to see you all!