-
-
Save dantin/277c0200caf03e4c7141 to your computer and use it in GitHub Desktop.
Spring in Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
注释@Component | |
注释不会自动生效,使用@ComponentScan或<context:component-scan>开启扫描 | |
类似注释: | |
@Component("identifierName"),recommend | |
@Name("identifierName"),可互用,于@Component略有不同 | |
制定特定的扫描包 | |
@ComponentScan(basePackages="soundsystem") // 扫描单个包 | |
@ComponentScan(basePackages={"soundsystem", "video"}) // 扫描多个包 | |
@ComponentScan(basePackageClasses={CDPlayer.class, DVDPlayer.class}) // 扫描类所在包 | |
@Autowired,通过注释形式wire对象 | |
@Autowired(required=false),未发现匹配的对象时,不抛出异常,会产生NullPointerException | |
@Inject,可互用,于@Autowired略有不同 | |
--- | |
使用于构造函数 | |
@Autowired | |
public CDPlayer(CompactDisc cd) { | |
this.cd = cd; | |
} | |
使用于set方法 | |
@Autowired | |
public void setCompactDisc(CompactDisc cd) { | |
this.cd = cd; | |
} | |
使用在其他方法上 | |
@Autowired | |
public void insertDisc(CompactDisc cd) { | |
this.cd = cd; | |
} | |
CompactDisc.java | |
--- | |
package soundsystem; | |
public interface CompactDisc { | |
void play(); | |
} | |
SgtPeppers.java | |
--- | |
package soundsystem; | |
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); | |
} | |
} | |
CDPlayerConfig.java | |
--- | |
package soundsystem; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
@ComponentScan | |
public class CDPlayerConfig { | |
} | |
annotation.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:context="http://www.springframework.org/schema/context" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context.xsd"> | |
<context:component-scan base-package="soundsystem" /> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
本例展示Spring的AOP | |
BraveKnight.java是基本的POJO | |
Minstrel.java是AOP的处理对象 | |
aop.xml声明了切面的位置,以及处理方法 | |
BraveKnight.java | |
--- | |
package com.springinaction.knights; | |
public class BraveKnight implements Knight { | |
private Quest quest; | |
public BraveKnight(Quest quest) { | |
this.quest = quest; | |
} | |
public void embarkOnQuest() { | |
quest.embark(); | |
} | |
} | |
Minstrel.java | |
--- | |
package com.springinaction.knights; | |
import java.io.PrintStream; | |
public class Minstrel { | |
private PrintStream stream; | |
public Minstrel(PrintStream stream) { | |
this.stream = stream; | |
} | |
public void singBeforeQuest() { | |
stream.println("Fa la la, the knight is so brave!"); | |
} | |
public void singAfterQuest() { | |
stream.println("Tee hee hee, the brave knight " + | |
"did embark on a quest!"); | |
} | |
} | |
aop.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:aop="http://www.springframework.org/schema/aop" | |
xsi:schemaLocation="http://www.springframework.org/schema/aop | |
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd | |
http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<bean id="knight" class="com.springinaction.knights.BraveKnight"> | |
<constructor-arg ref="quest" /> | |
</bean> | |
<bean id="quest" class="com.springinaction.knights.SlayDragonQuest"> | |
<constructor-arg value="#{T(System).out}" /> | |
</bean> | |
<bean id="minstrel" class="com.springinaction.knights.Minstrel"> | |
<constructor-arg value="#{T(System).out}" /> | |
</bean> | |
<aop:config> | |
<aop:aspect ref="minstrel"> | |
<aop:pointcut id="embark" | |
expression="execution(* *.embarkOnQuest(..))"/> <!-- Define pointcut --> | |
 <aop:before pointcut-ref="embark" | |
method="singBeforeQuest"/> <!-- Declare before advice --> | |
<aop:after pointcut-ref="embark" | |
method="singAfterQuest"/> <!-- Decalre after advice --> | |
</aop:aspect> | |
</aop:config> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
本例展示Spring的Dependency Injection | |
启动类: KnightMain.java | |
对象: BraveKnight.java | |
依赖对象: SlayDragonQuest.java | |
配置: knight.xml | |
BraveKnight.java | |
--- | |
package com.springinaction.knights; | |
public class BraveKnight implements Knight { | |
private Quest quest; | |
public BraveKnight(Quest quest) { | |
this.quest = quest; | |
} | |
public void embarkOnQuest() { | |
quest.embark(); | |
} | |
} | |
SlayDragonQuest.java | |
--- | |
package com.springinaction.knights; | |
import java.io.PrintStream; | |
public class SlayDragonQuest implements Quest { | |
private PrintStream stream; | |
public SlayDragonQuest(PrintStream stream) { | |
this.stream = stream; | |
} | |
public void embark() { | |
stream.println("Embarking on quest to slay the dragon!"); | |
} | |
} | |
knight.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" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<bean id="knight" class="com.springinaction.knights.BraveKnight"> | |
<constructor-arg ref="quest" /> | |
</bean> <!-- Inject quest bean --> | |
<bean id="quest" class="com.springinaction.knights.SlayDragonQuest"> | |
<constructor-arg value="#{T(System).out}" /> | |
 </bean> | |
</beans> | |
KnightMain.java | |
--- | |
package com.springinaction.knights; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
public class KnightMain { | |
public static void main(String[] args) throws Exception { | |
// Load Spring context | |
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( | |
"META-INF/spring/knight.xml"); | |
//Get knight bean | |
Knight knight = context.getBean(Knight.class); | |
knight.embarkOnQuest(); | |
context.close(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration,创建ApplicationContext配置 | |
@ComponentScan,自动扫描 | |
CDPlayerConfig.java | |
--- | |
package soundsystem; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class CDPlayerConfig { | |
/** | |
* Declare Simple Bean | |
*/ | |
@Bean | |
@Bean(name="lonelyHeartsClubBand") // 声明特定名字 | |
public CompactDisc sgtPeppers() { | |
return new SgtPeppers(); | |
} | |
/** | |
* constructor injection 1 | |
*/ | |
@Bean | |
public CDPlayer cdPlayer() { | |
return new CDPlayer(sgtPeppers()); | |
} | |
/** | |
* constructor injection 2 | |
*/ | |
@Bean | |
public CDPlayer cdPlayer(CompactDisc compactDisc) { | |
return new CDPlayer(compactDisc); | |
} | |
/** | |
* property injection | |
*/ | |
@Bean | |
public CDPlayer cdPlayer(CompactDisc compactDisc) { | |
CDPlayer cdPlayer = new CDPlayer(compactDisc); | |
cdPlayer.setCompactDisc(compactDisc); | |
return cdPlayer; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
混合使用 | |
CDConfig.java | |
--- | |
package soundsystem; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class CDConfig { | |
@Bean | |
public CompactDisc compactDisc() { | |
return new SgtPeppers(); | |
} | |
} | |
CDPlayerConfig.java | |
--- | |
package soundsystem; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Import; | |
@Configuration | |
@Import(CDConfig.class) | |
public class CDPlayerConfig { | |
@Bean | |
public CDPlayer cdPlayer(CompactDisc compactDisc) { | |
return new CDPlayer(compactDisc); | |
} | |
} | |
JavaConfig整合两个JavaConfig | |
=== | |
package soundsystem; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Import; | |
@Configuration | |
@Import({CDPlayerConfig.class, CDConfig.class}) | |
public class SoundSystemConfig { | |
} | |
JavaConfig整合JavaConfig和XML | |
=== | |
package soundsystem; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Import; | |
import org.springframework.context.annotation.ImportResource; | |
@Configuration | |
@Import(CDPlayerConfig.class) | |
@ImportResource("classpath:cd-config.xml") | |
public class SoundSystemConfig { | |
} | |
XML整合JavaConfig和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 class="soundsystem.CDConfig" /> | |
<import resource="cdplayer-config.xml" /> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
注意XSD: XML Schema Definition | |
相比JavaConfig方式,XML配置不会做类型检查 | |
如果用c-namespace,需要用响应的XSD | |
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" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans.xsd | |
http://www.springframework.org/schema/context"> | |
<!-- configuration details go here --> | |
<!-- Declare simple Bean --> | |
<bean class="soundsystem.SgtPeppers" /> | |
<!-- 通过id给Bean做标识 --> | |
<bean id="compactDisc" class="soundsystem.SgtPeppers" /> | |
<!-- constructor injection --> | |
<bean id="cdPlayer" class="soundsystem.CDPlayer"> | |
<constructor-arg ref="compactDisc" /> | |
</bean> | |
<!-- 传参数值 --> | |
<bean id="compactDisc" | |
class="soundsystem.BlankDisc"> | |
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> | |
<constructor-arg value="The Beatles" /> | |
</bean> | |
<!-- 可以使用NULL --> | |
<bean id="compactDisc" class="soundsystem.BlankDisc"> | |
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> | |
<constructor-arg value="The Beatles" /> | |
<constructor-arg><null/></constructor-arg> | |
</bean> | |
<!-- 使用List --> | |
<bean id="compactDisc" class="soundsystem.BlankDisc"> | |
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> | |
<constructor-arg value="The Beatles" /> | |
<constructor-arg> | |
<list> | |
<value>Sgt. Pepper's Lonely Hearts Club Band</value> | |
<value>With a Little Help from My Friends</value> | |
<value>Lucy in the Sky with Diamonds</value> | |
<value>Getting Better</value> | |
<value>Fixing a Hole</value> | |
<!-- ...other tracks omitted for brevity... --> | |
<!-- 也可以使用引用 --> | |
<ref bean="xxx" /> | |
</list> | |
</constructor-arg> | |
</bean> | |
<!-- Set --> | |
<bean id="compactDisc" class="soundsystem.BlankDisc"> | |
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> | |
<constructor-arg value="The Beatles" /> | |
<constructor-arg> | |
<set> | |
<value>Sgt. Pepper's Lonely Hearts Club Band</value> | |
<value>With a Little Help from My Friends</value> | |
<value>Lucy in the Sky with Diamonds</value> | |
<value>Getting Better</value> | |
<value>Fixing a Hole</value> | |
<!-- ...other tracks omitted for brevity... --> | |
</set> | |
</constructor-arg> | |
</bean> | |
</beans> | |
c-namespace | |
--- | |
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:c="http://www.springframework.org/schema/c" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<!-- c-namespace --> | |
<!-- 这里的cd时参数的名字--> | |
<bean id="cdPlayer" class="soundsystem.CDPlayer" | |
c:cd-ref="compactDisc" /> | |
<!-- 这里也能使用参数的位置 --> | |
<bean id="cdPlayer" class="soundsystem.CDPlayer" | |
c:_0-ref="compactDisc" /> | |
<!-- 唯一参数 --> | |
<bean id="cdPlayer" class="soundsystem.CDPlayer" | |
c:_-ref="compactDisc" /> | |
<bean id="compactDisc" | |
class="soundsystem.BlankDisc" | |
c:_title="Sgt. Pepper's Lonely Hearts Club Band" | |
c:_artist="The Beatles" /> | |
<bean id="compactDisc" | |
class="soundsystem.BlankDisc" | |
c:_0="Sgt. Pepper's Lonely Hearts Club Band" | |
c:_1="The Beatles" /> | |
</beans> | |
p-namespace | |
--- | |
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:p="http://www.springframework.org/schema/p" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<!-- 引用 --> | |
<bean id="cdPlayer" | |
class="soundsystem.CDPlayer" | |
p:compactDisc-ref="compactDisc" /> | |
</bean> | |
property injection | |
--- | |
<bean id="compactDisc" | |
class="soundsystem.BlankDisc"> | |
<property name="title" | |
value="Sgt. Pepper's Lonely Hearts Club Band" /> | |
<property name="artist" value="The Beatles" /> | |
<property name="tracks"> | |
<list> | |
<value>Sgt. Pepper's Lonely Hearts Club Band</value> | |
<value>With a Little Help from My Friends</value> | |
<value>Lucy in the Sky with Diamonds</value> | |
<value>Getting Better</value> | |
<value>Fixing a Hole</value> | |
<!-- ...other tracks omitted for brevity... --> | |
</list> | |
</property> | |
</bean> | |
util-namespace | |
--- | |
<?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:p="http://www.springframework.org/schema/p" | |
xmlns:util="http://www.springframework.org/schema/util" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans.xsd | |
http://www.springframework.org/schema/util | |
http://www.springframework.org/schema/util/spring-util.xsd"> | |
... | |
</beans> | |
创建List | |
<util:list id="trackList"> | |
<value>Sgt. Pepper's Lonely Hearts Club Band</value> | |
<value>With a Little Help from My Friends</value> | |
<value>Lucy in the Sky with Diamonds</value> | |
<value>Getting Better</value> | |
<value>Fixing a Hole</value> | |
<!-- ...other tracks omitted for brevity... --> | |
</util:list> | |
然后再用property injection | |
<bean id="compactDisc" | |
class="soundsystem.BlankDisc" | |
p:title="Sgt. Pepper's Lonely Hearts Club Band" | |
p:artist="The Beatles" | |
p:tracks-ref="trackList" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment