Java乔晓松-spring自动装配Bean的4种方式
spring自动装配Bean有4种方式:
第一种:autowire="default"即no
[html]
<?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="dept" class="www.csdn.spring.constructor.bean.Dept">
<property name="name" value="教育部" />
</bean>
<bean id="computer" class="www.csdn.spring.constructor.bean.Computer">
<property name="name" value="联想B470" />
</bean>
<bean id="employee" class="www.csdn.spring.constructor.bean.Employee">
<property name="name" value="乔晓松" />
<property name="dept" ref="dept"/>
<property name="computer" ref="computer"/>
</bean>
</beans>
<?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="dept" class="www.csdn.spring.constructor.bean.Dept">
<property name="name" value="教育部" />
</bean>
<bean id="computer" class="www.csdn.spring.constructor.bean.Computer">
<property name="name" value="联想B470" />
</bean>
<bean id="employee" class="www.csdn.spring.constructor.bean.Employee">
<property name="name" value="乔晓松" />
<property name="dept" ref="dept"/>
<property name="computer" ref="computer"/>
</bean>
</beans>第二种:autowire="byName"即,根据属性的名称装载
[html]
<?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="dept" class="www.csdn.spring.constructor.bean.Dept">
<property name="name" value="教育部" />
</bean>
<bean id="computer" class="www.csdn.spring.constructor.bean.Computer">
<property name="name" value="联想B470" />
</bean>
<bean id="employee" class="www.csdn.spring.constructor.bean.Employee" autowire="byName">
<property name="name" value="乔晓松" />
</bean>
</beans>
<?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="dept" class="www.csdn.spring.constructor.bean.Dept">
<property name="name" value="教育部" />
</bean>
<bean id="computer" class="www.csdn.spring.constructor.bean.Computer">
<property name="name" value="联想B470" />
</bean>
<bean id="employee" class="www.csdn.spring.constructor.bean.Employee" autowire="byName">
<property name="name" value="乔晓松" />
</bean>
</beans>第三种:autowire="byType"即根据属性的类型进行装载Bean
[html]
<?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="dept" class="www.csdn.spring.constructor.bean.Dept">
<property name="name" value="教育部" />
</bean>
<bean id="computer" class="www.csdn.spring.constructor.bean.Computer">
<property name="name" value="联想B470" />
</bean>
<bean id="employee" class="www.csdn.spring.constructor.bean.Employee" autowire="byType">
<property name="name" value="乔晓松" />
</bean>
</beans>
<?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=&qu
补充:软件开发 , Java ,