Skip to content

Commit d06d202

Browse files
author
Dave Syer
committed
Add AopAutoConfiguration (also starter and sample)
A side effect is that spring-boot-starter-data-jpa needs to include an aspectjweaver depdendency. Hope that doesn't hurt anything else. [Fixes #56780004]
1 parent ba4a815 commit d06d202

File tree

14 files changed

+289
-5
lines changed

14 files changed

+289
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.aop;
18+
19+
import org.aspectj.lang.annotation.Aspect;
20+
import org.aspectj.lang.reflect.Advice;
21+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
26+
27+
/**
28+
* {@link EnableAutoConfiguration Auto-configuration} for Spring AOP.
29+
*
30+
* @author Dave Syer
31+
* @see EnableAspectJAutoProxy
32+
*/
33+
@Configuration
34+
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class })
35+
@ConditionalOnExpression("${spring.aop.auto:true}")
36+
public class AopAutoConfiguration {
37+
38+
@Configuration
39+
@EnableAspectJAutoProxy(proxyTargetClass = false)
40+
@ConditionalOnExpression("!${spring.aop.proxyTargetClass:false}")
41+
public static class JdkDynamicAutoProxyConfiguration {
42+
}
43+
44+
@Configuration
45+
@EnableAspectJAutoProxy(proxyTargetClass = true)
46+
@ConditionalOnExpression("${spring.aop.proxyTargetClass:false}")
47+
public static class CglibAutoProxyConfiguration {
48+
}
49+
50+
}

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Auto Configure
22
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
3+
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
34
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
45
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
56
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\

spring-boot-samples/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<modules>
1717
<module>spring-boot-sample-actuator</module>
1818
<module>spring-boot-sample-actuator-ui</module>
19+
<module>spring-boot-sample-aop</module>
1920
<module>spring-boot-sample-batch</module>
2021
<module>spring-boot-sample-data-jpa</module>
2122
<module>spring-boot-sample-integration</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<!-- Your own application should inherit from spring-boot-starter-parent -->
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-samples</artifactId>
9+
<version>0.5.0.BUILD-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>spring-boot-sample-aop</artifactId>
12+
<packaging>jar</packaging>
13+
<properties>
14+
<main.basedir>${basedir}/../..</main.basedir>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>${project.groupId}</groupId>
19+
<artifactId>spring-boot-starter-aop</artifactId>
20+
</dependency>
21+
</dependencies>
22+
<build>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-maven-plugin</artifactId>
27+
</plugin>
28+
</plugins>
29+
</build>
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.sample.aop;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.boot.CommandLineRunner;
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.sample.aop.service.HelloWorldService;
24+
import org.springframework.context.annotation.ComponentScan;
25+
import org.springframework.context.annotation.Configuration;
26+
27+
@Configuration
28+
@EnableAutoConfiguration
29+
@ComponentScan
30+
public class SampleAopApplication implements CommandLineRunner {
31+
32+
// Simple example shows how an application can spy on itself with AOP
33+
34+
@Autowired
35+
private HelloWorldService helloWorldService;
36+
37+
@Override
38+
public void run(String... args) {
39+
System.out.println(this.helloWorldService.getHelloMessage());
40+
}
41+
42+
public static void main(String[] args) throws Exception {
43+
SpringApplication.run(SampleAopApplication.class, args);
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.springframework.boot.sample.aop.monitor;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.AfterReturning;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.springframework.stereotype.Component;
7+
8+
@Aspect
9+
@Component
10+
public class ServiceMonitor {
11+
12+
@AfterReturning("execution(* *..*Service.*(..))")
13+
public void logServiceAccess(JoinPoint joinPoint) {
14+
System.out.println("Completed: " + joinPoint);
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.sample.aop.service;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
import org.springframework.stereotype.Component;
21+
22+
@Component
23+
public class HelloWorldService {
24+
25+
@Value("${name:World}")
26+
private String name;
27+
28+
public String getHelloMessage() {
29+
return "Hello " + this.name;
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name: Phil
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.sample.aop;
18+
19+
import org.junit.After;
20+
import org.junit.Before;
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.springframework.boot.OutputCapture;
24+
import org.springframework.boot.sample.aop.SampleAopApplication;
25+
26+
import static org.junit.Assert.assertTrue;
27+
28+
/**
29+
* Tests for {@link SampleAopApplication}.
30+
*
31+
* @author Dave Syer
32+
* @author Phillip Webb
33+
*/
34+
public class SampleAopApplicationTests {
35+
36+
@Rule
37+
public OutputCapture outputCapture = new OutputCapture();
38+
39+
private String profiles;
40+
41+
@Before
42+
public void init() {
43+
this.profiles = System.getProperty("spring.profiles.active");
44+
}
45+
46+
@After
47+
public void after() {
48+
if (this.profiles != null) {
49+
System.setProperty("spring.profiles.active", this.profiles);
50+
}
51+
else {
52+
System.clearProperty("spring.profiles.active");
53+
}
54+
}
55+
56+
@Test
57+
public void testDefaultSettings() throws Exception {
58+
SampleAopApplication.main(new String[0]);
59+
String output = this.outputCapture.toString();
60+
assertTrue("Wrong output: " + output, output.contains("Hello Phil"));
61+
}
62+
63+
@Test
64+
public void testCommandLineOverrides() throws Exception {
65+
SampleAopApplication.main(new String[] { "--name=Gordon" });
66+
String output = this.outputCapture.toString();
67+
assertTrue("Wrong output: " + output, output.contains("Hello Gordon"));
68+
}
69+
70+
}

spring-boot-starters/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
</properties>
1616
<modules>
1717
<module>spring-boot-starter</module>
18+
<module>spring-boot-starter-aop</module>
1819
<module>spring-boot-starter-batch</module>
1920
<module>spring-boot-starter-data-jpa</module>
2021
<module>spring-boot-starter-integration</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starters</artifactId>
8+
<version>0.5.0.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>spring-boot-starter-aop</artifactId>
11+
<packaging>jar</packaging>
12+
<properties>
13+
<main.basedir>${basedir}/../..</main.basedir>
14+
</properties>
15+
<dependencies>
16+
<dependency>
17+
<groupId>${project.groupId}</groupId>
18+
<artifactId>spring-boot-starter</artifactId>
19+
<version>${project.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework</groupId>
23+
<artifactId>spring-aop</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.aspectj</groupId>
27+
<artifactId>aspectjrt</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.aspectj</groupId>
31+
<artifactId>aspectjweaver</artifactId>
32+
</dependency>
33+
</dependencies>
34+
</project>

spring-boot-starters/spring-boot-starter-data-jpa/pom.xml

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@
1515
<dependencies>
1616
<dependency>
1717
<groupId>${project.groupId}</groupId>
18-
<artifactId>spring-boot-starter</artifactId>
18+
<artifactId>spring-boot-starter-aop</artifactId>
1919
<version>${project.version}</version>
2020
</dependency>
2121
<dependency>
2222
<groupId>org.hibernate</groupId>
2323
<artifactId>hibernate-entitymanager</artifactId>
2424
</dependency>
25-
<dependency>
26-
<groupId>org.springframework</groupId>
27-
<artifactId>spring-aop</artifactId>
28-
</dependency>
2925
<dependency>
3026
<groupId>org.springframework</groupId>
3127
<artifactId>spring-orm</artifactId>

spring-boot-starters/spring-boot-starter-parent/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
<artifactId>spring-boot-starter</artifactId>
4343
<version>0.5.0.BUILD-SNAPSHOT</version>
4444
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-aop</artifactId>
48+
<version>0.5.0.BUILD-SNAPSHOT</version>
49+
</dependency>
4550
<dependency>
4651
<groupId>org.springframework.boot</groupId>
4752
<artifactId>spring-boot-starter-actuator</artifactId>

0 commit comments

Comments
 (0)