美文网首页
Spring依赖注入用法

Spring依赖注入用法

作者: sunpy | 来源:发表于2018-11-29 14:00 被阅读6次

常用依赖注入的方式

  1. setter注入
<bean id="person" class="cn.spy.spring.test.Person">
    <property name="name" value="张三"></property>
    <property name="age" value="22"></property>
</bean>
  1. 构造器注入
<bean id="person" class="cn.spy.spring.test.Person">
    <constructor-arg name="name" value="张三"></constructor-arg>
    <constructor-arg name="age" value="22"></constructor-arg>
</bean>

自动装配

首先spring在默认情况下是不会为Bean进行装配的,需要通过autowire来进行。

  1. byType:IOC容器使用与Bean类型相匹配的属性类进行注入
<beans:bean id="myServiceImpl" class="cn.spy.service.MyServiceImpl" autowire="byType"></beans:bean>
    
<beans:bean id="myDAOImpl" class="cn.spy.dao.MyDAOImpl"></beans:bean>
<beans:bean id="yourDAOImpl" class="cn.spy.dao.YourDAOImpl"></beans:bean>
public class MyServiceImpl {

    private MyDAOImpl myDAOImpl;

    private YourDAOImpl yourDAOImpl;
    
    public void setMyDAOImpl(MyDAOImpl myDAOImpl) {
        this.myDAOImpl = myDAOImpl;
    }

    public void setYourDAOImpl(YourDAOImpl yourDAOImpl) {
        this.yourDAOImpl = yourDAOImpl;
    }

    public void print() {
        myDAOImpl.insert();
        yourDAOImpl.insert();
    }
}

public class MyDAOImpl {
    
    public void insert() {
        System.out.println("插入我的记录");
    }
}

public class YourDAOImpl {

    public void insert() {
        System.out.println("插入你的记录");
    }
}

测试:

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        MyServiceImpl myServiceImpl = context.getBean("myServiceImpl", MyServiceImpl.class);
        myServiceImpl.print();
    }

Bean过滤的配置:autowire-candidate
下面例子中,过滤掉了yourDAOImpl这个Bean的注入,那么自然出现空指针异常。

<beans:bean id="myServiceImpl" class="cn.spy.service.MyServiceImpl" autowire="byType"></beans:bean>
    
<beans:bean id="myDAOImpl" class="cn.spy.dao.MyDAOImpl"></beans:bean>
<beans:bean id="yourDAOImpl" class="cn.spy.dao.YourDAOImpl" autowire-candidate="false"></beans:bean>

结果:

插入我的记录
Exception in thread "main" java.lang.NullPointerException
    at cn.spy.service.MyServiceImpl.print(MyServiceImpl.java:22)
    at cn.spy.spring.source.test.MySpringDI.main(MySpringDI.java:13)

  1. byName:IOC容器使用与Bean名称相匹配的属性类注入。
<beans:bean id="myServiceImpl" class="cn.spy.service.MyServiceImpl" autowire="byName"></beans:bean>
    
<beans:bean id="myDAOImpl" class="cn.spy.dao.MyDAOImpl"></beans:bean>
<beans:bean id="yourDAOImpl" class="cn.spy.dao.YourDAOImpl" autowire-candidate="false"></beans:bean>

全局设置

<?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:beans="http://www.springframework.org/schema/beans"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:task="http://www.springframework.org/schema/task" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-4.0.xsd" 
default-autowire="byName"></beans>

静态注入

  1. 工厂类使用静态方法创建对象
<beans:bean id="myServiceImpl" class="cn.spy.service.MyServiceFactory"
        factory-method="getStaticInstance" autowire="byType">
</beans:bean>
    
<beans:bean id="myDAOImpl" class="cn.spy.dao.MyDAOImpl"></beans:bean>
<beans:bean id="yourDAOImpl" class="cn.spy.dao.YourDAOImpl"></beans:bean>

工厂类静态方法:

public class MyServiceFactory {
    
    public static MyServiceImpl getStaticInstance() {
        return new MyServiceImpl();
    }
}
  1. 工厂类使用非静态方法创建对象
<beans:bean id="myServiceFactory" class="cn.spy.service.MyServiceFactory"></beans:bean>
<beans:bean id="myServiceImpl" factory-bean="myServiceFactory"
        factory-method="getInstance" autowire="byType">
</beans:bean>
    
<beans:bean id="myDAOImpl" class="cn.spy.dao.MyDAOImpl"></beans:bean>
<beans:bean id="yourDAOImpl" class="cn.spy.dao.YourDAOImpl"></beans:bean>

工厂类非静态方法:

public class MyServiceFactory {
    
    public MyServiceImpl getInstance() {
        return new MyServiceImpl();
    }
}

Bean作用域

spring默认的作用域是Singleton单例模式。
测试程序:

    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        Person person1 = ac.getBean("person", Person.class);
        Person person2 = ac.getBean("person", Person.class);
        System.out.println(person1 == person2);
    }
  1. singleton:Bean仅创建一个实例,默认作用域。
<bean id="person" class="cn.spy.spring.test.Person" scope="singleton">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
</bean>
  1. prototype:每一次对Bean定义或者其他获取都创建一个新的实例。
<bean id="person" class="cn.spy.spring.test.Person" scope="prototype">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
</bean>
  1. request:在web请求过程中使用相同Bean实例,每一个web请求都创建一个新的Bean实例。
<bean id="person" class="cn.spy.spring.test.Person" scope="request">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
</bean>
  1. session:在不同的Http会话中创建一个新的实例。仅适用支持web的ApplicationContrexts。
<bean id="person" class="cn.spy.spring.test.Person" scope="session">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
</bean>
  1. globalSession:仅适用于基于portlet的web应用程序的上下文。
<bean id="person" class="cn.spy.spring.test.Person" scope="globalSession">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
</bean>

延迟初始化

延迟初始化就是在spring ioc容器启动时不进行加载初始化,在使用的时候才开始加载初始化。

<bean id="person" class="cn.spy.spring.test.Person" lazy-init="true">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
</bean>

全局配置:

<?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-4.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd" 
default-lazy-init="true"></beans>

Bean生命周期

<bean id="person" class="cn.spy.spring.test.Person" init-method="initM" destroy-method="destoryM">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
</bean>
public class Person {

    private String name;
    private Integer age;
    
    public Person(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }

    public void initM() {
        System.out.println("Bean初始化执行的方法");
    }
    
    public void destoryM() {
        System.out.println("Bean销毁执行的方法");
    }
    
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
}

说明:init-method字段在Bean创建完毕之后,初始化方法会被spring IOC容器调用。而对于destory-method,如果singleton作用域则在spring IOC容器关闭时调用;如果request作用域则在当前web请求结束之后调用。但是prototype作用则无法跟踪Bean实例化之后的销毁,所以destory-method不会被调用。

相关文章

  • Spring依赖注入用法

    常用依赖注入的方式 setter注入 构造器注入 自动装配 首先spring在默认情况下是不会为Bean进行装配的...

  • Spring学习手册(5)—— bean作用域

    Spring学习手册(4)—— Spring 依赖注入中介绍了Spring依赖注入的基本方式以及各种类型的参数注入...

  • Spring学习之依赖注入

    Spring学习之依赖注入 依赖注入的基本概念 依赖注入(Dependecy Injection),也称为IoC(...

  • 2018-05-05

    spring源码分析(三) 目录五、Spring 源码解读--5.4、IOC 容器的依赖注入----1、依赖注入发...

  • Spring bean注解

    Spring自带依赖注入注解 @Required,依赖检查 @Autowired,根据Type注入 @Value,...

  • JAVA 核心笔记 || [xxx] Spring 之 依赖注入

    Spring 依赖注入 DL Spring 两种注入方式 Setter 方法注入 构造器注入 使用App.java...

  • 依赖注入

    依赖注入 Spring支持两种依赖注入方式,分别是属性注入和构造函数注入.除此之外,Spring 还支持工厂方法注...

  • spring源码解析-循环依赖

    讲解内容: spring的循环依赖---属性注入--自动注入 spring bean的生命周期 spring be...

  • 依赖注入的方式

    依赖注入: 依赖于某些方式给Bean的资源进行注入 Spring 支持三种依赖注入的方式 属性注入 构造器注入 工...

  • Spring 的属性注入

    Spring 的属性注入 Spring 的属性注入,我们可以理解成之前说过的 DI (依赖注入)。 注入方式 对于...

网友评论

      本文标题:Spring依赖注入用法

      本文链接:https://www.haomeiwen.com/subject/tyozqqtx.html