Spring装配Bean -- 通过Java代码装配Bean

接上篇<<自动化装配Bean>>,创建对应组件如下:

创建可被发现的Bean:

        创建CD接口:

package main.java.Demo1;

/**
 * @author myvina@qq.com
 * @date 18-4-15 下午6:53
 */

public interface CompactDisc {
    void play();
}
        创建BeatlesCD类实现CD接口:
package main.java.Demo1;

import org.springframework.stereotype.Component;

/**
 * @author myvina@qq.com
 * @date 18-4-15 下午8:50
 */

public class BeatlesCD implements CompactDisc {
    @Override
    public void play() {
        System.out.println("Playing Beatles");
    }
}

        创建CDPlayer类引用BeatlesCD实例:

package main.java.Demo1;  
  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Component;  
  
/** 
 * @author myvina@qq.com 
 * @date 18-4-15 下午9:16 
 */  
  
public class CDPlayer {  
    private CompactDisc cd;  
  
    public CDPlayer(CompactDisc cd){  
        this.cd = cd;  
    }  
  
    public void play(){  
        cd.play();  
    }  
}  

       JavaConfig代码如下:

package main.java.Demo1;

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

/**
 * @author myvina@qq.com
 * @date 18-4-16 上午8:53
 */

@Configuration
public class CompactDiskConfig2 {
    @Bean
    public CompactDisc beatlesCD(){
        return new BeatlesCD();
    }

    @Bean
    public CDPlayer cdPlayer(){
        return new CDPlayer(beatlesCD());
    }
}
        同之前,测试代码如下:
package main.java.Demo1;

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;

/**
 * @author myvina@qq.com
 * @date 18-4-15 下午6:56
 */

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

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

    @Autowired
    private CDPlayer cdPlayer;

    @Test
    public void cdPlayerPlay(){
        cdPlayer.play();
    }
}
        运行cdShouldNotNull,结果如下:
四月 16, 2018 8:56:35 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
四月 16, 2018 8:56:36 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@38082d64, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@dfd3711, org.springframework.test.context.support.DirtiesContextTestExecutionListener@42d3bd8b, org.springframework.test.context.transaction.TransactionalTestExecutionListener@26ba2a48, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5f2050f6]
四月 16, 2018 8:56:36 上午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@3a82f6ef: startup date [Mon Apr 16 08:56:36 CST 2018]; root of context hierarchy
四月 16, 2018 8:56:38 上午 org.springframework.context.support.GenericApplicationContext doClose
信息: Closing org.springframework.context.support.GenericApplicationContext@3a82f6ef: startup date [Mon Apr 16 08:56:36 CST 2018]; root of context hierarchy

Process finished with exit code 0
        运行cdPlayerPaly,结果如下:
四月 16, 2018 8:58:21 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
四月 16, 2018 8:58:21 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@38082d64, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@dfd3711, org.springframework.test.context.support.DirtiesContextTestExecutionListener@42d3bd8b, org.springframework.test.context.transaction.TransactionalTestExecutionListener@26ba2a48, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5f2050f6]
四月 16, 2018 8:58:22 上午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@3a82f6ef: startup date [Mon Apr 16 08:58:22 CST 2018]; root of context hierarchy
Playing Beatles
四月 16, 2018 8:58:23 上午 org.springframework.context.support.GenericApplicationContext doClose
信息: Closing org.springframework.context.support.GenericApplicationContext@3a82f6ef: startup date [Mon Apr 16 08:58:22 CST 2018]; root of context hierarchy

Process finished with exit code 0

        成功输出"Playing Beatles"(但后面多了一些关闭框架的东西,暂时不懂!),测试成功.


补充:

        1.@Bean(name="name")可以为Bean命名.

        2.上面设置CDPlayer Bean时还可以用下面这种更简单的方式:

@Bean
    public CDPlayer cdPlayer(CompactDisc compactDisc){
        return new CDPlayer(compactDisc);
    }

        这种方法可以不明确地引用CompactCD的@Bean方法;这种方法也是最佳的选择,因为它不会要求将CompactCD声明到同一个配置类当中,这里甚至没有要求CompactCD必须在JavaConfig中声明,实际上它可以通过组件扫描功能或自动发现或者通过XML进行配置.

        3.上面是使用了CDPlayer的构造器实现了DI功能,但完全可以使用其他风格的DI配置,比如Setter方法,如下:

@Bean
    public CDPlayer cdPlayer(CompactDisc compactDisc){
        CDPlayer cdPlayer = new CDPlayer();
        cdPlayer.setCd(compactDisc);
        return cdPlayer;
    }
        带有@Bean注解的方法可以采用任何必要的Java功能来产生Bean实例.



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值