spring笔记:第二章(1)

来源:互联网 发布:遥感数据下载 编辑:程序博客网 时间:2024/05/22 12:22

装配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;@Componentpublic 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@ComponentScanpublic 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