Browse Source
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
14 changed files with 289 additions and 5 deletions
@ -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 { |
||||
} |
||||
|
||||
} |
||||
@ -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> |
||||
@ -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); |
||||
} |
||||
} |
||||
@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -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; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
name: Phil |
||||
@ -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")); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
/target |
||||
@ -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> |
||||
Loading…
Reference in new issue