当前位置: 移动技术网 > IT编程>开发语言>Java > Spring依赖注入的三种方式小结

Spring依赖注入的三种方式小结

2019年07月19日  | 移动技术网IT编程  | 我要评论

spring的主要特性包括ioc和di,其中di是ioc的基础。在以前的spring使用过程中大部分都是使用xml配置文件显式配置spring组件,导致大量的xml配置文件以及冗余的xml配置代码。阅读《spring in action》后总结spring的di功能的三种主要装配方式以及混合装配方式

根据注解自动装配

spring中有非常丰富的注解,通过这些注解可以方便地配置spring容器,使得spring容器可以自动识别相关bean并做自动注入装配。

使用注解

  • @component注解:标注一个类,标识此类为一个java bean。
  • @configuration注解:标注一个类,标识此类为一个java配置类
  • @componentscan注解:标注一个类,用以配置扫描的包名称

示例代码

这里使用一个最简单的例子来展示自动装配的配置,代码结构如图所示。

actionconfig类用于做全局配置,我们知道,一名骑士一般都会有一匹马,所以这里horse和knight类作为java bean被spring容器创建,并将horse类的一个bean注入到knight中。所以在horse和knight中必须使用@component注解进行修饰。

knight代码:

package entities;

import org.springframework.stereotype.component;

/**
 * created by administrator on 2017/8/3 0003.
 */
@component
public class knight {
  private horse horse;

  knight(horse horse){
    this.horse = horse;
  }

  public void ride(){
    horse.bark();
  }
}

horse代码:

package entities;

import org.springframework.stereotype.component;

/**
 * created by administrator on 2017/8/3 0003.
 */
@component
public class horse {
  void bark(){
    system.out.println("horse!!!!");
  }
}

actionconfig代码:

package config;

import entities.knight;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;

/**
 * created by administrator on 2017/8/3 0003.
 */
@configuration
@componentscan(basepackageclasses = {knight.class})
public class actionconfig {
}

这里的componentscan注解内的属性用以注明knight类所在的包为扫描的基础包。凡是这个基础包及其子包内的标注了@component的类都将被视为java bean,这些bean被spring容器创建和管理。

java配置类装配

从自动装配的代码中,可以看出自动装配是非常简单方便的。java配置类进行装配的方式是采用对配置类中的方法进行注解标注,实现bean的创建和管理。

还是采用以上的例子,现在我们将上面的自动装配改成java配置类进行装配。

首先,删掉knight和horse中的@component注解,删掉actionconfig中的@componentscn注解,因为这些注解都是自动装配才需要的。

package entities;


/**
 * created by administrator on 2017/8/3 0003.
 */
public class knight {
  private horse horse;

  public knight(horse horse){
    this.horse = horse;
  }

  public void ride(){
    horse.bark();
  }
}


package entities;

/**
 * created by administrator on 2017/8/3 0003.
 */
public class horse {
  void bark(){
    system.out.println("horse!!!!");
  }
}

actionconfig类:

package config;

import entities.horse;
import entities.knight;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

/**
 * created by administrator on 2017/8/3 0003.
 */
@configuration
public class actionconfig {
  @bean
  public knight getknight(){
    return new knight(gethorse());
  }
  @bean
  public horse gethorse(){
    return new horse();
  }
}

通过@bean注解对相关的方法进行修饰,spring就可以知道调用这些方法来构建相应的bean,并注入到依赖的对象中。

xml配置文件装配

使用xml配置文件来装配组件是最为繁琐的,需要对各个bean做一一配置,特别是需要配置构造器或者setter的参数。

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

</beans>

以上是一个空的xml配置文件,看一下就知道,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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <bean id="knight" class="entities.knight">
    <constructor-arg ref="horse"></constructor-arg>
  </bean>
  
  <bean id="horse" class="entities.horse"></bean>
</beans>

通过配置两个bean元素,并且通过constructor-arg元素来配置构造函数注入的bean,达到依赖注入的目的。

导入和混合配置

有时候单纯的xml配置或者java配置不满足我们的需求怎么办呢?spring支持混合配置的方式来管理bean,通过java和xml配置的混合,达到你想要的效果。

java配置中引用java配置

在配置类中使用@import注解进行引入另一个java配置类。

package config;

import entities.horse;
import entities.knight;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.import;

/**
 * created by administrator on 2017/8/3 0003.
 */
@configuration
@import(beautyconfig.class)
public class actionconfig {
  @bean
  public knight getknight(){
    return new knight(gethorse());
  }
  @bean(name = "horse")
  public horse gethorse(){
    return new horse();
  }
}

beautyconfig代码:

package config;

import entities.beauty;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

/**
 * created by administrator on 2017/8/3 0003.
 */
@configuration
public class beautyconfig {
  @bean
  beauty getbeauty(){
    return new beauty();
  }
}

在main中,只需要通过actionconfig就可以获取到所有的上下文对象。

package main;

import config.actionconfig;
import entities.beauty;
import entities.knight;
import org.springframework.context.annotation.annotationconfigapplicationcontext;
/**
 * created by administrator on 2017/8/3 0003.
 */
public class main {
  public static void main(string[] args) {
    annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(actionconfig.class);
    knight knight = context.getbean(knight.class);
    knight.ride();
    beauty beauty = context.getbean(beauty.class);
    beauty.sleep();
  }
}

这里简单使用import引入了另一个配置的java文件,就实现了将多个bean分别配置到多个java配置类中。

java配置中引入xml配置

在java中引入xml配置的基本方法与引入java代码配置类似,只需要将@import改为@importresource,配上xml的文件地址即可。

@importresource("classpath:spring.xml")

以上,就是java配置spring依赖注入的方法。

xml配置中引入xml配置

同理,在xml配置中引入xml也需要使用import,但是在xml中是import标签,通过使用import标签导入另一个xml文件。

这里我新建了一个another.xml的文件,用以作为另一个配置文件示例。在原有的spring.xml中加入以下代码:

<import resource="another.xml"></import>

这样就简单导入了anoter.xml配置文件。

xml配置中引入java配置

乍看之下,在xml中似乎没有标签可以导入java配置类的,不过任何java配置类都是一个bean,所以可以通过配置bean标签来导入java配置类!

<bean class="config.beautyconfig"></bean>

通过在xml中配置这个java配置类的bean,就直接导入了在java配置类中的所有bean,真是神奇!

以下是配置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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <import resource="another.xml"></import>
  <bean class="config.beautyconfig"></bean>
  <bean id="knight" class="entities.knight">
    <constructor-arg ref="horse"></constructor-arg>
  </bean>

  <bean id="horse" class="entities.horse"></bean>
</beans>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网