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); ...
随机推荐
- c++学习(二)------this指针学习
在c++中,类的不同实例有自己的数据(储存在不同地方),有很多拷贝.而类的成员函数却只有一份备份. 而不同的类的实例却可以调用同一个函数,这是通过this指针来完成的. *this代表当前类本身,th ...
- 17-MySQL DBA笔记-应用程序调优
第17章 应用程序调优 本章将主要讲述应用程序调优的一些方法和步骤,应用程序调优的领域很广,本章主要关注的是涉及数据库方面的调优. 在进行性能分析之前,我们先要熟悉应用的角色,它是什么版本的,做什么的 ...
- (九)springmvc之json的数据请求(客户端发送json数据到服务端)
index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...
- Java内存 模型理解
概述 在正式讲Java内存模型之前,我们先了解一些物理计算机并发问题,然后一点点的引出Java内存模型的由来. 多任务处理在现在计算机操作系统中几乎是一项必备的功能.这不单是因为计算机计算能力强大,更 ...
- IIs发布的项目无法打开问题
1/查看一下ISAPI筛选器,是否存在2.0,4.0,若缺少东西,就从新安装一下iis,存在某些程序没有被勾选,一般是asp.net3.5,asp.net4.0
- C# 关于爬取网站数据遇到csrf-token的分析与解决
需求 某航空公司物流单信息查询,是一个post请求.通过后台模拟POST HTTP请求发现无法获取页面数据,通过查看航空公司网站后,发现网站使用避免CSRF攻击机制,直接发挥40X错误. 关于CSRF ...
- SQL递归查询(with as)
SQL递归查询(with cte as) with cte as( select Id,Pid,DeptName,0 as lvl from Department where Id = 2 ...
- 【推荐】 Neutralizer 安卓上特殊的均衡器
首先 直切正题 这个均衡器特殊就特殊在 会产生 特定频率的声音 根据声音来调整 自己喜欢的声音 下载地址: https://d-02.apkplz.org/dl.php?s=czlDeEt ...
- MMU简介
MMU(Memory Management Unit)内存管理单元 负责虚拟地址到物理地址的映射,并提供硬件机制的内存访问权限检查.内存访问权限的检查可以保护每个进程所用的内存不会被其他进程所破坏 地 ...
- Echarts如何添加鼠标点击事件?防止重复触发点击事件
Echarts如何添加鼠标点击事件? 1.通常我们只使用了以下代码,通过配置项和数据显示图表. var myChart = echarts.init(document.getElementById(' ...