Java进阶知识18 Spring对象依赖关系的几种写法
<?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对象依赖关系的几种写法的更多相关文章
- Java进阶知识16 Spring创建IOC容器的两种方式
1.直接得到 IOC 容器对象 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app ...
- Java进阶知识15 Spring的基础配置详解
1.SSH各个的职责 Struts2:是web框架(管理jsp.action.actionform等).Hibernate:是ORM框架,处于持久层.Spring:是一个容器框架,用于配置bean,并 ...
- Spring对象依赖关系处理
Spring中给对象属性赋值 1.通过set方法给属性注入值 2.p名称空间 3.自动装配 4.注解 编写MVCModel调用userAction MVCModel public class MVCM ...
- Java进阶知识17 Spring Bean对象的创建细节和创建方式
本文知识点(目录): 1.创建细节 1) 对象创建: 单例/多例 2) 什么时候创建? 3)是否延迟创建(懒加载) 4) 创建对象之后, ...
- Java进阶知识23 Spring对JDBC的支持
1.最主要的代码 Spring 配置文件(beans.xml) <!-- 连接池 --> <bean id="dataSource" class="co ...
- Java进阶知识25 Spring与Hibernate整合到一起
1.概述 1.1.Spring与Hibernate整合关键点 1) Hibernate的SessionFactory对象交给Spring创建. 2) hibernate事务交给spring的声明 ...
- Java进阶知识20 Spring的代理模式
本文知识点(目录): 1.概念 2.代理模式 2.1.静态代理 2.2.动态代理 2.3.Cglib子类代理 1.概念 1.工厂模式 2. 单例模式 代理(Proxy ...
- Spring对象依赖关系
Spring中,如何给对象的属性赋值? [DI, 依赖注入] 1) 通过构造函数 2) 通过set方法给属性注入值 3) p名称空间 4)自动装配(了解) 5) 注解 package loade ...
- Java进阶知识24 Spring的事务管理(事务回滚)
1.事务控制概述 1.1.编程式事务控制 自己手动控制事务,就叫做编程式事务控制. Jdbc代码: connection.setAutoCommit(false); ...
随机推荐
- diy操作系统 附录:gcc栈帧开启与关闭
在gcc命令行参数中可以使用-fno-omit-frame-pointer来开启栈帧的使用,或者使用-fomit-frame-pointer选项来关闭. 然而,也可以针对某一个函数进行配置方法如下,这 ...
- STM32与ARM代码执行过程
内存分配 1.ARM(JZ2440) 启动方式: 1)nor启动 注:1.bootloader烧在norflash的0地址 2.将bootloader从norflash中复制到SDRAM中的链接地址( ...
- asp.net core-10.Http请求的处理过程
左边是一个普通的网页请求过程,右边是.net core请求过程 这是大致请求流程图:
- Jmeter5.1——聚合报告参数分析
Jmeter5.1——聚合报告参数分析 Label: 每个JMeter的element的Name值.例如HTTP Request的Name. Samples:发出请求的数量.如果线程组中配置的是线程数 ...
- Element-ui-Basic
一.Layout 布局 1.基础布局 <el-row> <el-col :span="24"><div class="grid-conten ...
- 表格中的DOM
通过DOM来操作table跟在html中操作table是不一样的,下面来看看怎样通过DOM来操作table. 按照table的分布来创建: <table> <thead> &l ...
- MVC框架+vue+elementUI
用自动化构建做的vue项目,因为是动态加载数据,在SEO优化时一直不如意,于是我们换了框架,用MVC框架,做成静态页面,但是原来的代码都是用vue和elementUI,为了快速的复用原来的代码,于是在 ...
- Python实现MATLAB中的 bwlabel函数
最近做验证码识别,原本用MATLAB已经实现的整个识别模型,不过代码要部署在Linux服务器上还是需要用另外的语言实现,于是决定用Python + OpenCV来实现. bwlabel函数的作用是检测 ...
- openstack安装部署——计算服务(控制节点&计算节点)前言
1.前言Openstack计算服务通过认证服务获取认证:通过镜像服务获取镜像:通过仪表盘提供的用户界面与用户交互.镜像的存取受工程和用户的限制,配额受工程的限制(例如不同工程允许虚拟机实例数量不同). ...
- inotify+rsync文件实时同步
原文转自http://dl528888.blog.51cto.com/2382721/771533/ 之前做了“ssh信任与scp自动传输脚本”的技术文档,此方案是作为公司里备份的方法,但在实际的运行 ...