目录结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.geyao</groupId><artifactId>spring01geyao</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.13.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>3.2.0.RELEASE</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.4.RELEASE</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.4.RELEASE</version><scope>test</scope></dependency></dependencies>
</project>
applictioncontext.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:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--bean元素:描述当前的对象需要由spring容器管理id属性:标识对象 未来在应用程序中可以根据id获取对象class对象:被管理的对象的全名--><bean id="music1" class="com.geyao.demo.soundSystem.Music"><constructor-arg value="i do 1"/><constructor-arg value="270"/></bean><bean id="music2" class="com.geyao.demo.soundSystem.Music"><constructor-arg value="i do 2"/><constructor-arg value="270"/></bean><bean id="music3" class="com.geyao.demo.soundSystem.Music"><constructor-arg value="i do 3"/><constructor-arg value="270"/></bean>
<bean id="compactDisc1" class="com.geyao.demo.soundSystem.CompactDisc"><constructor-arg name="title" value="I Do"/><constructor-arg name="artist" value="莫文蔚"/><constructor-arg name="tracks"><list><!--<value>i do 1</value><value>i do 2</value><value>i do 3</value>--><ref bean="music1"/><ref bean="music2"/><ref bean="music3"/></list></constructor-arg>
</bean>
</beans><!-- <bean name="compactDisc2" class="com.geyao.demo.soundSystem.CompactDisc"></bean><bean name="cdPlayer1" class="com.geyao.demo.soundSystem.CDPlayer"><constructor-arg ref="compactDisc1"/></bean><bean name="cdPlayer2" class="com.geyao.demo.soundSystem.CDPlayer" c:cd-ref="compactDisc2"></bean>-->
log4j.properties
### 设置###
log4j.rootLogger = ERROR,stdout### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.category.org.springframework.beans.factory=ERROR
CompactDisc类
import com.geyao.demo.soundSystem.CompactDisc;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ApplicationSpring {public static void main(String[] args){System.out.println("Application ");ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");CompactDisc cd=context.getBean(CompactDisc.class);cd.play();}
}
Music类
package com.geyao.demo.soundSystem;public class Music {private String title;private Integer duration;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public Integer getDuration() {return duration;}public void setDuration(Integer duration) {this.duration = duration;}public Music() {super();}public Music(String title, Integer duration) {this.title = title;this.duration = duration;}
}
cdplayer类
package com.geyao.demo.soundSystem;public class CDPlayer {private CompactDisc cd;public CDPlayer() {super();System.out.println("cdplayer的构造方法"+this.toString());}public CDPlayer(CompactDisc cd){this.cd=cd;System.out.println("cdplayer的有残构造方法");}public void play(){cd.play();}
}
ApplicationSpring类
import com.geyao.demo.soundSystem.CompactDisc;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ApplicationSpring {public static void main(String[] args){System.out.println("Application ");ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");CompactDisc cd1=(CompactDisc) context.getBean("compactDisc1");CompactDisc cd2=(CompactDisc) context.getBean("compactDisc2");cd1.play();cd2.play();}
}
appTest类
package com.geyao.demo.souundSystem;import com.geyao.demo.soundSystem.CompactDisc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AppTest {@Autowiredprivate CompactDisc compactDisc1;//@Autowired//private CompactDisc compactDisc12;//@Autowired//@Qualifier("compacDisc2")// private CompactDisc compactDisc3;@Testpublic void testApp(){compactDisc1.play();// compactDisc12.play();// compactDisc3.play();}}
CDplayerTest
package com.geyao.demo.souundSystem;import com.geyao.demo.soundSystem.CDPlayer;
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("classpath:applicationContext.xml")
public class CDplayerTest {@Autowiredprivate CDPlayer cdPlayer1;@Autowiredprivate CDPlayer cdPlayer2;@Testpublic void test01(){cdPlayer1.play();}@Testpublic void test02(){cdPlayer1.play();}}
运行结果
s三个方法的构造函数
播放cd音乐com.geyao.demo.soundSystem.CompactDisc@4d1c005eI Do莫文蔚
音乐i do 1时长270
音乐i do 2时长270
音乐i do 3时长270