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:變?yōu)?@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:任何方法都可以實現(xiàn)自動裝配
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>新聞熱點
疑難解答