Spring DI使用详解

一、介绍

  • DI的定义:依赖注入,为类里面的属性设值。例如,我们之前的setName方法就是在为name属性设值。
  • IOC与DI的关系:IOC进行对象的创建,DI进行值的注入。二者共同管理JavaBean,但DI是在IOC的基础上存在的,它不能单独存在。

二、代码演示

DI依赖注入也有两种方式,即配置文件注入和注解注入

一、配置文件注入

属性须知:

  • type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型
  • index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值,索引的位置从 0 开始
  • name:用于指定给构造函数中指定名称的参数赋值(一般用这个)
  • value:用于提供基本类型和String类型的数据
  • ref:用于指定其他的bean类型数据,即bean的id

前期代码准备:

//Dao.Class文件
public class Dao {
private String testDI;
public Dao(String testDI){
this.testDI=testDI;
}
}
//Service.Class文件
public class Service {
private Dao dao;
private String test;
private Map<String,String> map;
private Properties properties; public void setProperties(Properties properties) {
this.properties = properties;
} public void setMap(Map<String, String> map) {
this.map = map;
}
public void setDao(Dao dao) {
this.dao = dao;
}
public void setDao(Dao dao) {
this.dao = dao;
}
private String[] args;
private List<String> list; public void setArgs(String[] args) {
this.args = args;
}
public void setList(List<String> list) {
this.list = list;
}
}

applicationContext.xml配置文件

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>

配置文件注入又分三种 :

  1. 使用有参构造注入(以Dao类为例)

    <?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dao" class="com.testWeb.dao.impl.Dao">
    <constructor-arg name="testDI" value="测试DI"></constructor-arg>
    </bean>
    </beans>
  2. 使用set方法注入(以Service类为例,注意: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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dao" class="com.testWeb.dao.impl.Dao">
    <constructor-arg name="testDI" value="测试DI"></constructor-arg>
    </bean>
    <bean id="service" class="com.testWeb.service.Service">
    <property name="dao" ref="dao"></property>
    </bean>
    </beans>
  3. P名称空间注入

    <?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="dao" class="com.testWeb.dao.impl.Dao">
    <constructor-arg name="testDI" value="测试DI"></constructor-arg>
    </bean>
    <bean id="service" class="com.testWeb.service.Service" p:dao-ref="dao" p:test="测试"></bean>
    </beans>

4.复杂属性的注入

这里复杂属性的注入其实属于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"
xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--
id:用于SpringIOC调用,可以为任意
class:类的全路径
-->
<bean id="user" class="com.testWeb.daomain.User"></bean> <!--开启注解扫描-->
<context:component-scan base-package="com.testWeb"></context:component-scan>
<bean id="dao" class="com.testWeb.dao.impl.Dao">
<constructor-arg name="testDI" value="测试DI"></constructor-arg>
</bean>
<bean id="service" class="com.testWeb.service.Service" p:dao-ref="dao" p:test="测试">
<!--数组-->
<property name="args">
<list>
<value>测试1</value>
<value>测试2</value>
<value>测试3</value>
</list>
</property>
<!-- List-->
<property name="list">
<list>
<value>测试1</value>
<value>测试2</value>
<value>测试3</value>
</list>
</property>
<!--Map-->
<property name="map">
<map>
<entry key="name" value="LiMing"></entry>
<entry key="class" value="Class1"></entry>
<entry key="hoby" value="PingPang"></entry>
</map>
</property>
<!--properties-->
<property name="properties">
<props>
<prop key="driverclass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--开启注解扫描-->
<context:component-scan base-package="com.testWeb"></context:component-scan>
</beans>

第二步、利用注解创建对象并注入属性

//Dao.class文件
@Service(value = "dao")
public class Dao { public void test(){
System.out.println("test");
}
}
//Service.class文件
@Service(value = "service")
public class Service {
//得到dao对象
//在dao属性上利用注解直接注入,使用注解不用set方法
@Autowired //自动装配
private Dao dao;
//name中注解创建对象的Value值
@Resource(name="dao")
private Dao dao1;
}

小节,一般在实际开发中,对JavaBean的管理一般是,配置文件进行对象的创建,注解进行属性的注入

Spring DI使用详解的更多相关文章

  1. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  2. Spring jar包详解

    Spring jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spri ...

  3. Spring——jar包详解(转)

    Spring——jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spr ...

  4. Spring Boot异常处理详解

    在Spring MVC异常处理详解中,介绍了Spring MVC的异常处理体系,本文将讲解在此基础上Spring Boot为我们做了哪些工作.下图列出了Spring Boot中跟MVC异常处理相关的类 ...

  5. spring事务配置详解

    一.前言 好几天没有在对spring进行学习了,由于这几天在赶项目,没有什么时间闲下来继续学习,导致spring核心架构详解没有继续下去,在接下来的时间里面,会继续对spring的核心架构在继续进行学 ...

  6. spring注入参数详解

    spring注入参数详解 在Spring配置文件中, 用户不但可以将String, int等字面值注入到Bean中, 还可以将集合, Map等类型的数据注入到Bean中, 此外还可以注入配置文件中定义 ...

  7. Spring的lazy-init详解

    1.Spring中lazy-init详解ApplicationContext实现的默认行为就是在启动服务器时将所有singleton bean提前进行实例化(也就是依赖注入).提前实例化意味着作为初始 ...

  8. Spring Security Filter详解

    Spring Security Filter详解 汇总 Filter 作用 DelegatingFilterProxy Spring Security基于这个Filter建立拦截机制 Abstract ...

  9. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

随机推荐

  1. 负载均衡服务之HAProxy基础入门

    首先我们来了解下haproxy是干嘛的?haproxy是一个法国人名叫Willy Tarreau开发的一个开源软件:这款软件主要用于解决客户端10000以上的同时连接的高性能的TCP和HTTP负载均衡 ...

  2. Alpha Release Note 12/15/2015

    内容提要: ******Personal Photo Experience可供您存放所有的私人照片,系统会自动整理内容,您可以借助搜索功能快速找到所需图片,同时过滤重复图片和低质量图片,给您全新的搜索 ...

  3. D-Power Products

    题目连接: 题解: 根据题目的意思,对每个X进行质因子分解,保存其质因子以及质因子出现的个数,如果两个数的乘积变成一个数的K次幂,那么两个数的质因子的指数之间相加应为k的倍数.保存完毕后,开始遍历,将 ...

  4. ubuntu17.10安装lnmp安装包的核心问题-gcc版本、g++版本

    大致碰到的问题都是这样,不是php安装失败,就是MySQL安装失败,或者Nginx也安装失败 基本上是花式报错.后来在军哥的论坛中找到了这个帖子:https://bbs.vpser.net/viewt ...

  5. Java 添加、隐藏/显示、删除PDF图层

    本文介绍操作PDF图层的方法.可分为添加图层(包括添加线条.形状.字符串.图片等图层).隐藏或显示图层.删除图层等.具体可参考如下Java代码示例. 工具:Free Spire.PDF for Jav ...

  6. git工具上传项目到码云

    首先,你需要在本地安装git客户端,此处简单易懂,略过然后,在本地建好文件夹,以本人为例,我的路径为 E:\git_project,此时需要通过鼠标右键选择:git bush here 如图所示然后会 ...

  7. MySQL主从数据库配置与原理

    1.为什么要搭建主从数据库 (1)通过增加从库实现读写分离,提高系统负载能力 (2)将从库作为数据库备份库,实现数据热备份,为数据恢复提供机会 (3)根据业务将不同服务部署在不同机器同时又共享相同的数 ...

  8. Spring Boot中的测试

    文章目录 简介 添加maven依赖 Repository测试 Service测试 测试Controller @SpringBootTest的集成测试 Spring Boot中的测试 简介 本篇文章我们 ...

  9. Hyperledger Fabric基础知识

    文章目录 什么是Hyperledger Fabric? Hyperledger架构是怎么工作的? Hyperledger交易如何执行 总结 Hyperledger Fabric基础知识 本文我们会介绍 ...

  10. hdu_1052 Tian Ji -- The Horse Racing 贪心

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...