<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 1、set方法注入值(依赖),层层分离 -->
<!-- Dao层 --> <!-- 无参构造器用:property 有参构造器用:constructor-arg -->
<bean id="userDao1" class="com.bw.dao.UserDao">
<property name="name" value="Huang Long"></property>
</bean>
<!-- service层 -->
<bean id="userService1" class="com.bw.service.UserService">
<property name="userDao" ref="userDao1"></property>
</bean>
<!-- Action层、多例-->
<bean id="userAction1" class="com.bw.action.UserAction" scope="prototype">
<property name="userService" ref="userService1"></property>
</bean>
<!-- ========================================================================== --> <!-- 2、set方法注入值(依赖),层层嵌套 -->
<bean id="userAction2" class="com.bw.action.UserAction">
<property name="userService">
<bean class="com.bw.service.UserService">
<property name="userDao">
<bean class="com.bw.dao.UserDao">
<property name="name"><value>Huang Long</value></property>
</bean>
</property>
</bean>
</property>
</bean>
<!-- ========================================================================== --> <!-- 3、p名称空间 ,头部要引入xmlns:p="http://www.springframework.org/schema/p"-->
<!-- Dao层 -->
<bean id="userDao3" class="com.bw.dao.UserDao">
<property name="name" value="Huang Long"></property>
</bean>
<!-- service层 -->
<bean id="userService3" class="com.bw.service.UserService" p:userDao-ref="userDao3"></bean>
<!-- Action层、多例-->
<bean id="userAction3" class="com.bw.action.UserAction" p:userService-ref="userService3" scope="prototype"></bean>
</beans>

下面两种方式,不建议使用:

1、自动装配

 <?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- default-autowire="byName" 可以配置全局自动装配 --> <!-- 4、自动装配(局部) --> <!-- 自动装配方式,不建议使用,1、增加服务器负担;2、大项目不好维护 -->
<!-- Dao层 -->
<bean id="userDao" class="com.bw.dao.UserDao">
<property name="name" value="Huang Long"></property>
</bean> <!-- service层 -->
<bean id="userService" class="com.bw.service.UserService" autowire="byName"></bean> <!-- Action层、多例-->
<bean id="userAction4" class="com.bw.action.UserAction" autowire="byName" scope="prototype"></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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd" default-autowire="byName">
<!-- default-autowire="byName" 可以配置全局自动装配 --> <!-- 4、自动装配(全局) --> <!-- 自动装配方式,不建议使用,1、增加服务器负担;2、大项目不好维护 -->
<!-- Dao层 -->
<bean id="userDao" class="com.bw.dao.UserDao">
<property name="name" value="Huang Long"></property>
</bean> <!-- service层 -->
<bean id="userService" class="com.bw.service.UserService" ></bean> <!-- Action层、多例-->
<bean id="userAction4" class="com.bw.action.UserAction" scope="prototype"></bean>
</beans>

2、注解  (省略)

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/11704218.html

欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

Java进阶知识18 Spring对象依赖关系的几种写法的更多相关文章

  1. Java进阶知识16 Spring创建IOC容器的两种方式

    1.直接得到 IOC 容器对象 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app ...

  2. Java进阶知识15 Spring的基础配置详解

    1.SSH各个的职责 Struts2:是web框架(管理jsp.action.actionform等).Hibernate:是ORM框架,处于持久层.Spring:是一个容器框架,用于配置bean,并 ...

  3. Spring对象依赖关系处理

    Spring中给对象属性赋值 1.通过set方法给属性注入值 2.p名称空间 3.自动装配 4.注解 编写MVCModel调用userAction MVCModel public class MVCM ...

  4. Java进阶知识17 Spring Bean对象的创建细节和创建方式

    本文知识点(目录): 1.创建细节         1) 对象创建: 单例/多例         2) 什么时候创建?         3)是否延迟创建(懒加载)         4) 创建对象之后, ...

  5. Java进阶知识23 Spring对JDBC的支持

    1.最主要的代码 Spring 配置文件(beans.xml) <!-- 连接池 --> <bean id="dataSource" class="co ...

  6. Java进阶知识25 Spring与Hibernate整合到一起

    1.概述 1.1.Spring与Hibernate整合关键点 1) Hibernate的SessionFactory对象交给Spring创建.    2) hibernate事务交给spring的声明 ...

  7. Java进阶知识20 Spring的代理模式

    本文知识点(目录): 1.概念  2.代理模式      2.1.静态代理      2.2.动态代理      2.3.Cglib子类代理 1.概念 1.工厂模式  2. 单例模式 代理(Proxy ...

  8. Spring对象依赖关系

    Spring中,如何给对象的属性赋值?  [DI, 依赖注入] 1) 通过构造函数 2) 通过set方法给属性注入值 3) p名称空间   4)自动装配(了解) 5) 注解 package loade ...

  9. Java进阶知识24 Spring的事务管理(事务回滚)

    1.事务控制概述   1.1.编程式事务控制         自己手动控制事务,就叫做编程式事务控制.         Jdbc代码: connection.setAutoCommit(false); ...

随机推荐

  1. (十五)Hibernate中的多表操作(5):双向多对多

    Hibernate的双向关联. 对象之间可以相互读取.        双向只针对读取的操作.对于增.删除.改的操作没有任何影响. 案例 : 实现双向多对多 MenuBean.java package ...

  2. 加密算法 MD5 和 SHA 的 JAVA 实现

    首先先简单的介绍一下MD5 和 SHA 算法 然后看一下在  java.security.MessageDigest   (信息摘要包下) 如何分别实现  md5 加密 和 sha 加密 最后在看一下 ...

  3. Eclipse 反编译工具 jad

    ** 1 下载 jad工具 ** 2 将.exe文件放在jdk安装路径下,里面有java ,javac 等命令,然后将jad.jar放在eclipse的dropins目录下 ** 3 启动eclips ...

  4. Snort Inline IPS Mode

    Snort Inline IPS Mode https://forum.netgate.com/topic/143812/snort-package-4-0-inline-ips-mode-intro ...

  5. springboot启动流程(一)构造SpringApplication实例对象

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 启动入口 本文是springboot启动流程的第一篇,涉及的内容是SpringApplicat ...

  6. 整屏纵向切换超出整屏内容纵向滚动 解决办法-fullpage

    这个问题我也是研究了好久,百度了很多办法,swiper我始终没有找到合适的解决办法,所以我放弃了swiper,改用fullpage. fullpage里面有个scrollOverflow的属性: 并且 ...

  7. vue语法概要二

    函数 用途 类别 v-html 用于输出html格式的数据 输出 v-bing 用于输出值 输出 v-model 双向绑定 输入和输出 v-if 逻辑判断 分支语句 v-else 同上 分支语句 v- ...

  8. process exporter 配置项解释

    process exporter在prometheus中用于监控进程,通过process exporter,可从宏观角度监控应用的运行状态(譬如监控redis.mysql的进程资源等) 配置文件样例如 ...

  9. AWD模式搅屎模式

    AWD模式搅屎模式 ###0x01 出题思路 ####1:题目类型 1-出题人自己写的cms,为了恶心然后加个so. 2-常见或者不常见的cms. 3-一些框架漏洞,比如ph师傅挖的CI这种 #### ...

  10. 2.Vue调试工具vue-devtools的安装步骤和使用

    1.安装步骤: 打开谷歌浏览器设置 -->扩展程序 -->勾选开发者模式 --->加载已解压的扩展程序 --->选择“chrome扩展”文件夹即可: