Spring配置有3种主要的装配机制:

  1. Java自动装配
  2. Java显式配置
  3. XML显式配置

Spring从组件扫描和自动装配满足自动化装配bean:

  • 组件扫描(component scanning):Spring自动发现应用上下文所创建的bean.
  • 自动装配(autowiring):Spring自动满足bean之间的依赖.

自动化装配可以在最大程度上将显式配置降低到最少,但在将第三方库中的组件装配到自己的应用时是无法添加@Component和@Autowired注解的,这个时候只能选择Java或XML的显式配置.


maven:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

CompactDisc是一个光盘接口,Class Stefanie是孙燕姿发行的一张光盘.

1. CompactDisc

package music;

/**
 * Created by guoli on 2017/4/17 0017.
 * aW1laWxpZ2VAZ21haWwuY29tCiAgICAgcXE6Mjg5MTY4NTEKICAgIHpoYW5nZ3VvbGkuY24=
 */
public interface CompactDisc {
    void play();
}

2. @Component

@Component注解表明该类作为组件类,并告知Spring要为这个类创建bean.

package music;

import org.springframework.stereotype.Component;

/**
 * Created by guoli on 2017/4/17 0017.
 * aW1laWxpZ2VAZ21haWwuY29tCiAgICAgcXE6Mjg5MTY4NTEKICAgIHpoYW5nZ3VvbGkuY24=
 */
@Component
public class Stefanie implements CompactDisc {
    @Override
    public void play() {
        System.out.println(" Stefanie Sun song Someone...");
    }
}

默认情况下Spring扫描的bean会将首字符变为小写来命名注册到IoC容器中,即Stefanie会注册为stefanie;也可以为扫描的bean命名:@Component("xxx")@Component(value="xxxx")

3. @ComponentScan

@ComponentScan启用组件扫描,如果未配置属性的话则默认以当前配置文件所在的包(music)作为basePackage来扫描组件. @Configuration表明当前Java文件是一个配置文件.

package music;

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

/**
 * Created by guoli on 2017/4/17 0017.
 * aW1laWxpZ2VAZ21haWwuY29tCiAgICAgcXE6Mjg5MTY4NTEKICAgIHpoYW5nZ3VvbGkuY24=
 */
@Configuration
@ComponentScan
public class CDPlayerConfig {
    
}

开发中常把配置文件放到单独的包中,使其和应用分开,这样需要明确的配置basePackages,单个基础包可以使用@ComponentScan(basePackages="xxx.xxx")配置,多个基础包用@ComponentScan(basePackages={"x.x", "xxxx.xxxx"})

4. @Autowired

使用Junit4测试Java自动配置. @Autowired自动注入CompactDisc. 使用SpringJUnit4ClassRunner自动创建Spring的应用上下文. @ContextConfiguration对应配置文件所在位置.

package music;

import static org.junit.Assert.*;

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;

/**
 * Created by guoli on 2017/4/17 0017.
 * aW1laWxpZ2VAZ21haWwuY29tCiAgICAgcXE6Mjg5MTY4NTEKICAgIHpoYW5nZ3VvbGkuY24=
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {

    @Autowired
    private CompactDisc compactDisc;

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

运行CDPlayerTest, 应该可以通过测试.

标签: Spring, 依赖注入

添加新评论