Java进阶知识19 Spring对象依赖关系的几种写法
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" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:p="http://www.springframework.org/schema/p" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop.xsd"> 14 15 <!-- 1、set方法注入值(依赖),层层分离 --> 16 <!-- Dao层 --> <!-- 无参构造器用:property 有参构造器用:constructor-arg --> 17 <bean id="userDao1" class="com.bw.dao.UserDao"> 18 <property name="name" value="Huang Long"></property> 19 </bean> 20 <!-- service层 --> 21 <bean id="userService1" class="com.bw.service.UserService"> 22 <property name="userDao" ref="userDao1"></property> 23 </bean> 24 <!-- Action层、多例--> 25 <bean id="userAction1" class="com.bw.action.UserAction" scope="prototype"> 26 <property name="userService" ref="userService1"></property> 27 </bean> 28 <!-- ========================================================================== --> 29 30 <!-- 2、set方法注入值(依赖),层层嵌套 --> 31 <bean id="userAction2" class="com.bw.action.UserAction"> 32 <property name="userService"> 33 <bean class="com.bw.service.UserService"> 34 <property name="userDao"> 35 <bean class="com.bw.dao.UserDao"> 36 <property name="name"><value>Huang Long</value></property> 37 </bean> 38 </property> 39 </bean> 40 </property> 41 </bean> 42 <!-- ========================================================================== --> 43 44 <!-- 3、p名称空间 ,头部要引入xmlns:p="http://www.springframework.org/schema/p"--> 45 <!-- Dao层 --> 46 <bean id="userDao3" class="com.bw.dao.UserDao"> 47 <property name="name" value="Huang Long"></property> 48 </bean> 49 <!-- service层 --> 50 <bean id="userService3" class="com.bw.service.UserService" p:userDao-ref="userDao3"></bean> 51 <!-- Action层、多例--> 52 <bean id="userAction3" class="com.bw.action.UserAction" p:userService-ref="userService3" scope="prototype"></bean> 53 </beans>
下面两种方式,不建议使用:
1、自动装配
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" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/tx 10 http://www.springframework.org/schema/tx/spring-tx.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd"> 13 <!-- default-autowire="byName" 可以配置全局自动装配 --> 14 15 <!-- 4、自动装配(局部) --> <!-- 自动装配方式,不建议使用,1、增加服务器负担;2、大项目不好维护 --> 16 <!-- Dao层 --> 17 <bean id="userDao" class="com.bw.dao.UserDao"> 18 <property name="name" value="Huang Long"></property> 19 </bean> 20 21 <!-- service层 --> 22 <bean id="userService" class="com.bw.service.UserService" autowire="byName"></bean> 23 24 <!-- Action层、多例--> 25 <bean id="userAction4" class="com.bw.action.UserAction" autowire="byName" scope="prototype"></bean> 26 </beans>
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" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/tx 10 http://www.springframework.org/schema/tx/spring-tx.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd" default-autowire="byName"> 13 <!-- default-autowire="byName" 可以配置全局自动装配 --> 14 15 <!-- 4、自动装配(全局) --> <!-- 自动装配方式,不建议使用,1、增加服务器负担;2、大项目不好维护 --> 16 <!-- Dao层 --> 17 <bean id="userDao" class="com.bw.dao.UserDao"> 18 <property name="name" value="Huang Long"></property> 19 </bean> 20 21 <!-- service层 --> 22 <bean id="userService" class="com.bw.service.UserService" ></bean> 23 24 <!-- Action层、多例--> 25 <bean id="userAction4" class="com.bw.action.UserAction" scope="prototype"></bean> 26 </beans>
2、注解 (省略)
原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/11704218.html 欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |