第二章 装配Bean

完整代码请见:https://github.com/codercuixin/SpringInAction
创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入(DI)的本质。

2.1 Sprig配置的可选方案

在xml文件中显式配置(基本上公司中都不用了)
在Java中进行显式配置
隐式的bean发现机制和自动装配
建议:尽可能使用自动配置的机制,显式配置越少越好。
当你必须要显式配置bean的时候(比如源码不是由你来维护的,而你需要这些代码配置bean的时候),使用类型安全并且比xml更为强大的JavaConfig。
也就是自己写的尽量使用自动配置,他人的(第三方)使用JavaConfig

2.2 自动化配置Bean

主要通过两个角度来实现自动化配置
组件扫描(component scanning):Spring会自动发现应用上下文所创建的Bean
自动装配(autowiring):Spring会自动满足bean之间的依赖

2.2.1 创建可被发现的Bean

总共分为三步:
第一步,将要使用的Bean添加上@Component注解

package stereo_autoconfig.soundsystem;

import org.springframework.stereotype.Component;

/**
 * Create by cuixin on 2018/8/26
 **/
@Component
public class SgtPeppers implements CompactDisc {
    private String title = "Sgt.Peppers's Lonely Hearts Club Band";
    private String artist = "The Beatles";
    @Override
    public void play() {
        System.out.println("Play "+title+" by "+ artist);
    }
}

第二步,通过@ComponentScan注解来扫描指定包及其子包中带有@Component注解的类

package stereo_autoconfig.soundsystem;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Create by cuixin on 2018/8/26
 * 通过@ComponentScan注解启动组件扫描,
 * 默认扫描所在包及其子包中带有@Component注解的类
 **/
@Configuration
@ComponentScan
public class CDPlayerConfig {
}

第三步,通过@ContextConfiguration配置上下文信息,也就是声明第二步中使用配置类

package stereo_autoconfig.soundsystem;

import org.junit.Test;
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;

import static org.junit.Assert.assertNotNull;

/**
 * Create by cuixin on 2018/8/26
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {
    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(cd);
    }

}

2.2.2 为组件扫描的bean命名

默认bean的id是将类名的第一个字母小写,比如上面的bean的ID就是sgtPeppers.
如果想要指定不同的ID,只需将值传给@Component注解。

@Component("lonelyHeartClub")
public class SgtPeppers implements CompactDisc {
    ...
}

2.2.3 设置组件扫描的基础包

方法一:基于String类型指定基础包.

Spring实战中说这种方式是类型不安全的,我使用IDEA重命名包时,只有在Refactor时选择Rename Package才会更改@ComponentScan中的String值,所以才说这是不安全的。
@ComponentScan默认的扫描的是所在包及其子包,如果你扫描其他包,或者想扫描多个包,应该怎么设置呢?指定不同的基础包

@Configuration //方式一,指定一个,通过设置value属性
@ComponentScan("stereo_autoconfig.soundsystem")
public class CDPlayerConfig {
}
@Configuration //方式二,指定一个,通过设置basePackages属性
@ComponentScan(basePackages = "stereo_autoconfig.soundsystem")
public class CDPlayerConfig {
}
@Configuration//方式三,指定多个,通过设置basePackages为数组
@ComponentScan(basePackages = {"stereo_autoconfig.soundsystem", "stereo_autoconfig.video"})
public class CDPlayerConfig {
}
方法二:基于包中所含的类或接口来指定基础包
@Configuration 
@ComponentScan(basePackageClasses = {CompactDisc.class, Video.class})
public class CDPlayerConfig {
}

当然为了重构时删除接口及相关类,你可以在包下面创建一个空标记接口(Marker Interface),专门用来指定基础包。

public Interface SoundSystemMarkerInface{
}
@Configuration 
@ComponentScan(basePackageClasses = {SoundSystemMarkerInface.class, Video.class})
public class CDPlayerConfig {
}

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

通过@ComponentScan,我们已经可以扫描得到Bean,但是如何装配这个Bean依赖的另一个bean呢?这就是自动装配解决的问题了。
简单来说,自动装配就是让Spring自动满足bean依赖的一种方法。

@Component
public class CDPlayer implements MediaPlayer {
    private CompactDisc cd;
    @Autowired
    public CDPlayer(CompactDisc cd){
        this.cd = cd;
        System.out.println("构造器 自动装配");
    }

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

    @Autowired
    public void setCompactDisc(CompactDisc cd){
        this.cd = cd;
        System.out.println("Setter方法 自动装配");
    }
    @Autowired(required = false)
    public void insertDisc(CompactDisc cd){
        this.cd = cd;
        System.out.println("其他方法 自动装配");
    }

}
package stereo_autoconfig.soundsystem;

import org.junit.Test;
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;

import static org.junit.Assert.assertNotNull;

/**
 * Create by cuixin on 2018/8/26
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {
    @Autowired
    private CompactDisc cd;
    @Autowired
    private MediaPlayer player;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(cd);
    }

    @Test
    public void play(){
        player.play();
    }
}

不管是构造器,Setter方法还是其他的方法,Spring都会去尝试满足方法参数上所声明的依赖。上面依次会先执行构造器方法,
然后按照出现代码顺序执行其他自动注入方法。所以上面的输出是
构造器 自动装配
Setter方法 自动装配
其他方法 自动装配

使用@AutoWired进行自动装配的时候,需要注意一下几点。

第一点,假如有且只有一个bean匹配依赖需求的话,那么这个bean就会被装配进来。
第二点,如果没有匹配的bean,那么在应用上下文创建的时候spring会抛出一个异常。
为了避免异常的出现,可以将@AutoWired的required属性设置为false。但这可能导致这个bean处于未转配状态,使用的使用需要判断是否为空。
第三点,如果出现多个bean满足依赖关系的话,spring的@AutoWired自动装配也会抛出异常,因为他们不知道选哪一个

2.3 通过Java代码装配bean

当使用第三方库中的组件装配到你的应用中,在这种情况下,是没有办法在它的类上添加@ComponentScan和@AutoWired注解的,因此就不能使用自动化装配方案了。

JavaConfig比xml的优势:更为强大,类型安全,并且对重构友好。
尽管不是必须的,但通常会将JavaConfig放到单独的包中,使他与其他的应用程序逻辑分离开来。
步骤:使用@Configuration创建配置类,并使用带有@Bean的方法声明bean,最后通过@ContextConfiguration来创建上下文

package stereo_javaconfig.soundsystem;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Create by cuixin on 2018/8/26
 **/
@Configuration //创建配置类
public class CDPlayerConfig {
    @Bean //通过bean注解来声明简单的bean
    public CompactDisc compactDisc(){
        return new SgtPeppers();
    }
    @Bean(name = "cdPlayer") //bean id默认使用的方法名,使用方法参数实现注入,最优方法,可以将CompactDisc放在其他配置类中
    public CDPlayer cdPlayer(CompactDisc compactDisc){
        return new CDPlayer(compactDisc);
    }
//    @Bean(name = "cdPlayer2")
//    //bean id默认使用的方法名,引用创建bean的方法实现注入,spring会拦截所有compactDisc()调用,
//    // 确保按照规定(single,request,session)使用正确的bean,而不是简单地每次调用都生成一个
//    public CDPlayer cdPlayer2(){
//        return new CDPlayer(compactDisc());
//    }

}
package stereo_javaconfig.soundsystem;

import org.junit.Test;
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;
import stereo_javaconfig.soundsystem.*;

import static org.junit.Assert.*;

/**
 * Create by cuixin on 2018/8/26
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayeTest {
    @Autowired
    private MediaPlayer player;
    @Test
    public void player(){
        player.play();
    }
}

2.4 通过xml装配Bean(略)

只是维护早先代码的时候你才会用到这个。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值