spring笔记:第二章(1)

装配bean

装配bean的作用是将多有对象初始化,通过spring进行bean与bean之间的关系绑定。

spring装配bean的方式

 1. 在XML中进行显示装配
 2. 在JAVA中进行显示装配
 3. 隐士的bean发现机制和自动装配
 建议尽量使用自动装配,显示配置越少越好。
 其次建议使用在java中进行显示装配
 最后再考虑使用xml进行显示装配

自动化装配Bean

spring从两个角度来完成自动化装配
1. 组件扫描:spring会自动发现应用上下文中所创建的bean。
2. 自动装配:spring自动满足bean之间的依赖

 组件扫描和自动装配结合使用才能发挥出最大功效。

自动化装配bean实例

实例包含一个CD光盘类,一个CD播放器类,一个测试类。
1.cd光盘接口类以及cd光盘类
package chujie.demo.spring.ioc.soundsystem;

/**
 * CD 接口
 * @author Administrator
 *
 */
public interface CompactDisc {
    void play();
}

package chujie.demo.spring.ioc.soundsystem;

import org.springframework.stereotype.Component;

@Component
public class SgtPeppers implements CompactDisc {

    private static String title = "CD标题";
    private static String artist = "CD内容";

    public void play() {
        System.out.println("title = "+title+" , artist = " + artist);
    }
}

2.cd播放器类

package chujie.demo.spring.ioc.soundsystem;

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

@Configuration
@ComponentScan
public class CDPlayerConfig {
}

3.测试类

package chujie.demo.spring.ioc.soundsystem;

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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayTest {
    @Autowired
    private CompactDisc cd;
    @Test
    public void test() {
        assertNotNull(cd);
    }
}
实例代码说明
1.测试类利用@RunWith(SpringJUnit4ClassRunner.class)注解,在启动过程中初始化了spring环境。
2.测试类利用@ContextConfiguration(classes=CDPlayerConfig.class)注解,指定对CDPlayerConfig类进行spirng初始化(根据类中的注解信息,做出相应处理)。
3.播放器类利用@ComponentScan注解加载当前包下所有@Component类(也就是cd类)。
4.测试类将cd类进行非空判断,因为在第三步时已经初始化cd类,所有非空判断为真。
为组件扫描的bean命名

spring为每个初始化的类设定了id。默认是采用类名(首字母小写)当做id。也可通过以下方法手动设定。

package chujie.demo.spring.ioc.soundsystem;

import org.springframework.stereotype.Component;

@Component("abcdefg")
public class SgtPeppers implements CompactDisc {

    private static String title = "CD标题";
    private static String artist = "不知道";

    public void play() {
        System.out.println("title = "+title+" , artist = " + artist);
    }
}

@Component(“abcdefg”)注解自定义bean的id。

设置组件扫描的基础包

1.默认规则
@ComponentScan默认规则:它会以配置类所在的包作为基础包来扫描组件(包含基础包的所有下级包)。
2.配置规则
@ComponentScan("包名"):它会将指定包作为基础包来扫描组件(包含基础包的所有下级包)
3.扩展配置1
@ComponentScan(basePackages="包名1")
@ComponentScan(basePackages={"包名1","包名2",...})
通过basePackages属性指定多个包,多个包作为基础包来扫描组件(包含基础包的所有下级包)
4.扩展配置2
@ComponentScan(basePackageClasses={类名1.class,类名2.class})
通过basePackageClasses属性指定多个类,每个类所在的包将作为基础包来扫描组件(包含基础包的所有下级包)

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

自动装配就是让spring自动满足bean依赖的一种方法,在满足依赖的过程中,会在spring应用上下文中寻找匹配某个bean需要的其他bean。为了声明要进行自动装配,需要借助spring的@Autowired注解。
实例
package chujie.demo.spring.ioc.soundsystem;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
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 cd;
    @Test
    public void test() { 
        assertNotNull(cd);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值