Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e62d82

Browse files
Spring AOP
Programación orientada a aspectos (AOP) desde Spring.
1 parent 27b0e06 commit 9e62d82

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

‎tutorial_AOP_introduccion/pom.xml

Lines changed: 34 additions & 0 deletions
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"
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+
<groupId>tutor.programacion</groupId>
7+
<artifactId>SpringAOP</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.springframework</groupId>
14+
<artifactId>spring-aop</artifactId>
15+
<version>4.3.8.RELEASE</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.springframework</groupId>
19+
<artifactId>spring-context</artifactId>
20+
<version>4.3.8.RELEASE</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.aspectj</groupId>
24+
<artifactId>aspectjweaver</artifactId>
25+
<version>1.8.10</version>
26+
</dependency>
27+
</dependencies>
28+
29+
<properties>
30+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31+
<maven.compiler.source>1.8</maven.compiler.source>
32+
<maven.compiler.target>1.8</maven.compiler.target>
33+
</properties>
34+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tutor.programacion.aspect;
2+
3+
import java.time.LocalDateTime;
4+
import org.aspectj.lang.JoinPoint;
5+
import org.aspectj.lang.annotation.After;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.aspectj.lang.annotation.Before;
8+
import org.aspectj.lang.annotation.Pointcut;
9+
import org.springframework.stereotype.Component;
10+
11+
@Aspect
12+
@Component
13+
public class LoggingAspect {
14+
15+
@Pointcut("execution(* tutor.programacion.component.*.*(..))")
16+
public void serviceMethod() { }
17+
18+
@Before("serviceMethod()")
19+
public void beforeLoginAdvice(JoinPoint jp) {
20+
System.out.println("\n" + jp.toLongString());
21+
System.out.println("iniciado " + LocalDateTime.now());
22+
}
23+
24+
@After("serviceMethod()")
25+
public void afterLoginAdvice() {
26+
System.out.println("finalizado " + LocalDateTime.now());
27+
}
28+
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tutor.programacion.component;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class CrudService {
7+
8+
public void create() {
9+
System.out.println("crear dato...");
10+
}
11+
12+
public void read() {
13+
System.out.println("leer dato...");
14+
}
15+
16+
public void update() {
17+
try {
18+
Thread.sleep(377);
19+
System.out.println("actualizar dato...");
20+
} catch (InterruptedException ex) {
21+
22+
}
23+
}
24+
25+
public void delete() {
26+
System.out.println("eliminar dato...");
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
package tutor.programacion.springaop;
3+
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6+
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
9+
import tutor.programacion.component.CrudService;
10+
11+
@Configuration
12+
@ComponentScan({"tutor.programacion.aspect", "tutor.programacion.component"})
13+
@EnableAspectJAutoProxy
14+
public class TutorialAOP {
15+
16+
public static void main(String[] args) {
17+
ApplicationContext ctx = new AnnotationConfigApplicationContext(TutorialAOP.class);
18+
19+
CrudService ps = ctx.getBean(CrudService.class);
20+
21+
ps.create();
22+
ps.read();
23+
ps.update();
24+
ps.delete();
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /