服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - spring的IoC和DI详解

spring的IoC和DI详解

2021-01-29 11:12好小伙 JAVA教程

这篇文章主要介绍了spring的IoC和DI详解,还是挺不错的,这里分享给加,需要的朋友可以参考下。

这里先来简单介绍下IoCDI的区别:

IOC:翻译过来是控制反转,将对象的创建权由Spring管理,HelloService不需要自己去创建,Spring可以帮你创建。

DI:依赖注入,在我们创建对象的过程中,把对象依赖的属性注入到我们的类中。

我们现在编写的类是没有其它的属性的,如果你学过UML这种设计的话,面向对象中对象中的几种关系

简单来书,IoC更像是一种思想,DI是一种行为。

另一种说法是,ioc是目的,di是手段。ioc是指让生成类的方式由传统方式(new)反过来,既程序员不调用new,需要类的时候由框架注入(di),是同一件不同层面的解读。

IoC:Inverse of Control(控制反转):

将原本在程序中手动创建对象的控制权,交由Spring框架来管理。

Spring第一个spring测试程序

1.准备jar包

spring-beans-4.1.2.RELEASE.jar
spring-core-4.1.2.RELEASE.jar
com.springsource.org.apache.commons.logging-

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class HelloSpring {
    @Setter //必须有set属性
    private String name;
    public void say() {
        System.out.println("hello---" + name);
    }
}
@Test
//把创建的对象交给spring框架管理
public void test1() throws Exception {
    //1读取资源文件
    Resource resource = new ClassPathResource("/applicationContext.xml");
    //2创建spring容器对象
    BeanFactory bf = new XmlBeanFactory(resource);
    //3从srping容器中获得指定名称对象
    HelloSpring hello = bf.getBean("helloSpring", HelloSpring.class);

配置applicateion.xml文件

?
1
2
3
4
5
6
7
8
9
10
11
<?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">
 
<beans>
  <bean name="helloSpring" class="全限定类名">
    <property name="需要注入的属性名称" value="注入值" />
  </bean>
</beans>

使用import元素引入其他的配置文件:

?
1
<import resource="classpath:bin目录文件路径"/>

使用import元素注意:

1、默认情况下,从classpath的跟路径寻找。
2、可以使用前缀来定位文件的基础位置:

①:[classpath:]:后面的文件从classpath路径开始找(推荐);[注意classloader的问题。]
②:[file:]:后面的文件使用文件系统的路径开始找;

注意:只有当框架中实现了Resource接口才能够识别上述的前缀标识符。

Spring中的测试

Spring测试环境准备:

依赖jar:

1.spring-test-4.1.2.RELEASE.jar
2.spring-context-4.1.2.RELEASE.jar
3.spring-aop-4.1.2.RELEASE.jar
4.spring-expression-4.1.2.RELEASE.jar

Spring4.x需要依赖的单元测试得是最新的junit4.12,Eclipse自带的junit4.8不支持,同时从Spring4.x开始,还得依赖AOP包的支持。

junit-4.12.jar
hamcrest-core-1.3.jar

?
1
2
3
4
5
6
7
8
9
10
11
@RunWith(SpringJUnit4ClassRunner.class)表示先启动Spring容器,把junit运行在Spring容器中
@ContextConfiguration("classpath:applicationContext.xml"):表示从哪里加载资源文件
public class HelloWorldTest {
    @Autowired :表示自动装配
    private BeanFactory factory;
    @Test
    public void testSpringTest() throws Exception {
        HelloWorld helloWorld = factory.getBean("springTest", HelloWorld.class);
        helloWorld.sayHello();
    }
}

把@ContextConfiguration(“classpath:applicationContext.xml”) 写成@ContextConfiguration
默认去找的当前测试类名-context.xml配置文件,如:HelloWorldTest-context.xml

Spring容器

BeanFactory:是Spring中最底层的接口,只提供了最简单的IoC功能(创建和管理bean)。

在应用中,一般不使用BeanFactory,而推荐使用ApplicationContext(应用上下文),原因如下。

被Spring所管理的成员都称之为bean(类,对象,bean元素).

BeanFactory和ApplicationContext的区别

1、ApplicationContext继承了BeanFactory,拥有了基本的IoC功能;
2、除此之外,ApplicationContext还提供了以下的功能:

①、支持国际化;
②、支持消息机制;
③、支持统一的资源加载;
④、支持AOP功能;

bean的创建时机:

1.ApplicationContext在加载的时候就会创建所有的bean(Web应用建议).
2.BeanFactory需要等到拿bean的时候才会创建bean(桌面程序),延迟初始化.

?
1
2
3
4
//针对于当前xml中所有的bean:是否需要延迟初始化.
<bean lazy-init="default | false | true">
//针对于指定的bean:是否需要延迟初始化.
<beans default-lazy-init="default | false | true">

bean的作用域,bean对象可以存活多久

?
1
2
3
4
5
6
7
<bean id="" class="" scope="作用域"/>
singleton: 单例 ,在Spring IoC容器中仅存在一个Bean实例 (默认的scope)
prototype: 多例 ,每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时 ,相当于执行new XxxBean():不会在容器启动时创建对象
request: 用于web开发,将Bean放入request范围 ,request.setAttribute("xxx") , 在同一个request 获得同一个Bean
session: 用于web开发,将Bean 放入Session范围,在同一个Session 获得同一个Bean
globalSession: 一般用于Porlet应用环境 , 分布式系统存在全局session概念(单点登录),如果不是porlet环境,globalSession 等同于Session
对于Struts2中的Action使用prototype类型,其他使用singleton

DI:Dependency Injection:
依赖注入介绍 和XML的自动装配
指Spring创建对象的过程中,将对象依赖属性通过配置进行注入
xml方式-自动装配(一般不推荐使用):

?
1
2
<bean />元素的:autowire属性
<bean id="somebean" class="SomeBean全限定名" autowire="byType"/>

autowire属性:让spring按照一定的方式自己去找合适的对象,并完成DI

- default:不要自动注入
- no:不要自动注入
- byName:按照名字注入(按照属性的名字在spring中找bean) factory.getBean(“属性的名字”)
- byType:按照依赖对象的类型注入(factory.getBean(属性的类型))
- constructor:按照对象的构造器上面的参数类型注入

注意:

1,如果按照byName自动注入,要求所有的属性名字和id的名字必须保证一种规范的命名方式;
2,如果按照byType注入,如果spring中同一个类型有多个实例-->报bean不是唯一类型错误;

手动装配:

属性注入: 通过对象的setter方法注入依赖的对象.

使用setter注入:
1,使用bean元素的子元素设置;
1,简单类型值,直接使用value赋值;
2,引用类型,使用ref赋值;
3,集合类型,直接使用对应的集合类型元素即可。
2,spring通过属性的setter方法注入值;
3,在配置文件中配置的值都是string,spring可以自动的完成类型的转换
4,属性的设置值是在init方法执行之前完成的
5,改进spring的测试,直接在测试类里面注入需要测试的对象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//实体类
@Setter
public class Employee {
    private String name;
}
//测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class EmployeeTest {
    @Autowired
      private ApplicationContext ctx;
    @Test
      public void test1() throws Exception {
        Employee bean = ctx.getBean("employee", Employee.class);
        System.out.println(bean);
    }
}
//application配置
<bean id="employee" class="com.***.Employee">
  <property name="name" value="zoe" />
</bean>

构造器注入: 通过构造器,在创建对象的时候就注入依赖对象

constructor-arg:构造器参数

1,spring在实例化对象的时候,如果对象没有配置constructor-arg,则使用默认的构造器实例化对象
2,如果有constructor-arg,那么spring使用这些constructor-arg来唯一确定一个构造器
1,默认情况下,constructor-arg的顺序就是构造器参数的顺序
2,3中调整构造器顺序:
1.index:在构造器中的参数位置
2.type:在构造器中的参数的类型
3.name:在构造器中按照构造器的参数名字设置值

使用哪种注入方式比较好(setter?构造器?)?

1,如果一个类必须依赖另一个类才能正常运行,用构造器;
2,但是构造器的参数如果过多,构造器很难看;
3,更多的还是使用setter注入;
4,可以使用@Required标签来要求一个属性必须注入

?
1
2
3
4
5
6
7
8
9
10
public class Employee {
  private String name;
  public Employee(String name) {
    this.name = name;
  }
}
//application配置
<bean id="employee" class="com.***.Employee">
  <constructor-arg name="name" value="zoe" />
</bean>

属性占位符(property place holder)

spring属性占位符之创建连接池对象

依赖的jar:>>MySQL驱动包>>druid包.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class PropertyPlaceHolderTest {
  @Autowired
  private DataSource ds ;
 
  @Test
  public void testLink() throws Exception {
    Connection conn = ds.getConnection();
    String sql = "select * from user";
    PreparedStatement ps = conn.prepareStatement(sql);
    ResultSet rs = ps.executeQuery();
  }
}

application配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  ">
<!-- 加载 properties -->
<!-- system-properties-mode="NEVER" 不使用系统默认属性 -->
<context:property-placeholder location="classpath:db.properties"
  system-properties-mode="NEVER" />
 
<!-- 配置连接池 -->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
  <property name="driverClassName" value="${jdbd.driverClassName}" />
  <property name="url" value="${jdbd.url}" />
  <property name="jdbd.username" value="${username}" />
  <property name="jdbd.password" value="${password}" />
</bean>
</beans>

db.properties文件

?
1
2
3
4
jdbd.driverClassName=com.mysql.jdbc.Driver
jdbd.url=jdbc:mysql:///springdemo
jdbd.username=root
jdbd.password=admin

总结

以上就是本文关于spring的IoC和DI详解的全部内容,希望对大家有所帮助。有什么问题可以随时留言指出,感谢大家!

原文链接:https://www.2cto.com/kf/201609/548582.html

延伸 · 阅读

精彩推荐