Browse Source

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]
pull/50/head
Dave Syer 12 years ago
parent
commit
d06d202fd0
  1. 50
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/aop/AopAutoConfiguration.java
  2. 1
      spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories
  3. 1
      spring-boot-samples/pom.xml
  4. 30
      spring-boot-samples/spring-boot-sample-aop/pom.xml
  5. 45
      spring-boot-samples/spring-boot-sample-aop/src/main/java/org/springframework/boot/sample/aop/SampleAopApplication.java
  6. 17
      spring-boot-samples/spring-boot-sample-aop/src/main/java/org/springframework/boot/sample/aop/monitor/ServiceMonitor.java
  7. 32
      spring-boot-samples/spring-boot-sample-aop/src/main/java/org/springframework/boot/sample/aop/service/HelloWorldService.java
  8. 1
      spring-boot-samples/spring-boot-sample-aop/src/main/resources/application.properties
  9. 70
      spring-boot-samples/spring-boot-sample-aop/src/test/java/org/springframework/boot/sample/aop/SampleAopApplicationTests.java
  10. 1
      spring-boot-starters/pom.xml
  11. 1
      spring-boot-starters/spring-boot-starter-aop/.gitignore
  12. 34
      spring-boot-starters/spring-boot-starter-aop/pom.xml
  13. 6
      spring-boot-starters/spring-boot-starter-data-jpa/pom.xml
  14. 5
      spring-boot-starters/spring-boot-starter-parent/pom.xml

50
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/aop/AopAutoConfiguration.java

@ -0,0 +1,50 @@ @@ -0,0 +1,50 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.Advice;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring AOP.
*
* @author Dave Syer
* @see EnableAspectJAutoProxy
*/
@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class })
@ConditionalOnExpression("${spring.aop.auto:true}")
public class AopAutoConfiguration {
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = false)
@ConditionalOnExpression("!${spring.aop.proxyTargetClass:false}")
public static class JdkDynamicAutoProxyConfiguration {
}
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ConditionalOnExpression("${spring.aop.proxyTargetClass:false}")
public static class CglibAutoProxyConfiguration {
}
}

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

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\

1
spring-boot-samples/pom.xml

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
<modules>
<module>spring-boot-sample-actuator</module>
<module>spring-boot-sample-actuator-ui</module>
<module>spring-boot-sample-aop</module>
<module>spring-boot-sample-batch</module>
<module>spring-boot-sample-data-jpa</module>
<module>spring-boot-sample-integration</module>

30
spring-boot-samples/spring-boot-sample-aop/pom.xml

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-aop</artifactId>
<packaging>jar</packaging>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

45
spring-boot-samples/spring-boot-sample-aop/src/main/java/org/springframework/boot/sample/aop/SampleAopApplication.java

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.sample.aop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.sample.aop.service.HelloWorldService;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SampleAopApplication implements CommandLineRunner {
// Simple example shows how an application can spy on itself with AOP
@Autowired
private HelloWorldService helloWorldService;
@Override
public void run(String... args) {
System.out.println(this.helloWorldService.getHelloMessage());
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleAopApplication.class, args);
}
}

17
spring-boot-samples/spring-boot-sample-aop/src/main/java/org/springframework/boot/sample/aop/monitor/ServiceMonitor.java

@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
package org.springframework.boot.sample.aop.monitor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class ServiceMonitor {
@AfterReturning("execution(* *..*Service.*(..))")
public void logServiceAccess(JoinPoint joinPoint) {
System.out.println("Completed: " + joinPoint);
}
}

32
spring-boot-samples/spring-boot-sample-aop/src/main/java/org/springframework/boot/sample/aop/service/HelloWorldService.java

@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.sample.aop.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class HelloWorldService {
@Value("${name:World}")
private String name;
public String getHelloMessage() {
return "Hello " + this.name;
}
}

1
spring-boot-samples/spring-boot-sample-aop/src/main/resources/application.properties

@ -0,0 +1 @@ @@ -0,0 +1 @@
name: Phil

70
spring-boot-samples/spring-boot-sample-aop/src/test/java/org/springframework/boot/sample/aop/SampleAopApplicationTests.java

@ -0,0 +1,70 @@ @@ -0,0 +1,70 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.sample.aop;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.sample.aop.SampleAopApplication;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link SampleAopApplication}.
*
* @author Dave Syer
* @author Phillip Webb
*/
public class SampleAopApplicationTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();
private String profiles;
@Before
public void init() {
this.profiles = System.getProperty("spring.profiles.active");
}
@After
public void after() {
if (this.profiles != null) {
System.setProperty("spring.profiles.active", this.profiles);
}
else {
System.clearProperty("spring.profiles.active");
}
}
@Test
public void testDefaultSettings() throws Exception {
SampleAopApplication.main(new String[0]);
String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("Hello Phil"));
}
@Test
public void testCommandLineOverrides() throws Exception {
SampleAopApplication.main(new String[] { "--name=Gordon" });
String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("Hello Gordon"));
}
}

1
spring-boot-starters/pom.xml

@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
</properties>
<modules>
<module>spring-boot-starter</module>
<module>spring-boot-starter-aop</module>
<module>spring-boot-starter-batch</module>
<module>spring-boot-starter-data-jpa</module>
<module>spring-boot-starter-integration</module>

1
spring-boot-starters/spring-boot-starter-aop/.gitignore vendored

@ -0,0 +1 @@ @@ -0,0 +1 @@
/target

34
spring-boot-starters/spring-boot-starter-aop/pom.xml

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-starter-aop</artifactId>
<packaging>jar</packaging>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies>
</project>

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

@ -15,17 +15,13 @@ @@ -15,17 +15,13 @@
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>

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

@ -42,6 +42,11 @@ @@ -42,6 +42,11 @@
<artifactId>spring-boot-starter</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>

Loading…
Cancel
Save