Skip to content

Commit ac00e2b

Browse files
vonzhouvonzhou
vonzhou
authored and
vonzhou
committed
大江东流去,游子日月长
1 parent ddc5525 commit ac00e2b

File tree

106 files changed

+1757
-391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1757
-391
lines changed

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 341 additions & 343 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,30 @@
2222

2323
[springidol]()
2424

25+
## 3. 最小化Spring XML配置
26+
27+
* 4 种自动装配 byName, byType, contructor, autodetect
28+
* 可以在一个应用上下文中定义多个配置文件,每个配置文件设置自己的默认自动装配策略(default-autowire)
29+
* 如果使用constructor自动装配策略,就不能混合使用constructor-arg
30+
* 注解方式可以实现更细粒度的自动装配,Spring容器默认禁用注解装配,要在配置文件中开启
31+
* \<context:component-scan> 配置自动扫描
32+
* 在基于Java的配置中使用@Configuration注解的Java类,等价于XML配置中的\<beans>元素
33+
34+
[springidol-autodiscovery]()
35+
36+
## 4. 面向切面Spring
37+
38+
* Spring只支持方法连接点
39+
* 体会切面的应用场景
40+
* before, after, around advice
41+
* 为advice传递参数
42+
* introduction为已有的接口引入新接口
43+
* at aspect
44+
45+
[springidol-aop]()
46+
47+
[springidol-aspectj]()
48+
49+
## 5.
50+
51+

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<modules>
1212
<module>knights</module>
1313
<module>springidol</module>
14+
<module>springidol-autodiscovery</module>
15+
<module>springidol-aop</module>
16+
<module>springidol-aspectj</module>
1417
</modules>
1518

1619

springidol-aop/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.vonzhou.springinaction</groupId>
8+
<artifactId>springidol-aop</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<spring.group>org.springframework</spring.group>
13+
<spring.version>3.0.2.RELEASE</spring.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>${spring.group}</groupId>
19+
<artifactId>spring-context</artifactId>
20+
<version>${spring.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>${spring.group}</groupId>
24+
<artifactId>spring-aop</artifactId>
25+
<version>${spring.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.aspectj</groupId>
29+
<artifactId>aspectjweaver</artifactId>
30+
<version>1.6.4</version>
31+
</dependency>
32+
33+
34+
<dependency>
35+
<groupId>junit</groupId>
36+
<artifactId>junit</artifactId>
37+
<version>4.7</version>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
</project>

springidol-aop/springidol-aop.iml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="Maven: org.springframework:spring-context:3.0.2.RELEASE" level="project" />
15+
<orderEntry type="library" name="Maven: org.springframework:spring-beans:3.0.2.RELEASE" level="project" />
16+
<orderEntry type="library" name="Maven: org.springframework:spring-core:3.0.2.RELEASE" level="project" />
17+
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.1" level="project" />
18+
<orderEntry type="library" name="Maven: org.springframework:spring-expression:3.0.2.RELEASE" level="project" />
19+
<orderEntry type="library" name="Maven: org.springframework:spring-asm:3.0.2.RELEASE" level="project" />
20+
<orderEntry type="library" name="Maven: org.springframework:spring-aop:3.0.2.RELEASE" level="project" />
21+
<orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
22+
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.6.4" level="project" />
23+
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.7" level="project" />
24+
</component>
25+
</module>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
import org.aspectj.lang.ProceedingJoinPoint;
4+
5+
public class AroundAudience {
6+
public void watchPerformance(ProceedingJoinPoint joinpoint) {
7+
try {
8+
System.out.println("The audience is taking their seats.");
9+
System.out.println("The audience is turning off their cellphones");
10+
long start = System.currentTimeMillis(); //<co id="co_beforeProceed"/>
11+
12+
joinpoint.proceed(); //<co id="co_proceed"/>
13+
14+
long end = System.currentTimeMillis(); // <co id="co_afterProceed"/>
15+
System.out.println("CLAP CLAP CLAP CLAP CLAP");
16+
System.out.println("The performance took " + (end - start)
17+
+ " milliseconds.");
18+
} catch (Throwable t) {
19+
System.out.println("Boo! We want our money back!"); //<co id="co_afterException"/>
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
import org.aspectj.lang.ProceedingJoinPoint;
4+
5+
public class Audience {
6+
// 表演之前
7+
public void takeSeats() { //<co id="co_takeSeats"/>
8+
System.out.println("The audience is taking their seats.");
9+
}
10+
// 表演之前
11+
public void turnOffCellPhones() { //<co id="co_turnOffCellPhones"/>
12+
System.out.println("The audience is turning off their cellphones");
13+
}
14+
// 表演之后
15+
public void applaud() { //<co id="co_applaud"/>
16+
System.out.println("CLAP CLAP CLAP CLAP CLAP");
17+
}
18+
// 表演失败之后
19+
public void demandRefund() { //<co id="co_demandRefund"/>
20+
System.out.println("Boo! We want our money back!");
21+
}
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
public interface Contestant {
4+
void receiveAward();
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
4+
/**
5+
* gracious 高尚的
6+
*/
7+
public class GraciousContestant implements Contestant {
8+
9+
public void receiveAward() {
10+
System.out.println("Why, thank you all very much!");
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
import static java.lang.Thread.sleep;
4+
5+
public class Guitar implements Instrument {
6+
public void play() {
7+
try{
8+
sleep(1000);
9+
}catch (Exception e){
10+
e.printStackTrace();
11+
}
12+
System.out.println("Strum strum strum");
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
public interface Instrument {
4+
public void play();
5+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
public class Instrumentalist implements Performer {
4+
public void perform() throws PerformanceException {
5+
instrument.play();
6+
}
7+
8+
private Instrument instrument;
9+
10+
public void setInstrument(Instrument instrument) {
11+
this.instrument = instrument;
12+
}
13+
14+
public Instrument getInstrument() {
15+
return instrument;
16+
}
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
public class Magician implements MindReader {
4+
private String thoughts;
5+
6+
public void interceptThoughts(String thoughts) {
7+
System.out.println("Intercepting volunteer's thoughts");
8+
this.thoughts = thoughts;
9+
}
10+
11+
public String getThoughts() {
12+
return thoughts;
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
/**
7+
* P99 sneaky : 卑鄙的
8+
* Created by vonzhou on 16/3/31.
9+
*/
10+
public class MagicianMain {
11+
public static void main(String[] args) throws Exception{
12+
ApplicationContext context = new ClassPathXmlApplicationContext("sneaky-magician.xml");
13+
Thinker volunteer = (Thinker)context.getBean("volunteer");
14+
volunteer.thinkOfSomething("I want to play basketball, en");
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
public interface MindReader {
4+
void interceptThoughts(String thoughts);
5+
6+
String getThoughts();
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
public class PerformanceException extends Exception {
4+
private static final long serialVersionUID = 1L;
5+
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
public interface Performer {
4+
void perform() throws PerformanceException;
5+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
/**
7+
* 排练
8+
* Created by vonzhou on 16/3/31.
9+
*/
10+
public class RehearsalMain {
11+
public static void main(String[] args) throws Exception{
12+
ApplicationContext context = new ClassPathXmlApplicationContext("spring-idol-around.xml");
13+
Performer performer = (Performer)context.getBean("eddie");
14+
performer.perform();
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
import org.aspectj.lang.ProceedingJoinPoint;
4+
5+
public class Stutter {
6+
public Object stutter(ProceedingJoinPoint call) throws Throwable {
7+
call.proceed();
8+
call.proceed();
9+
call.proceed();
10+
call.proceed();
11+
call.proceed();
12+
call.proceed();
13+
call.proceed();
14+
call.proceed();
15+
return call.proceed();
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
public interface Thinker {
4+
void thinkOfSomething(String thoughts);
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.vonzhou.springinaction.springidolaop;
2+
3+
public class Volunteer implements Thinker {
4+
private String thoughts;
5+
6+
public void thinkOfSomething(String thoughts) {
7+
this.thoughts = thoughts;
8+
}
9+
10+
public String getThoughts() {
11+
return thoughts;
12+
}
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:aop="http://www.springframework.org/schema/aop"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/aop
8+
http://www.springframework.org/schema/aop/spring-aop.xsd">
9+
10+
<bean id="volunteer"
11+
class="com.vonzhou.springinaction.springidolaop.Volunteer" />
12+
<bean id="magician"
13+
class="com.vonzhou.springinaction.springidolaop.Magician" />
14+
15+
<aop:config>
16+
<aop:aspect ref="magician">
17+
<aop:pointcut id="thinking"
18+
expression="execution(*
19+
com.vonzhou.springinaction.springidolaop.Thinker.thinkOfSomething(String))
20+
and args(thoughts)" />
21+
22+
<aop:before
23+
pointcut-ref="thinking"
24+
method="interceptThoughts"
25+
arg-names="thoughts" />
26+
</aop:aspect>
27+
</aop:config>
28+
29+
</beans>

0 commit comments

Comments
 (0)