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); ...
随机推荐
- Python之字符与编码笔记
概述 类型 str 字符串 bytes 字节 bytearray 字节数组 字符串编码架构 字符集:赋值一个编码到某个字符,以便在内存中表示 编码 Ecoding:转换字符到原始字节形式 解码 Dec ...
- sqlserver时间戳
SELECT DATEADD(S,1576464113 + 8 * 3600,'1970-01-01 00:00:00') --时间戳转换成普通时间 SELECT DATEDIFF(S,'1970-0 ...
- 分布式服务追踪与调用链 Zikpin
分布式服务追踪与调用链系统产生的背景 在为服务中,如果服务与服务之间的依赖关系非常复杂,如果某个服务出现了一些问题,很难追查到原因,特别是服务与服务之间调用的时候. 在微服务系统中,随着业务的发展,系 ...
- Jar包下载 开源网站 模板下载
在日常的java学习和开发中,总是遇到各种jar包下载,但是CSDN这种坑爹网站,各位码农们都想挣点C币,一个开源的免费的东西就这么变了味,我这里收集 了一些好用的工具,日常开发中需要用的请自取,毕竟 ...
- mac安装composer
推荐阅读:https://www.jianshu.com/p/edde14a67b1a 自己实际操作: 下载 composer https://getcomposer.org/download/ 下载 ...
- Vue路由嵌套
Vue路由嵌套 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- tornado 常见问题处理
1 怎么获取从页面中的传值 使用 self.get_body_argument tornado的参数存储在self.request.body内,通过json以后就可以直接取值,当初我在前端使用angu ...
- init system
参考:5 Best Modern Linux ‘init’ Systems (1992-2015) 参考:Linux开机流程 参考:<鸟哥的 Linux 私房菜:基础学习篇 第四版>第十七 ...
- 深入理解Kubernetes资源限制:CPU
写在前面 在上一篇关于Kubernetes资源限制的文章我们讨论了如何通过ResourceRequirements设置Pod中容器内存限制,以及容器运行时是如何利用Linux Cgroups实现这些限 ...
- 最近公共祖先算法LCA笔记(树上倍增法)
Update: 2019.7.15更新 万分感谢[宁信]大佬,认认真真地审核了本文章,指出了超过五处错误捂脸,太尴尬了. 万分感谢[宁信]大佬,认认真真地审核了本文章,指出了超过五处错误捂脸,太尴尬了 ...