Spring系列之装配Bean

Spring 的三种装配Bean的方式

  • 组件扫描+自动装配(隐式)
  • 通过Java config装配bean(显示)
  • 通过XML装配bean(显示)

一、组件扫描+自动装配(隐式配置)

  •   组件扫描:

     Spring会自动发现应用上下文中所创建的bean

  •   自动装配:

            Spring会自动满足bean之间的依赖关系

适用情况:

  简单bean,初始化不需要基本bean之外的其他参数,无参构造函数或者仅需要其他bean,如果需要其他bean作为属性需要用@Autowired注入,需要事先定义好装配策略。

  

关键字:@Component:标识Bean,可被自动扫描发现。

    @Configuration+@ComponentScan(basepackages="xx"): 制定扫描基本包,作为Spring上下文的配置文件。

    @Autowired:自动装配,查找容器中满足条件的Bean,注入。

实例:

CompactDisc.java

package cdplayer;

public interface CompactDisc {
    void play();
}

SgtPeppers.java

package cdplayer;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@Component
public class SgtPeppers implements CompactDisc {

    private String title="享受孤独的音乐";
    private String article="奏出和谐的篇章";

    @Override
    public void play() {
        System.out.println(title+article);
    }
}

CDPlayerConfig.java

package cdplayer;

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

@Configuration
@ComponentScan
public class CDPlayerConfig {
}

CDPlayerTest.java

package cdplayer;

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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {
    @Autowired
    private CompactDisc cd;

    @Test
    public void test(){
        cd.play();
    }
}

 

二、通过Java config装配bean(显示)

适用情况:所有情况基本适用,区别方法一:不需要事先定义好装配策略,因为会在config.java中定义。

 

关键词:@Configuration + @Bean:创建config.java,并定义bean的获取方法且需要用@Bean注释。

 

CDPlayerConfig.java

package cdplayer;

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

@Configuration
//@ComponentScan
public class CDPlayerConfig {
    @Bean
    public CompactDisc compactDisc(){
        return new SgtPeppers();
    }
}

SgtPeppers.java

package cdplayer;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

//@Component
public class SgtPeppers implements CompactDisc {

    private String title="享受孤独的音乐";
    private String article="奏出和谐的篇章";

    @Override
    public void play() {

        System.out.println(title+article);
    }
}

测试类不变。

三、通过XML装配bean(显示)

cdplayer.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:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <bean id="compactDisc" class="cdplayer.SgtPeppers"
        c:_0="Sgt. Pepper's Lonely Hearts Club Band"
        c:_1="The Beatles">
    </bean>

</beans>

CDPlayerTest:

package cdplayer;

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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "cdplayer.xml")
public class CDPlayerTest {
    @Autowired
    private CompactDisc cd;

    @Test
    public void test(){
        cd.play();
    }
}

 

posted @ 2017-10-17 21:01  涛声依旧~  阅读(131)  评论(0编辑  收藏  举报