PS:@Component表明组件类
package soundsystem;import org.sPRingframework.stereotype.Component;@Componentpublic class SgtPeppers implements CompactDisc{ private String title = "Sgt.Pepper's Lonely Hearts Club Bands"; private String artist = "The Beatles"; public void play() { System.out.println("Playing "+title+" by "+artist); }}PS:@ComponentScan注解启用了组件扫描,如果没有其他配置的话,@ComponentScan默认会扫描与配置类相同的包以及它的子包。这里是基于java的配置启用组件扫描
package soundsystem;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScanpublic class CDPlayerConfig {}PS:@Autowired注解表明自动装配
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 soundsystem.CDPlayerConfig;import soundsystem.CompactDisc;import static org.junit.Assert.assertNotNull;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes=CDPlayerConfig.class)public class CDPlayerTest { @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() { assertNotNull(cd); }}PS:applicationContext.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 启用组件扫描 --> <context:component-scan base-package="soundsystem"/></beans>PS:一样
package soundsystem;public interface CompactDisc { void play();}PS:一样
package soundsystem;import org.springframework.stereotype.Component;@Componentpublic class SgtPeppers implements CompactDisc{ private String title = "Sgt.Pepper's Lonely Hearts Club Bands"; private String artist = "The Beatles"; public void play() { System.out.println("Playing "+title+" by "+artist); }}PS:变为 @ContextConfiguration(locations = { “classpath*:/applicationContext.xml”})
import static org.junit.Assert.assertNotNull;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath*:/applicationContext.xml"})public class CDPlayerTest { @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() { assertNotNull(cd); }}(1)默认bean的ID为sgtPeppers
@Componentpublic class SgtPeppers implements CompactDisc{ private String title = "Sgt.Pepper's Lonely Hearts Club Bands"; private String artist = "The Beatles"; public void play() { System.out.println("Playing "+title+" by "+artist); }}(2)bean的ID为Leo
@Component("Leo")public class SgtPeppers implements CompactDisc{ private String title = "Sgt.Pepper's Lonely Hearts Club Bands"; private String artist = "The Beatles"; public void play() { System.out.println("Playing "+title+" by "+artist); }}(3)bean的ID为Leo
@Named("Leo")public class SgtPeppers implements CompactDisc{ private String title = "Sgt.Pepper's Lonely Hearts Club Bands"; private String artist = "The Beatles"; public void play() { System.out.println("Playing "+title+" by "+artist); }}(1)扫描soundsystem
@Configuration@ComponentScan("soundsystem")public class CDPlayerConfig {}(2)扫描soundsystem,video
@Configuration@ComponentScan(basePackages={"soundsystem","video"})public class CDPlayerConfig {}(3)扫描CDPlayer类,DVDPlayer类所在的包
@Configuration@ComponentScan(basePackageClasses = {CDPlayer.class,DVDPlayer.class})public class CDPlayerConfig {}PS:任何方法都可以实现自动装配
package soundsystem;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class CDPlayer implements MediaPlayer { private CompactDisc cd; @Autowired public CDPlayer(CompactDisc cd) { this.cd = cd; } @Autowired public void setCompactDisc(CompactDisc cd) { this.cd = cd; } @Autowired public void insertDisc(CompactDisc cd) { this.cd = cd; } public void play() { cd.play(); }}PS:Java配置组件扫描
package soundsystem;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScanpublic class CDPlayerConfig {}package soundsystem;public interface CompactDisc { void play();}package soundsystem;public interface MediaPlayer { public void play();}package soundsystem;import org.springframework.stereotype.Component;/** * Created by adc333 on 2017/2/3. */@Componentpublic class SgtPeppers implements CompactDisc{ private String title = "hello"; private String artist = " world"; public void play() { System.out.print(title+artist); }}PS:控制台输出比较,这里简单输出hello world
import org.junit.Rule;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;import soundsystem.CDPlayerConfig;import soundsystem.CompactDisc;import soundsystem.MediaPlayer;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertNotNull;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = CDPlayerConfig.class)public class CDPlayerTest { @Rule public final StandardOutputStreamLog log = new StandardOutputStreamLog(); @Autowired private MediaPlayer player; @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() { assertNotNull(cd); } @Test public void play() { player.play(); assertEquals("hello world",log.getLog()); }}(1)默认bean的ID为sgtPeppers
package soundsystem;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class CDPlayerConfig { @Bean public CompactDisc sgtPeppers() { return new SgtPeppers(); }}(2)bean的ID为Leo
package soundsystem;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class CDPlayerConfig { @Bean(name="Leo") public CompactDisc sgtPeppers() { return new SgtPeppers(); }}(1)bean的ID为soundsystem.SgtPeppers#0,如果声明了另外一个SgtPeppers,同样没有明确标识,bean的ID为soundsystem.SgtPeppers#1
<bean class="soundsystem.SgtPeppers"/>(2)bean的ID为compactDisc
<bean id="compactDisc" class="soundsystem.SgtPeppers"/>(1)
<bean id="cdPlayer" class="soundsystem.CDPlayer"> <constructor-arg ref="compactDisc"/></bean>(2)
<bean id="cdPlayer" class="soundsystem.CDPlayer" c:cd-ref="compactDisc" />(1)
<bean id="compactDisc" class="soundsystem.BlankDisc"> <constructor-arg value="hello"/> <constructor-arg value="world"/></bean>(2)
<bean id="compactDisc" class="soundsystem.BlankDisc" c:_title="hello" c:_artist="world" />(1)
<bean id="cdPlayer" class="soundsystem.CDPlayer"> <property name="compactDisc" ref="compactDisc"/></bean>(2)
<bean id="cdPlayer" class="soundsystem.CDPlayer" p:compactDisc-ref="compactDisc" />(1)
<bean id="compactDisc" class="soundsystem.BlankDisc"> <property name="title" value="hello"/> <property name="artist" value="world"/> <property name="tracks"> <list> <value>11111</value> <value>22222</value> <value>33333</value> <value>44444</value> <value>55555</value> <list> </property></bean>(2)
<bean id="compactDisc" class="soundsystem.BlankDisc" p:title="hello" p:artist="world"> <property name="tracks"> <list> <value>11111</value> <value>22222</value> <value>33333</value> <value>44444</value> <value>55555</value> <list> </property></bean>applicationContext.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 设置属性注入bean --> <bean id="compactDisc" class="soundsystem.BlankDisc"> <property name="title" value="hello"/> <property name="artist" value="world"/> <property name="tracks"> <list> <value>11111</value> <value>22222</value> <value>33333</value> <value>44444</value> <value>55555</value> </list> </property> </bean> <!-- 构造器注入bean --> <bean id="cdPlayer" class="soundsystem.CDPlayer"> <constructor-arg ref="compactDisc"/> </bean></beans>新闻热点
疑难解答