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); ...
随机推荐
- NavigatorOnLine.onLine——判断设备是否可以上网
概述:返回浏览器的联网状态.正常联网(在线)返回true,不正常联网(离线)返回false.一旦浏览器的联网状态发生改变,该属性值也会随之变化. 1.语法 let online = window.na ...
- 基因组所三代单分子测序PacBio完成技术升级—超长读长助力基因组学研究
基因组所三代单分子测序PacBio完成技术升级—超长读长助力基因组学研究 2015-09-23 | 作者:所级中心基因组平台 张兵 [关闭] 近日,基因组所所级中心基因组平台三代单分子实时测序PacB ...
- [JZOJ5897]密匙--哈希骚操作
[JZOJ5897]密匙--哈希骚操作 题目链接 太懒了自行Google 前置技能 二分/倍增求LCP e.g TJOI2017DNA 分析 这题看了样例解释才知道什么意思 本以为自己身为mo法师蛤希 ...
- Python练习_Python初识_day1
题目 1.作业 1.简述变量命名规范 2.name = input(“>>>”) name变量是什么数据类型? 3.if条件语句的基本结构? 4.用print打印出下面内容: ⽂能提 ...
- Opencl 学习笔记
1. HelloWorld
- Hibernate-validate工具类,手动调用校验返回结果
引言:在常见的工程中,一般是在Controller中校验入参,校验入参的方式有多种,这里介绍的使用hibernate-validate来验证,其中分为手动和自动校验,自动校验可以联合spring,使用 ...
- 如何在Web应用里消费SAP Leonardo的机器学习API
去年5月的时候,Jerry曾经写了一篇文章:使用Java程序消费SAP Leonardo的机器学习API,而最近另外做的一个项目,需要在Web应用里做同样的事情. 因为有了前一篇文章的铺垫,避免了很多 ...
- MyBatis-Spring 学习笔记一 SqlSessionFactoryBean以及映射器类
MyBatis-Spring 是一个用来整合 MyBatis 和 Spring 框架的小类库,通过这个jar包可以将 MyBatis 代码地整合到 Spring 中. 使用这个类库中的类, Sprin ...
- 一组简单好看的css3渐变按钮
主要代码如下: body { background:#fff } /* Mixins */ /* bg shortcodes */ .bg-gradient1 span,.bg-gradient1:b ...
- springboot系列(三) 启动类中关键注解作用解析
一.Springboot:请求入口 @SpringBootApplication @EnableAspectJAutoProxy @EnableScheduling @EnableTransactio ...