spring—依赖注入

依赖注入(Dependency Injection)

它是 Spring 框架核心 IOC 的具体实现。 在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是代码中不可能出现没有依赖的情况。
IOC 解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。 那这种业务层和持久层的依赖关系,在使用 Spring 之后,就让 Spring 来维护了。
简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取

Bean的依赖注入方式

①构造方法
创建有参构造

public class service {Dao dao;public service(Dao dao) {this.dao = dao;}public service() {}public void save(){dao.save();}public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");service i=(service) applicationContext.getBean("service");i.save();}
}```java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="impl" class="com.ImplDao" /><bean id="service" class="com.service"><constructor-arg name="dao" ref="impl"/></bean>
</beans>

②set方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="impl" class="com.ImplDao" /><bean id="service" class="com.service"><property name="dao" ref="impl"/></bean>
</beans>
public class service {Dao dao;public service(Dao dao) {this.dao = dao;}public service() {}public void save(){dao.save();}public void setDao(Dao dao) {this.dao = dao;}public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");service i=(service) applicationContext.getBean("service");i.save();}
}

set方法:P命名空间注入
P命名空间注入本质也是set方法注入,但比起上述的set方法注入更加方便,主要体现在配置文件中,如下: 首先,需要引入P命名空间:

  xmlns:p="http://www.springframework.org/schema/p"
 <bean id="impl" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"></bean>
</beans>

Bean的依赖注入的数据类型

(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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="impl" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/></bean>
</beans>
public class service {Dao dao;int no;String  name;public void setNo(int no) {this.no = no;}public void setName(String name) {this.name = name;}public service(Dao dao) {this.dao = dao;}public service() {}public void save(){dao.save();System.out.println(this.toString());}public void setDao(Dao dao) {this.dao = dao;}public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");service i=(service) applicationContext.getBean("service");i.save();}
}

(2)集合数据类型(List)的注入

<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="impl" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/><property name="list"><list><value>aaa</value><value>aadasa</value><value>sada</value></list></property></bean>
</beans>

(4)集合数据类型( Map<String,ImplDao> )的注入

<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="impl" class="com.ImplDao" /><bean id="impl2" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/><property name="list"><list><value>aaa</value><value>aadasa</value><value>sada</value></list></property><property name="map"><map><entry key="i1" value-ref="impl"/><entry key="i2" value-ref="impl2"/></map></property></bean>
</beans>

(5)集合数据类型(Properties)的注入

<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="impl" class="com.ImplDao" /><bean id="impl2" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/><property name="list"><list><value>aaa</value><value>aadasa</value><value>sada</value></list></property><property name="map"><map><entry key="i1" value-ref="impl"/><entry key="i2" value-ref="impl2"/></map></property><property name="properties"><props><prop key="aa">11</prop><prop key="bb">22</prop></props></property></bean>
</beans>

引入其他配置文件(分模块开发)

实际开发中,Spring的配置内容非常多,这就导致Spring配置很繁杂且体积很大,所以,可以将部分配置拆解到其 他配置文件中,而在Spring主配置文件通过import标签进行加载

    <import resource="applicationContext-dao.xml" />

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391966.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C# (类型、对象、线程栈和托管堆)在运行时的相互关系

在介绍运行时的关系之前,先从一些计算机基础只是入手,如下图: 该图展示了已加载CLR的一个windows进程,该进程可能有多个线程,线程创建时会分配到1MB的栈空间.栈空间用于向方法传递实参,方法定义的局部变量也在实参上,上图的右侧展示了线程的栈内存,栈从高位内存地址向地位内存地…

2019-08-01 纪中NOIP模拟赛B组

T1 [JZOJ2642] 游戏 题目描述 Alice和Bob在玩一个游戏&#xff0c;游戏是在一个N*N的矩阵上进行的&#xff0c;每个格子上都有一个正整数。当轮到Alice/Bob时&#xff0c;他/她可以选择最后一列或最后一行&#xff0c;并将其删除&#xff0c;但必须保证选择的这一行或这一列所有…

knn分类 knn_关于KNN的快速小课程

knn分类 knnAs the title says, here is a quick little lesson on how to construct a simple KNN model in SciKit-Learn. I will be using this dataset. It contains information on students’ academic performance.就像标题中所说的&#xff0c;这是关于如何在SciKit-Le…

spring—配置数据源

数据源&#xff08;连接池&#xff09;的作用 数据源(连接池)是提高程序性能如出现的 事先实例化数据源&#xff0c;初始化部分连接资源 使用连接资源时从数据源中获取 使用完毕后将连接资源归还给数据源 常见的数据源(连接池)&#xff1a;DBCP、C3P0、BoneCP、Druid等 开发步…

大型网站系统与Java中间件实践pdf

下载地址&#xff1a;网盘下载 基本介绍 编辑内容简介 到底是本什么书&#xff0c;拥有这样一份作序推荐人列表&#xff1a;阿里集团章文嵩博士|新浪TimYang|去哪网吴永强|丁香园冯大辉|蘑菇街岳旭强|途牛汤峥嵘|豆瓣洪强宁|某电商陈皓/林昊…… 这本书出自某电商技术部总监之手…

office漏洞利用--获取shell

环境&#xff1a; kali系统&#xff0c; windows系统 流程&#xff1a; 在kali系统生成利用文件&#xff0c; kali系统下监听本地端口&#xff0c; windows系统打开doc文件&#xff0c;即可中招 第一种利用方式&#xff0c; 适合测试用&#xff1a; 从git下载代码&#xff1a; …

pandas之DataFrame合并merge

一、merge merge操作实现两个DataFrame之间的合并&#xff0c;类似于sql两个表之间的关联查询。merge的使用方法及参数解释如下&#xff1a; pd.merge(left, right, onNone, howinner, left_onNone, right_onNone, left_indexFalse, right_indexFalse,    sortFalse, suffi…

typescript_如何掌握高级TypeScript模式

typescriptby Pierre-Antoine Mills皮埃尔安托万米尔斯(Pierre-Antoine Mills) 如何掌握高级TypeScript模式 (How to master advanced TypeScript patterns) 了解如何为咖喱和Ramda创建类型 (Learn how to create types for curry and Ramda) Despite the popularity of curry…

html函数splice,js数组的常用函数(slice()和splice())和js引用的三种方法总结—2019年1月16日...

总结&#xff1a;slice()和splice()slice(参数1,参数2)可以查找数组下对应的数据&#xff0c;参数1为起始位置&#xff0c;参数2为结束位置&#xff0c;参数2可以为负数&#xff0c;-1对应的是从后向前数的第一个数值。splice()可以进行增删改查数据操作&#xff0c;splice(参数…

leetcode 643. 子数组最大平均数 I(滑动窗口)

给定 n 个整数&#xff0c;找出平均数最大且长度为 k 的连续子数组&#xff0c;并输出该最大平均数。 示例&#xff1a; 输入&#xff1a;[1,12,-5,-6,50,3], k 4 输出&#xff1a;12.75 解释&#xff1a;最大平均数 (12-5-650)/4 51/4 12.75 代码 class Solution {publ…

python ==字符串

字符串类型(str)&#xff1a; 包含在引号&#xff08;单&#xff0c;双&#xff0c;三&#xff09;里面&#xff0c;由一串字符组成。 用途&#xff1a;姓名&#xff0c;性别&#xff0c;地址&#xff0c;学历&#xff0c;密码 Name ‘zbk’ 取值: 首先要明确&#xff0c;字符…

认证鉴权与API权限控制在微服务架构中的设计与实现(一)

作者&#xff1a; [Aoho’s Blog] 引言&#xff1a; 本文系《认证鉴权与API权限控制在微服务架构中的设计与实现》系列的第一篇&#xff0c;本系列预计四篇文章讲解微服务下的认证鉴权与API权限控制的实现。 1. 背景 最近在做权限相关服务的开发&#xff0c;在系统微服务化后&a…

mac下完全卸载程序的方法

在国外网上看到的&#xff0c;觉得很好&#xff0c;不仅可以长卸载的知识&#xff0c;还对mac系统有更深的认识。比如偏好设置文件&#xff0c;我以前设置一个程序坏了&#xff0c;打不开了&#xff0c;怎么重装都打不开&#xff0c;后来才知道系统还保留着原来的偏好设置文件。…

机器学习集群_机器学习中的多合一集群技术在无监督学习中应该了解

机器学习集群Clustering algorithms are a powerful technique for machine learning on unsupervised data. The most common algorithms in machine learning are hierarchical clustering and K-Means clustering. These two algorithms are incredibly powerful when appli…

自考本科计算机要学什么,计算机自考本科需要考哪些科目

高科技发展时代&#xff0c;怎离得开计算机技术&#xff1f;小学生都要学编程了&#xff0c;未来趋势一目了然&#xff0c;所以如今在考虑提升学历的社会成人&#xff0c;多半也青睐于计算机专业&#xff0c;那么计算机自考本科需要考哪些科目&#xff1f;难不难&#xff1f;自…

审查指南 最新版本_代码审查-最终指南

审查指南 最新版本by Assaf Elovic通过阿萨夫埃洛维奇 代码审查-最终指南 (Code Review — The Ultimate Guide) 构建团队代码审查流程的终极指南 (The ultimate guide for building your team’s code review process) After conducting hundreds of code reviews, leading R…

非对称加密

2019独角兽企业重金招聘Python工程师标准>>> 概念 非对称加密算法需要两个密钥&#xff1a;公钥&#xff08;publickey&#xff09;和私钥&#xff08;privatekey&#xff09;。公钥与私钥是一对&#xff0c;如果用公钥对数据进行加密&#xff0c;只有用对应的私…

管理Sass项目文件结构

http://www.w3cplus.com/preprocessor/architecture-sass-project.html 编辑推荐&#xff1a; 掘金是一个高质量的技术社区&#xff0c;从 CSS 到 Vue.js&#xff0c;性能优化到开源类库&#xff0c;让你不错过前端开发的每一个技术干货。 点击链接查看最新前端内容&#xff0c…

Spring—注解开发

Spring原始注解 Spring是轻代码而重配置的框架&#xff0c;配置比较繁重&#xff0c;影响开发效率&#xff0c;所以注解开发是一种趋势&#xff0c;注解代替xml配置文 件可以简化配置&#xff0c;提高开发效率。 Component 使用在类上用于实例化BeanController 使用在web层类…

政府公开数据可视化_公开演讲如何帮助您设计更好的数据可视化

政府公开数据可视化What do good speeches and good data visualisation have in common? More than you may think.好的演讲和好的数据可视化有什么共同点&#xff1f; 超出您的想象。 Aristotle — the founding father of all things public speaking — believed that th…