自动装配(了解)

根据名称自动装配:autowire="byName"

自动去IOC容器中找与属性名同名的引用的对象,并自动注入

延续使用user、dao、service、action

一、局部改变自动化注入方法,更改bean.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"> <!-- 对象属性赋值 通过构造函数 -->
<bean id="user" class="com.liuyang.auto.User">
</bean>
<!-- dao注入 -->
<bean id="dao" class="com.liuyang.auto.UserDAO">
</bean> <!-- service注入 -->
<bean id="us" class="com.liuyang.auto.UserService" autowire="byType">
</bean> <!-- action注入 ,,根据名称自动装配,userAction注入的属性,会去IOC容器中自动查找与属性同名的对象 -->
<bean id="userAction" class="com.liuyang.auto.UserAction"
autowire="byType">
</bean>
</beans>

全局更改,加了一个标签default-autowire="byName"

 <?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" default-autowire="byName"> <!-- 对象属性赋值 通过构造函数 -->
<bean id="user" class="com.liuyang.auto.User">
</bean>
<!-- dao注入 -->
<bean id="dao" class="com.liuyang.auto.UserDAO">
</bean> <!-- service注入 -->
<bean id="us" class="com.liuyang.auto.UserService">
</bean> <!-- action注入 ,,根据名称自动装配,userAction注入的属性,会去IOC容器中自动查找与属性同名的对象 --> <bean id="userAction" class="com.liuyang.auto.UserAction"
autowire="byType">
</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" 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"> <!-- 对象属性赋值 通过构造函数 -->
<bean id="user" class="com.liuyang.auto.User">
</bean>
<!-- dao注入 -->
<bean id="dao" class="com.liuyang.auto.UserDAO">
</bean> <!-- service注入 -->
<bean id="us" class="com.liuyang.auto.UserService" autowire="byType">
</bean> <!-- action注入 ,,根据名称自动装配,userAction注入的属性,会去IOC容器中自动查找与属性同名的对象 -->
<bean id="userAction" class="com.liuyang.auto.UserAction"
autowire="byType">
</bean>
</beans>

全局加default-autowire="byType"

必须确保改类型在IOC容器中只有一个对象;否则报错

总结:

        Spring提供的自动装配主要是为了简化配置; 但是不利于后期的维护。(一般不推荐使用)

05-15 15:04