Spring框架总结(五)
名称 | 描述 |
---|---|
自动装配(了解) | 根据名称自动装配:autowire="byName" 自动去IOC容器中找与属性名同名的引用的对象,并自动注入 延续使用user、dao、service、action |
一、局部改变自动化注入方法,更改bean.xml(根据名字自动加载)
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="
6 http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context.xsd">
10
11
12 <!-- 对象属性赋值 通过构造函数 -->
13 <bean id="user" class="com.liuyang.auto.User">
14 </bean>
15 <!-- dao注入 -->
16 <bean id="dao" class="com.liuyang.auto.UserDAO">
17 </bean>
18
19 <!-- service注入 -->
20 <bean id="us" class="com.liuyang.auto.UserService" autowire="byType">
21 </bean>
22
23 <!-- action注入 ,,根据名称自动装配,userAction注入的属性,会去IOC容器中自动查找与属性同名的对象 -->
24 <bean id="userAction" class="com.liuyang.auto.UserAction"
25 autowire="byType">
26 </bean>
27 </beans>
- 全局更改,加了一个标签default-autowire="byName"
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="
6 http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName">
10
11
12 <!-- 对象属性赋值 通过构造函数 -->
13 <bean id="user" class="com.liuyang.auto.User">
14 </bean>
15 <!-- dao注入 -->
16 <bean id="dao" class="com.liuyang.auto.UserDAO">
17 </bean>
18
19 <!-- service注入 -->
20 <bean id="us" class="com.liuyang.auto.UserService">
21 </bean>
22
23 <!-- action注入 ,,根据名称自动装配,userAction注入的属性,会去IOC容器中自动查找与属性同名的对象 -->
24
25 <bean id="userAction" class="com.liuyang.auto.UserAction"
26 autowire="byType">
27 </bean>
28 </beans>
二、根据类型自动加载
- 全局加default-autowire="byType",必须确保改类型在IOC容器中只有一个对象;否则报错
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="
6 http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context.xsd">
10
11
12 <!-- 对象属性赋值 通过构造函数 -->
13 <bean id="user" class="com.liuyang.auto.User">
14 </bean>
15 <!-- dao注入 -->
16 <bean id="dao" class="com.liuyang.auto.UserDAO">
17 </bean>
18
19 <!-- service注入 -->
20 <bean id="us" class="com.liuyang.auto.UserService" autowire="byType">
21 </bean>
22
23 <!-- action注入 ,,根据名称自动装配,userAction注入的属性,会去IOC容器中自动查找与属性同名的对象 -->
24 <bean id="userAction" class="com.liuyang.auto.UserAction"
25 autowire="byType">
26 </bean>
27 </beans>
总结:
- Spring提供的自动装配主要是为了简化配置; 但是不利于后期的维护。(一般不推荐使用)
十年磨一剑,一剑破万法