spring笔记:第二章(通过javaconfig类装配bean)

通过java代码装配bean

当需要自动装配第三方jar包类的时候,无法修改源码添加@Component和@Autowired注解,此时需要依靠java代码进行配置实现自动装配。
借助@Configuration注解将某个javaBean标注为spring配置类(spring环境初始化过程中会自动扫描)。
在spring配置类中可以利用@Bean标签标注某个方法为某个bean的生产者。方法内可以使用任何方法创建一个指定bean,方法被调用一次后返回值被缓存,下次调用直接返回缓存值,不再经过方法处理。除非使用@Scope(value="prototype")明确标注此方法非单例模式。这样在调用的话就会产生新的对象了。


javaConfig类实例
package chujie.demo.spring.ioc.soundsystem;

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

@Configuration
//@ComponentScan
public class CDPlayerConfig {
    @Bean
    public CompactDisc sgtPeppers() {
        return new SgtPeppers();
    }
    @Bean
    public CompactDisc simPeppers() {
        return new SgtPeppers("","");
    }

    @Bean
    public CompactDisc valuePeppers( String title , String artist ) {
        return new SgtPeppers( title , artist );
    }
}
引用类实例
package chujie.demo.spring.ioc.soundsystem;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayTest {

    @Autowired
    private CompactDisc sgtPeppers;

    @Autowired
    private CompactDisc simPeppers;

    @Test
    public void test() {
        assertNotNull(sgtPeppers);
        sgtPeppers.play();
    }
}
运行结果说明
@Autowired注解寻找对象的方式方法
第一步,根据类型查找所有的bean是否存在满足切唯一的对象。
有则使用,没有则异常,存在多个时执行第二步
第二步,根据属性名称过滤查找到的bean信息。有则使用,没有则异常。存在多个则报错。
@Autowired注解运行时无法指定参数,因为它只是查找依赖对象,并不是去初始化对象。

现在调用CDPlayerConfig.sgtPeppers方法,将得到一个cd对象,该对象是单例的。多次调用sgtPeppers方法也只会得到一个hash相同改的cd对象。

采用非单例模式定义的javaconfig实例

package chujie.demo.spring.ioc.soundsystem;

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

@Configuration
//@ComponentScan
public class CDPlayerConfig {
    @Bean
    @Scope(value="prototype")
    public CompactDisc sgtPeppers() {
        return new SgtPeppers();
    }
    @Bean
    @Scope(value="prototype")
    public CompactDisc simPeppers() {
        return new SgtPeppers("","");
    }

    @Bean
    @Scope(value="prototype")
    public CompactDisc txtPeppers() {
        return simPeppers();
    }
}

输出类及结果实例

package chujie.demo.spring.ioc.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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayTest {

    @Autowired
    private CompactDisc sgtPeppers;

    @Autowired
    private CompactDisc simPeppers;

    @Autowired
    private CompactDisc txtPeppers;
    @Test
    public void test() {
        System.out.println("sgt="+sgtPeppers.hashCode());
        System.out.println("sim="+simPeppers.hashCode());
        System.out.println("txt="+txtPeppers.hashCode());
    }
}

sgt=184201850
sim=1356557623
txt=1977573209
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值