需要引入的ja包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
1、自动注入属性
定义CD播放器对CD所能进行的操作接口类
package test.aop; public interface CompactDisc { void play(); }
定义CompactDisc接口的实现类SgtPeppers,增加component注解,将SgtPeppers类作为组件类
package test.aop; import org.springframework.stereotype.Component; @Component public class SgtPeppers implements CompactDisc { private String title = "Sgt. Pepper's Lonely Hearts Club Band"; private String artist = "The Beatles"; public void play() { System.out.println("Playing" + title + " by " + artist); } }
Spring中组件扫描默认是不启用的,需要显示配置Spring,让其去寻找带有@Component注解的类, @ComponentScan在没有显示指定参数情况会默认扫描与配置类相同的包test.aop,
即Spring会扫描这个包以及这个包下的所有子包,查找带有@Component注解的类。这样就能发现CompactDisc接口及其实现类,并为其创建一个bean。
package test.aop; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan public class CDPlayerConfig { }
测试扫描组件能够发现CompactDisc
package test.aop; 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 static junit.framework.TestCase.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=CDPlayerConfig.class) public class CDPlayerTest { @Autowired private CompactDisc cd; @Test public void dcShouldNotBeNull() { assertNotNull(cd); } }
2、 自动注入方法
创建MediaPlayer接口及其实现类CDPlayer,实现类CDPlayer需要注入CompactDisc
package test.aop; public interface MediaPlayer { void play(); }
package test.aop; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class CDPlayer implements MediaPlayer { private CompactDisc cd; @Autowired public CDPlayer(CompactDisc cd) { this.cd = cd; } public void play() { cd.play(); } }
测试类
package test.aop;
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 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 CompactDisc cd;
@Autowired
private MediaPlayer player;
@Test
public void dcShouldNotBeNull() {
assertNotNull(cd);
}
@Test
public void player() {
player.play();
assertEquals("PlayingSgt. Pepper's Lonely Hearts Club Band by The Beatles ", log.getLog());
}
}
需要引入jar包
<!-- https://mvnrepository.com/artifact/com.github.stefanbirkner/system-rules --> <dependency> <groupId>com.github.stefanbirkner</groupId> <artifactId>system-rules</artifactId> <version>1.16.0</version> </dependency>