当前位置: 移动技术网 > IT编程>开发语言>Java > 浅谈Spring装配Bean之组件扫描和自动装配

浅谈Spring装配Bean之组件扫描和自动装配

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

spring从两个角度来实现自动化装配:

  • 组件扫描:spring会自动发现应用上下文中所创建的bean。
  • 自动装配:spring自动满足bean之间的依赖。

案例:音响系统的组件。首先为cd创建compactdisc接口及实现类,spring会发现它并将其创建为一个bean。然后,会创建一个cdplayer类,让spring发现它,并将compactdisc bean注入进来。

创建compactdisc接口:

package soundsystem;

public interface compactdisc {
 void play();
}

实现compactdisc接口:

package soundsystem;
import org.springframework.stereotype.component;

@component
public class sgtpeppers implements compactdisc {

 private string title = "sgt. pepper's lonely hearts club band"; 
 private string artist = "the beatles";
 
 public void play() {
  system.out.println("playing " + title + " by " + artist);
 }
 
}

在sgtpeppers类上使用了 @component注解,这个注解表明该类会作为组件类,并告知spring要为这个类创建bean,不需要显示配置sgtpeppers bean。

不过组件扫描默认是不开启的。我们需要显示配置一下spring,从而命令spring去寻找带有 @component注解的类,并创建bean。

显示配置spring包括java和xml两种方式,通过java启用组件扫描:

package soundsystem;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;

@configuration
@componentscan
public class cdplayerconfig { 
}

注意,类cdplayerconfig通过java代码定义了spring的装配规则,但是可以看出并没有显示地声明任何bean,只不过它使用了 @componentscan注解,这个注解能够在spring中启用组件扫描。

如果没有其他配置的话,@componentscan默认会扫描与配置类相同的包。因为cdplayerconfig位于sound system包中,因此spring默认将会扫描这个包以及这个包下的所有子包,查找所有带有 @component注解的类。这样的话,sgtpeppers类就会被自动创建一个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:context="http://www.springframework.org/schema/context";
 xmlns:c="http://www.springframework.org/schema/c";
 xmlns:p="http://www.springframework.org/schema/p";
 xsi:schemalocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">;

 <context:component-scan base-package="soundsystem" />

</beans>

测试组件扫描能够发现compactdisc:

package soundsystem;

import static org.junit.assert.*;

import org.junit.rule;
import org.junit.test;
import org.junit.contrib.java.lang.system.standardoutputstreamlog;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;

@runwith(springjunit4classrunner.class)
@contextconfiguration(classes=cdplayerconfig.class)
public class cdplayertest {

 @rule
 public final standardoutputstreamlog log = new standardoutputstreamlog();
 
 @autowired
 private compactdisc cd;
 
 @test
 public void cdshouldnotbenull() {
  assertnotnull(cd);
 }

}

cdplayertest使用了spring的springjunit4classrunner,以便在测试开始的时候自动创建spring的应用上下文。注解contextconfiguration会告诉它需要在cdplayerconfig类中加载配置。因为cdplayerconfig类中包含了 @componentscan,因此最终的应用上下文应该包含compactdisc bean。

 测试方法断言cd属性部位null。如果不为null。就意味着spring能发现compactdisc类,并自动在spring上下文中创建为bean并注入到测试代码中。

为组件扫描的bean命名

spring应用上下文中所有的bean都会给定一个id。默认bean命名为类名的第一个字母变为小写,上面的sgtpeppers bean指定的id为sgtpeppers。

如果想要指定bean id,可以将id值传递给 @component注解。如下:

@component("lonelyheartsclub")
public class sgtpeppers implements compactdisc {
 
}

设置组件扫描的基础包

@component注解没有设置任何属性的情况下,按照默认规则,spring会以配置类所在的包作为基础包来扫描组件。但是如果想扫描不同的包,需要做的就是@component的value属性中指名包的名称:

@configuration
@componentscan("soundsystem")
public class cdplayerconfig { 
}

如果想更加清晰的表明设置的基础包,可以通过设置basepackages属性:

@configuration
@componentscan(basepackages="soundsystem")
public class cdplayerconfig { 
}

同时basepackages支持多个基础包的设置,属性设置为数组即可:

@configuration
@componentscan(basepackages={"soundsystem", "voice"})
public class cdplayerconfig { 
}

另外还提供一种方法,可以指定包中所含的类或接口:

@configuration
@componentscan(basepackegeclasses={cdplayer.class, dvdplayer.class})
public class cdplayerconfig { 
}

通过为bean添加注解实现自动装配

自动装配就是让spring自动满足bean依赖的一种方式,在满足依赖的过程中,会在spring的上下文中寻找匹配一个bean需求的其他bean。为了声明要进行自动装配,我们可以借助spring的 @autowried注解。

比如说cdplayer类,它在构造器上添加了 @autowried注解,这表明当创建cdplayer bean的时候,会通过这个构造器来进行实例化并且会传入一个可设置给compactdisc类型的bean。

package soundsystem;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.component;

@component
public class cdplayer implements mediaplayer {
 private compactdisc cd;

 @autowired
 public cdplayer(compactdisc cd) {
  this.cd = cd;
 }

 public void play() {
  cd.play();
 }

}

@autowried注解不仅能够用在构造器上,还能用在setter方法上。
@autowired
public void setcompactdisc(compactdisc cd) {
  this.cd = cd;
}

事实上,@autowried注解可以用在类的任何方法上去引入依赖的bean,spring都会尝试满足方法参数上所声明的依赖。

如果没有匹配的bean,那么在应用上下文创建的时候,spring会抛出一个异常。为了避免出现异常,可以将 @autowried的required属性设置为false:

@autowired(required=false)
public cdplayer(compactdisc cd) {
  this.cd = cd;
}

设置以后,会尝试自动装配,但是如果没有匹配的bean,spring默认会处于未装配的状态。但是把required设置为false时,需要谨慎对待,如果代码中没有进行null检查的话,建议不使用,不然就会出现nullpointerexception异常。

验证自动装配

前面我们的cdplayertest测试类,实现了自动装配compactdisc,现在我们借助cdplayer bean播放cd,表现出依赖的自动装配:

package soundsystem;

import static org.junit.assert.*;

import org.junit.rule;
import org.junit.test;
import org.junit.contrib.java.lang.system.standardoutputstreamlog;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;

@runwith(springjunit4classrunner.class)
@contextconfiguration(classes=cdplayerconfig.class)
public class cdplayertest {

 @rule
 public final standardoutputstreamlog log = new standardoutputstreamlog();

 @autowired
 private mediaplayer player;
 
 @autowired
 private compactdisc cd;
 
 @test
 public void cdshouldnotbenull() {
  assertnotnull(cd);
 }

 @test
 public void play() {
  player.play();
  assertequals(
    "playing sgt. pepper's lonely hearts club band by the beatles\n", 
    log.getlog());
 }

}

现在除了注入compactdisc,还将cdplayer bean注入到测试代码的player成员变量中。

总结一下,自动装配bean的过程:

 一、把需要被扫描的类,添加 @component注解,使它能够被spring自动发现。
 二、通过显示的设置java代码 @componentscan注解或xml配置,让spring开启组件扫描,并将扫描的结果类创建bean。
 三、@autowried注解能偶实现bean的自动装配,实现依赖注入。

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

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

相关文章:

验证码:
移动技术网