当前位置: 代码迷 >> 综合 >> Sping(一):Bean装配和注入
  详细解决方案

Sping(一):Bean装配和注入

热度:14   发布时间:2024-01-04 07:49:29.0

spring有三种bean装配方式:自动装配、java代码装配、xml装配。
1.自动装配
通过扫描包中的注解(如@Component)生成bean,通过注解@Autowired查找匹配的bean注入。
例子:
1)定义接口和实现类
定义接口CompactDisc
CompactDisc.java:

package soundsystem;public interface CompactDisc {void play();
}

定义实现类,并加上注解@Component,这个注解会告知Spring为这个类创建bean,不需要再显式配置。
SgtPeppers.java

package soundsystem;
import org.springframework.stereotype.Component;@Component
public class SgtPeppers implements CompactDisc {
    private String songName = "love";private String artist = "MoWenWei";@Overridepublic void play() {System.out.println("playing " + songName + " by "  + artist);}}

2)通过配置类或者xml定义组件扫描
方式一:通过配置类
创建装配规则类CDPlayerConfig,类CDPlayerConfig定义了Spring的装配规则,@ComponentScan默认是扫描与配置类相同的包;

package soundsystem;import org.springframework.context.annotation.*;@Configuration
@ComponentScan
public class CDPlayerConfig {
    }

方式二:通过xml
resources/CDPlayerConfig.xml,context:component-scan和@ComponentScan注解有对应的属性和元素;

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="soundsystem"/>
</beans>

3)Run as Junit Test
对应上一步中通过配置类定义扫描组件,的测试代码CDPlayerTest.java:

package soundsystem;import org.junit.Test;
import static  org.junit.Assert.*;
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) //对应上一步的,方式一:通过配置类
//@ContextConfiguration(locations = {
   "classpath:CDPlayerConfig.xml"})对应上一步的,方式二:通过xml,注意,需要配置classpath目录resouces才能找到CDPlayerConfig.xml
public class CDPlayerTest {@Autowiredprivate CompactDisc cd;@Testpublic void cdShouldNotBeNull(){assertNotNull(cd);cd.play();}
}

Run as Junit Test测试OK。
4)在java application中
如果步骤3)采用的是方式一,使用new AnnotationConfigApplicationContext(“soundsystem”);或者new AnnotationConfigApplicationContext(CDPlayerConfig.class)来构造ApplicationContext;
如果步骤3)采用的是方式二,使用new ClassPathXmlApplicationContext(“CDPlayerConfig.xml”)。

2.java代码装配
1)添加代码CDPlayer.java

package soundsystem;public class CDPlayer {private CompactDisc cd;CDPlayer(CompactDisc icd){cd = icd;}void play(){cd.play();}
}

2)修改代码CDPlayerConfig.java

package soundsystem;import org.springframework.context.annotation.*;@Configuration
public class CDPlayerConfig {
    @Beanpublic CompactDisc sgtPeppers(){return new SgtPeppers();}@Beanpublic CDPlayer cdPlayer(){return new CDPlayer(sgtPeppers());}
}

删去了注解@ComponentScan,@Bean注解告诉spring此方法会产生一个Bean,其中new CDPlayer(sgtPeppers())表示以方法sgtPeppers产生的Bean为参数,sgtPeppers方法只会被调用一次;
把cdPlayer方法修改成如下代码,效果一样,会自动寻找一个CompactDisc类型的Bean,传给方法cdPlayer;

    @Beanpublic CDPlayer cdPlayer(CompactDisc  cd){return new CDPlayer(cd);}

3)Run as Junit Test

package soundsystem;import org.junit.Test;
import static  org.junit.Assert.*;
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)
//@ContextConfiguration(locations = {
   "classpath:CDPlayerConfig.xml"})
public class CDPlayerTest {@Autowiredprivate CDPlayer cd;@Testpublic void cdShouldNotBeNull(){assertNotNull(cd);cd.play();}
}

测试OK。

3.使用xml装配
1)在xml中定义Bean,CDPlayerConfig.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="compactDisc" class="soundsystem.SgtPeppers"/><bean id="cdPlayer" class="soundsystem.CDPlayer"><constructor-arg ref="compactDisc"/>
</bean>
</beans>

配置bean compactDisc和cdPlayer,其中cdPlayer注入了compactDisc;
2)Run as Junit Test
CDPlayerTest.java:

package soundsystem;import org.junit.Test;
import static  org.junit.Assert.*;
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)
@ContextConfiguration(locations = {
   "classpath:CDPlayerConfig.xml"})
public class CDPlayerTest {@Autowiredprivate CDPlayer cd;@Testpublic void cdShouldNotBeNull(){assertNotNull(cd);cd.play();}
}

测试OK。
xml配置,除了构造时注入,还可以设置属性时注入。

  相关解决方案