实验环境:
1、eclipse开发工具
https://www.eclipse.org/downloads/
2、Java JDK(IDE有自己的编译环境,所以只需安装JRE即可)
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
3、Spring框架 spring-framework-4.0.0.RELEASEdist.zip
http://repo.spring.io/release/org/springframework/spring/
4、commons-logging下载 commons-logging-1.2-bin.zip
http://commons.apache.org/proper/commonslogging/download_logging.cgi
5、JUnit4框架(使用JUnit4.12) junit-4.12.jar
http://maven.outofmemory.cn/junit/junit/4.1/
6、hamcrest框架(JUnit4.12中不包含hamcrest框架所以需要单独下载)
hamcrest-all-1.3.jar
http://repo2.maven.org/maven2/org/hamcrest/hamcrest-all/1.3/
在Spring中,对象无需自己查找或创建与其所关联的其他对象,容器负责把需要相互协作的对象引用赋予各个对象。
装配(wiring):创建应用对象之间协作关系的行为。
配置Spring容器的三种机制如下:
- 在XML中进行显示配置
- 在Java中进行显示配置
- 隐式的bean发现机制和自动装配
Spring从两个角度实现自动化装配:
1、组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean
2、自动装配(autowiring):Spring自动满足bean之间的依赖
音响系统的例子
一、Spring环境搭建
1、新建Java Project
File–>New–>Project–>Java Project
2、导入相关的包
右键项目名称SoundSystem–>Bulid Path–>Configure Bulid Path
将spring-framework-4.0.0.RELEASEdist.zip解压到本地文件夹,点击“Add External JARs”后在弹出的对话框中选择spring-framework-4.0.4.RELEASE–>libs,简单起见将libs文件夹中的.jar文件全部选择加入。
再依次加入commons-logging-1.2.jar、junit-4.12.jar和hamcrest-all-1.3.jar。
二、在Java中建立CD,定义CompactDisc接口
右键src–>New–>Package
右键soundsystem–>New–>Interface
CompactDisc.java代码如下:
package soundsystem;
public interface CompactDisc {
void play();
}
三、类SgtPeppers实现CompactDisc接口
@Component注解表明该类会作为组件类,并告知Spring要为这个类创建bean。
SgtPeppers代码如下:
package soundsystem;
import org.springframework.stereotype.Component;
@Component //表示该类作为组件类
public class SgtPeppers implements CompactDisc{
private String title = "Stg.Pepper's Lonely Hearts Club Band";
private String artist = "The Beatles";
public void play() {
System.out.println("Playing " + title + " by " + artist);
}
}
四、启动组件扫描
组件扫描默认是不启动的,需要显示配置Spring,命令它去寻找带有@Component注解的类并为其创建bean。
CDPlayerConfig.java代码如下:
package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class CDPlayerConfig {
}
类CDPlayerConfig通过Java代码定义了Spring装配规则,类中并没有显示声明任何bean,通过使用@ComponentScan注解启用组件扫描。如果没有其他配置的话,默认扫描与配置类相同的包即本例中的soundsystem。
若要扫描其他的包,在@ComponentScan的value属性中指明包的名称:
@Configuration
@ComponentScan(“soundsystem”)
若要表明设置的是基础包,可通过basePackages属性配置:
@Configuration
@ComponentScan(basePackages=”soundsystem”)
若要设置多个基础包:
@Configuration
@ComponentScan(basePackages={“soundsystem”,”video”})
也可以将其指定为包中所包含的类或接口,这些类所在的包会作为组件扫描的基础包:
@Configuration
@ComponentScan(basePackageClasses={CDPlayer.class,DVDPlayer.class})
五、测试组件扫描
创建简单的JUnit测试,它会创建Spring上下文,并判断CompactDisc是否有创建。
CDPlayerTest.java代码如下:
package 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 CDPlayerTest {
@Autowired
private CompactDisc cd;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
}
运行程序结果如下:以测试成功的颜色显示
第一个使用Spring框架的例子终于运行成功啦(^o^)/