+ 8

Have you heard about aspect-oriented programming (AOP)?

24th Mar 2017, 6:10 PM
Grzegorz Kawalec
Grzegorz Kawalec - avatar
11 Answers
+ 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.
26th Mar 2017, 1:32 PM
Grzegorz Kawalec
Grzegorz Kawalec - avatar
+ 6
aspect-oriented???
24th Mar 2017, 4:32 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 6
can you give example
24th Mar 2017, 4:42 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 5
Yes @Prabhaker Pandey. The AOP can be used in the Java Spring framework.
24th Mar 2017, 4:36 PM
Grzegorz Kawalec
Grzegorz Kawalec - avatar
+ 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.
24th Mar 2017, 5:26 PM
Grzegorz Kawalec
Grzegorz Kawalec - avatar
+ 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).
24th Mar 2017, 5:43 PM
Grzegorz Kawalec
Grzegorz Kawalec - avatar
0
no
24th Mar 2017, 7:59 PM
Forbaneius
Forbaneius - avatar
0
What?!
25th Mar 2017, 1:13 AM
Blake Bates
Blake Bates - avatar
0
never heard
29th Mar 2017, 2:07 PM
DNABIRONG
DNABIRONG - avatar