+ 8
Have you heard about aspect-oriented programming (AOP)?
9 Respostas
+ 9
@Ace, if you are working on large projects, you can easily set up a Logger mechanism for the whole project or just parts, not trashing this part of the code. This makes it easier to master a large project (in small projects it is also helpful). Once you try it you will often use it. I recommend you.
+ 6
aspect-oriented???
+ 6
can you give example
+ 5
Yes @Prabhaker Pandey. The AOP can be used in the Java Spring framework.
+ 5
Of course Prabhakar Pandey, I can give you a simple example.
Simple example:
(Does not include import of external classes and all classes are in one package)
--------------------
package pl.gkawalec.aop;
@Aspect
@Component
public class SimpleAspect {
@Pointcut("execution(* *..SimpleBean.*(..))")
public void allMethods() {}
@Before("allMethods()")
public void log(JoinPoint joinPoint) {
System.out.println("Before method: "
+ joinPoint.getSignature().getName());
}
@After("allMethods()")
public void after(JoinPoint joinPoint) {
System.out.println("Ater method: "
+ joinPoint.getSignature().getName());
}
}
--------------------
package pl.gkawalec.aop;
@Component
public class SimpleBean {
public void simpleMethod() {
System.out.println("simpleMetod");
}
}
--------------------
package pl.gkawalec.aop;
@Configuration
@ComponentScan("pl.gkawalec.aop")
@EnableAspectJAutoProxy
public class SpringConf {}
--------------------
package pl.gkawalec.aop;
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(SpringConf.class);
SimpleBean simpleBean = context.getBean(SimpleBean.class);
simpleBean.simpleMethod();
}
}
--------------------
Output:
Before method: simpleMethod
simpleMetod
Ater method: simpleMethod
--------------------
One method was called in the program simpleMethod().
The other two have not been called anywhere, but have been launched anyway.
+ 5
If you want to run the code contained in the response, it is suggested to create a new Maven project in the IntelliJ environment, Netbeans, Eclipse or SpringToolSuit, and then paste the following code into the pom.xml file:
---------------------------------
<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>
<groupId>pl.gkawalec</groupId>
<artifactId>aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
</project>
---------------------------------
This will allow you to download the necessary dependencies (libraries).
0
no
0
What?!
0
never heard