松原建设小学网站,怎样提高网站权重,百度商桥怎样绑定网站,免费软件网站大全一、简介
Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系。Spring具有非常大的灵活性#xff0c;它提供了三种主要的装配机制#xff1a;
在XML中进行显示配置#xff1b;在Java中进行显示配置#xff1b;隐式的bean发现机制和自动装配。
二、…一、简介
Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系。Spring具有非常大的灵活性它提供了三种主要的装配机制
在XML中进行显示配置在Java中进行显示配置隐式的bean发现机制和自动装配。
二、在XML中进行显示配置
1. 声明一个简单的bean
1. 创建CD接口
public interface CompactDisc {void play();
}2. 创建NowAndThen类
public class NowAndThen implements CompactDisc{private String title Now And Then.;private String artist The Beatles;Overridepublic void play() {System.out.println(Playing title by artist);}}3. 在bean.xml文件中配置
!-- 通过class属性来声明一个简单的bean此时因为没有指定IDbean的ID将会是com.shiftycat.soundsystem.NowAndThen#0 --
bean classcom.shiftycat.soundsystem.NowAndThen/
!-- 使用id属性可以用来指定ID --
bean idCompactDisc classcom.shiftycat.soundsystem.NowAndThen/4. 进行测试
Test
public void compactDiscTest() {ClassPathXmlApplicationContext classPathXmlApplicationContext new ClassPathXmlApplicationContext(bean.xml);CompactDisc compactDisc classPathXmlApplicationContext.getBean(CompactDisc.class);compactDisc.play();
}5. 测试结果
Playing Now And Then. by The Beatles2. 借助构造器注入初始化bean
构造器注入有两种方案
constructor-arg元素使用Spring 3.0所引入的c-命名空间
1. 创建CD接口
public interface CompactDisc {void play();
}2. 创建NowAndThen类
public class NowAndThen implements CompactDisc{private String title Now And Then.;private String artist The Beatles;Overridepublic void play() {System.out.println(Playing title by artist);}}3. 创建MediaPlayer接口
public interface MediaPlayer {void play();
}4. 创建CDPlayer类
public class CDPlayer implements MediaPlayer{private CompactDisc cd;// 构造器注入public CDPlayer(CompactDisc cd) {this.cd cd;}Overridepublic void play() {System.out.println(Loading...);cd.play();}
}
5. 在bean.xml文件中配置
bean idCompactDisc classcom.shiftycat.soundsystem.NowAndThen/\
!-- 第一种配置方案constructor-arg元素 --
bean idcdPlayer classcom.shiftycat.soundsystem.CDPlayerconstructor-arg refCompactDisc/
/bean
!-- 第二种配置方案使用Spring 3.0所引入的c-命名空间 --
!-- c:构造器参数名-ref“要注入的bean的ID” --
bean idcdPlayer classcom.shiftycat.soundsystem.CDPlayer c:cd-refCompactDisc/
!-- 或者将参数的名称替换为_0或者_ --
bean idcdPlayer classcom.shiftycat.soundsystem.CDPlayer c:_0-refCompactDisc /
bean idcdPlayer classcom.shiftycat.soundsystem.CDPlayer c:_-refCompactDisc /6. 进行测试
Test
public void cdPlayerTest() {ClassPathXmlApplicationContext classPathXmlApplicationContext new ClassPathXmlApplicationContext(bean.xml);CDPlayer cdPlayer classPathXmlApplicationContext.getBean(CDPlayer.class);cdPlayer.play();}7. 测试结果
Loading...
Playing Now And Then. by The Beatles将字面量注入到构造器中
1. 创建BlackDisc类
public class BlackDisc implements CompactDisc{private String title;private String artist;public BlackDisc(String title, String artist) {this.title title;this.artist artist;}Overridepublic void play() {System.out.println(Playing title by artist);}
}2. 在bean.xml中进行配置
bean idblackDisc classcom.shiftycat.soundsystem.BlackDiscconstructor-arg valueNow And Then. /constructor-arg valueThe Beatles /
/beanbean idblackDisc classcom.shiftycat.soundsystem.BlackDiscc:titleNow And Then.c:artistThe Beatles
/bean idblackDisc classcom.shiftycat.soundsystem.BlackDiscc:_0Now And Then.c:_1The Beatles
/3. 进行测试
Test
public void blackDiscTest() {ClassPathXmlApplicationContext classPathXmlApplicationContext new ClassPathXmlApplicationContext(bean.xml);BlackDisc blackDisc classPathXmlApplicationContext.getBean(BlackDisc.class);blackDisc.play();
}4. 测试结果
Playing Now And Then. by The BeatlesP.S. 装配集合
public class BlackDisc implements CompactDisc {private String title;private String artist;private ListString tracks;public BlackDisc(String title, String artist, ListString tracks) {this.title title;this.artist artist;this.tracks tracks;}Overridepublic void play() {System.out.println(Playing title by artist);for (String track : tracks) {System.out.println(-Track: track);}}
}bean idblackDisc classcom.shiftycat.soundsystem.BlackDiscconstructor-arg valueNow And Then. /constructor-arg valueThe Beatles /constructor-arglistvalueI know its true/valuevalueIts all because of you/valuevalueAnd if I make it through/valuevalueIts all because of you/valuevalueAnd now and then/value/list/constructor-arg
/beanPlaying Now And Then. by The Beatles
-Track: I know its true
-Track: Its all because of you
-Track: And if I make it through
-Track: Its all because of you
-Track: And now and then3. 设置属性初始化bean
一般而言对于强依赖使用构造器注入对可选性的依赖使用属性注入。
1. 在CDPlayer类中设置setter方法
public class CDPlayer implements MediaPlayer{private CompactDisc cd;public void setCompactDisc(CompactDisc cd) {this.cd cd;}Overridepublic void play() {System.out.println(Loading...);cd.play();}
}
2. 在bean.xml文件中进行配置
!-- 可以使用property进行配置--
bean idcdPlayer classcom.shiftycat.soundsystem.CDPlayerproperty namecompactDisc refcompactDisc/
/bean
!-- 或者使用p-命名空间性配置--
bean idcdPlayer classcom.shiftycat.soundsystem.CDPlayerp:compactDisc-refcompactDisc/3. 进行测试
Test
public void cdPlayerTest() {ClassPathXmlApplicationContext classPathXmlApplicationContext new ClassPathXmlApplicationContext(bean.xml);CDPlayer cdPlayer classPathXmlApplicationContext.getBean(CDPlayer.class);cdPlayer.play();
}4. 测试结果
Loading...
Playing Now And Then. by The Beatles将字面量注入到属性中
1. 在WhiteDisc类中设置setter方法
public class WhiteDisc implements CompactDisc{private String title;private String artist;private ListString tracks;public String getTitle() {return title;}public void setTitle(String title) {this.title title;}public String getArtist() {return artist;}public void setArtist(String artist) {this.artist artist;}public ListString getTracks() {return tracks;}public void setTracks(ListString tracks) {this.tracks tracks;}Overridepublic void play() {System.out.println(Playing title by artist);for (String track : tracks) {System.out.println(-Track: track);}}
}
2. 在bean.xml文件中进行配置
bean idwhileDisc classcom.shiftycat.soundsystem.WhiteDiscproperty nameartist valueNow And Then./property nametitle valueThe Beatles /property nametrackslistvalueI know its true/valuevalueIts all because of you/valuevalueAnd if I make it through/valuevalueIts all because of you/valuevalueAnd now and then/value/list/property
/bean
!--或者--
bean idwhileDisc classcom.shiftycat.soundsystem.WhiteDiscp:artistNow And Then.p:titleThe Beatlesproperty nametrackslistvalueI know its true/valuevalueIts all because of you/valuevalueAnd if I make it through/valuevalueIts all because of you/valuevalueAnd now and then/value/list/property
/bean
!--或者--
bean idwhileDisc classcom.shiftycat.soundsystem.WhiteDiscp:artistNow And Then.p:titleThe Beatlesp:tracks-reftracks/beanutil:list idtracksvalueI know its true/valuevalueIts all because of you/valuevalueAnd if I make it through/valuevalueIts all because of you/valuevalueAnd now and then/value
/util:list3. 进行测试
Test
public void whiteDiscTest() {ClassPathXmlApplicationContext classPathXmlApplicationContext new ClassPathXmlApplicationContext(bean.xml);WhiteDisc whiteDisc classPathXmlApplicationContext.getBean(WhiteDisc.class);whiteDisc.play();
}4. 测试结果
Playing The Beatles by Now And Then.
-Track: I know its true
-Track: Its all because of you
-Track: And if I make it through
-Track: Its all because of you
-Track: And now and then三、在Java中进行显式配置
1. 声明简单的bean
1. 创建配置类
Configuration
public class CDPlayerConfig {}2. 声明简单的bean
Configuration
public class CDPlayerConfig {Beanpublic CompactDisc nowAndThen() {return new nowAndThen();}}2. 借助JavaConfig实现注入
Configuration
//ComponentScan(basePackages com.shiftycat.soundsystem) //在Spring中启动组件扫描默认扫描该类所在包及这个包下的所有子包
public class CDPlayerConfig {Beanpublic CompactDisc nowAndThen() {return new nowAndThen();}// 方法一: 最简单的方法是引用创建bean的方法Beanpublic CDPlayer cdPlayer() {return new CDPlayer(nowAndThen());}// 方法二: 请求一个CompactDisc作为参数将CompactDisc注入到CDPlayer的构造器中// 而且不用明确引用CompactDisc的Bean方法。Beanpublic CDPlayer cdPlayer(CompactDisc compactDisc) {return new CDPlayer(compactDisc);}// 方法三: 通过setter方法注入CompactDiscBeanpublic CDPlayer cdPlayer(CompactDisc compactDisc) {CDPlayer cdPlayer new CDPlayer();cdPlayer.setCd(compactDisc);return cdPlayer;}
}四、隐式的bean发现机制和自动装配
1. 自动化装配bean
Spring通过两个方面实现自动化装配
组件扫描component scanningSpring会自动发现应用上下文所创建的bean。自动装配autowiringSpring自动满足bean之间的依赖。
2. 步骤
1. 创建可被发现的bean
public interface CompactDisc {void play();
}public class nowAndThen implements CompactDisc{private String title Now And Then.;private String artist The Beatles;Overridepublic void play() {System.out.println(Playing title by artist);}
}public interface MediaPlayer {void play();
}2. 开启组件扫描
Configuration
ComponentScan(basePackages com.shiftycat.soundsystem) //在Spring中启动组件扫描默认扫描该类所在包及这个包下的所有子包
public class CDPlayerConfig {}?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdcontext:component-scan base-packagecom.shiftycat.soundsystem /
/beansAutowired
private CompactDisc compactDisc;Test
public void compactDiscTest() {compactDisc.play();
}Playing Now And Then. by The Beatles3. 通过为bean添加注解实现自动装配
不管是构造器、Setter方法还是其他的方法Spring都会尝试满足方法参数上所声明的依赖。假如有且只有一个bean匹配以来需求的话那么这个bean将会被装配进来。Component
public class CDPlayer implements MediaPlayer{private CompactDisc cd;Autowiredpublic CDPlayer(CompactDisc cd) {this.cd cd;}Autowiredpublic void setCd(CompactDisc cd) {this.cd cd;}Overridepublic void play() {System.out.println(Loading...);cd.play();}
}Autowired
private CDPlayer cdPlayer;
Test
public void cdPlayerTest() {cdPlayer.play();
}Loading...
Playing Now And Then. by The Beatles五、导入和混合配置
1. 在JavaConfig中引用XML配置
假如现存由两个config文件和一个XML文件如何实现在一个JavaConfig文件中引用两个config文件并且在其中引入XML配置文件。
1. 配置文件1
public interface CompactDisc {void play();
}Component
public class NowAndThen implements CompactDisc {private String title Now And Then.;private String artist The Beatles;Overridepublic void play() {System.out.println(Playing title by artist);}
}Configuration
public class CDConfig {Beanpublic CompactDisc compactDisc() {return new NowAndThen();}
}2. 配置文件2
public interface MediaPlayer {void play();
}Component
public class CDPlayer implements MediaPlayer {private CompactDisc cd;public CDPlayer(CompactDisc cd) {this.cd cd;}Overridepublic void play() {System.out.println(Loading...);cd.play();}
}Configuration
//Import(CDConfig.class)
public class CDPlayerConfig {Beanpublic CDPlayer cdPlayer(CompactDisc compactDisc) {return new CDPlayer(compactDisc);}
}3. 配置文件3
public class BlackDisc implements CompactDisc {private String title;private String artist;private ListString tracks;public String getTitle() {return title;}public void setTitle(String title) {this.title title;}public String getArtist() {return artist;}public void setArtist(String artist) {this.artist artist;}public ListString getTracks() {return tracks;}public void setTracks(ListString tracks) {this.tracks tracks;}Overridepublic void play() {System.out.println(Playing title by artist);for (String track : tracks) {System.out.println(-Track: track);}}
}?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:chttp://www.springframework.org/schema/cxmlns:phttp://www.springframework.org/schema/pxmlns:utilhttp://www.springframework.org/schema/utilxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdbean idblackDisc classcom.shiftycat.importAndBlend.BlackDiscp:artistNow And Then.p:titleThe Beatlesp:tracks-reftracks/beanutil:list idtracksvalueI know its true/valuevalueIts all because of you/valuevalueAnd if I make it through/valuevalueIts all because of you/valuevalueAnd now and then/value/util:list/beans4. 主要的Config文件
Configuration
Import({CDConfig.class, CDPlayerConfig.class})
ImportResource(classpath:cd-config.xml)
public class SoundSystemConfig {}5. 进行测试
Test
public void JavaConfigTest() {AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext();context.register(SoundSystemConfig.class);context.refresh();System.out.println(BlackDisc: );CompactDisc blackDisc context.getBean(BlackDisc.class);blackDisc.play();System.out.println(\nnowAndThen: );CompactDisc nowAndThen context.getBean(NowAndThen.class);nowAndThen.play();System.out.println(\nCDPlayer: );CDPlayer player context.getBean(CDPlayer.class);player.play();
}6. 测试结果
BlackDisc:
Playing The Beatles by Now And Then.
-Track: I know its true
-Track: Its all because of you
-Track: And if I make it through
-Track: Its all because of you
-Track: And now and thennowAndThen:
Playing Now And Then. by The BeatlesCDPlayer:
Loading...
Playing Now And Then. by The Beatles2. 在XML配置中引用JavaConfig
与在JavaConfig中引用XML配置一样我们可以使用更高层次的配置文件这个文件不声明任何的bean只是负责将两个或更多的配置组合起来。
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!-- 用来导入JavaConfig类配置到Xml文件中 --bean classcom.shiftycat.importAndBlend.CDConfig /!-- 用来导入xml文件配置 --import resourcecd-config.xml//beans《Spring实战(第4版)》