您的位置:首页 > 编程语言 > Java开发

Spring基础_在XML中进行显式配置<三>

2017-10-27 13:53 597 查看
链接:

1.隐式的bean发现机制和自动装配

2.在Java中进行显式配置

代码

1.CompactDisc.java
package one;

//光盘接口
public interface CompactDisc {
void play();
}
2.SgtPeppers.java
package one;

import java.util.List;

//光盘类
public class SgtPeppers implements CompactDisc {
private String title;//唱片名
private String artist;//艺术家
private List<String> tracks;//轨道:一个唱片中应该有多首歌曲,位于不同的光盘轨道

//构造器注入
//在xml中通过constructor-arg对构造器参数进行注入
public SgtPeppers(String title,String artist,List<String> tracks) {
System.out.println("调用1");
this.title=title;
this.artist=artist;
this.tracks=tracks;
}

@Override
public void play() {
System.out.println("playing"+title+"by"+artist);
for(String a:tracks){
System.out.println("歌曲详细:"+a);
}
}
}


3.MediaPlayer.java

package one;

//媒体播放器接口
public interface MediaPlayer {
void play();
}
4.CDPlayer.java
package one;

//cd播放机实现媒体播放器接口
public class CDPlayer implements MediaPlayer{
private CompactDisc cd;
//构造器注入
/*public CDPlayer(CompactDisc cd){
System.out.println("调用2");
this.cd=cd;
}*/

//属性注入
public void setCd(CompactDisc cd) {
System.out.println("调用2");
this.cd = cd;
}

@Override
public void play(){
cd.play();
}
}


5.Test.java
package one;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
MediaPlayer mp=(MediaPlayer)ac.getBean("cdPlayer");
mp.play();
}
}
6.spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!--$$$一. 在Spring XML配置中,只有一种声明bean的方式:使用<bean>元素并指定class属性。 Spring会从这里
获取必要的信息来创建bean。 我们再次使用<constructor-arg>元素进行构造器参数的注入。但是这一次我们
没有使用“ref”属性来引用其他的bean,而是使用了value属性,通过该属性表明给定的值要以字面量的形式注入到构造器之中 -->
<bean id="compactDisc" class="one.SgtPeppers">
<constructor-arg value="怒放的生命"/>
<constructor-arg value="汪峰"/>
<constructor-arg>
<!-- list可以替换为set,list和set区别在于如果spring创建的要装配的集合是set类型,装配时重复
的值会被过滤,顺序也得不到保证 -->
<list>
<value>我要飞的更高</value>
<value>我要飞的更快</value>
<value>我要飞的更强</value>
<value>我要飞的更远</value>
<value>我要飞的更冷</value>
</list>
</constructor-arg>
</bean>
<!-- $$二.如果SgtPeppers光盘类中的三个属性是通过set方式来指定(属性注入),spring的p-命名空间
无法装配list,可以查看spring的util-命名空间 -->
<!-- <bean id="compactDisc" class="one.SgtPeppers"
p:title="怒放的生命"
p:artist="汪峰">
<property name="tracks">
<list>
<value>我要飞的更高</value>
<value>我要飞的更快</value>
<value>我要飞的更强</value>
<value>我要飞的更远</value>
<value>我要飞的更冷</value>
</list>
</property>
</bean> -->

<!-- $$一.XML中声明DI通过构造器注入有两种基本方案: -->
<!-- 1.1通过constructor-arg元素实现 -->
<!-- 现在已经声明了SgtPeppers bean,并且SgtPeppers类实现了CompactDisc接口,所以实际上我们已经有
了一个可以注入到CDPlayerbean中的bean。我们所需要做的就是在XML中声明CDPlayer并通过ID引用SgtPeppers: -->
<!-- <bean id="cdPlayer" class="one.CDPlayer">
<constructor-arg ref="compactDisc"/>
</bean> -->
<!-- 1.2使用spring3.0引入的c-命名空间  c:cd中的cd是构造器的参数名,这样不好-->
<!-- <bean id="cdPlayer" class="one.CDPlayer" c:cd-ref="compactDisc"/> -->
<!-- 1.3.这儿0表示第一个参数,比上面构造器参数名称要好 -->
<!-- <bean id="cdPlayer" class="one.CDPlayer" c:0-ref="compactDisc"/> -->

<!--$$二.XML中声明DI通过属性的setter方法注入: -->
<!-- 2.1 使用property元素为属性setter方法注入-->
<!-- <bean id="cdPlayer" class="one.CDPlayer">
<property name="cd" ref="compactDisc"/>
</bean> -->
<!-- 2.2 声明p-命名空间,作为property元素的替代方案,和上面c作为contructor-arg替代方案一样.cd是属性名 -->
<bean id="cdPlayer" class="one.CDPlayer" p:cd-ref="compactDisc"/>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐