Steps to develop spring AOP Application
1) create new maven project.
2) add required
dependencies in POM.xml file
Spring aop jar, aspectjweaver, aspectjrt, context, etc.!
3) create beans configured Java/xml file.
4) write
business logic inside src/main/java
Java Project(primary logic)
package com.smarttechguides; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
import com.smarttechguides.transactions.BankTransactions; | |
public class BankApplication { | |
public static void main(String[] args) { | |
ApplicationContext factory = new ClassPathXmlApplicationContext("Beans.xml"); | |
BankTransactions bean = factory.getBean("bank", BankTransactions.class); | |
bean.depositeAmount(); | |
} | |
} |
package com.smarttechguides.transactions; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class BankTransactions { | |
public void depositeAmount() { | |
System.out.println("amount deposited.."); | |
} | |
public void withdrawnAmount() { | |
System.out.println("amount withdrawn..."); | |
} | |
} |
Java Project(cross-cutting/seconday logic)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.smarttechguides;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
@Component
@Aspect
@EnableAspectJAutoProxy
public class LoginForm {
@Before("logForm()")
public void loginForm() {
System.out.println("login form");
}
@After("logForm()")
public void logoutForm() {
System.out.println("logout sucessfully..");
}
@Pointcut("execution(public void depositeAmount())")
public void logForm() {
}
}
package com.smarttechguides; | |
import org.aspectj.lang.annotation.After; | |
import org.aspectj.lang.annotation.Aspect; | |
import org.aspectj.lang.annotation.Before; | |
import org.aspectj.lang.annotation.Pointcut; | |
import org.springframework.context.annotation.EnableAspectJAutoProxy; | |
import org.springframework.stereotype.Component; | |
@Component | |
@Aspect | |
@EnableAspectJAutoProxy | |
public class LoginForm { | |
@Before("logForm()") | |
public void loginForm() { | |
System.out.println("login form"); | |
} | |
@After("logForm()") | |
public void logoutForm() { | |
System.out.println("logout sucessfully.."); | |
} | |
@Pointcut("execution(public void depositeAmount())") | |
public void logForm() { | |
} | |
} |
pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.smarttechguides</groupId>
<artifactId>02-BankApplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.3</version>
</dependency>
</dependencies>
</project>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.smarttechguides</groupId> | |
<artifactId>02-BankApplication</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>5.3.13</version> | |
</dependency> | |
<dependency> | |
<groupId>org.aspectj</groupId> | |
<artifactId>aspectjweaver</artifactId> | |
<version>1.9.7</version> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>aspectj</groupId> | |
<artifactId>aspectjrt</artifactId> | |
<version>1.5.3</version> | |
</dependency> | |
</dependencies> | |
</project> |
Beans.xml
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:aop="http://www.springframework.org/schema/aop" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | |
http://www.springframework.org/schema/aop | |
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context.xsd "> | |
<context:component-scan | |
base-package="com.smarttechguides"></context:component-scan> | |
<bean id="bank" | |
class="com.smarttechguides.transactions.BankTransactions" /> | |
</beans> | |
login form | |
amount deposited.. | |
logout sucessfully.. |
Project Folder Structure
Note: spring
beans we can configure in 3 ways
1) xml
based configuration file
Configure <beans> inside beans.xml file
2) annotation-based
configuration
Configure
<context:annotation-config/> inside beans.xml file and use annotations
inside Java class.
3) Java
based configuration
Configurations
happen in Java file without xml file.
with the help of annotations like @configuration, @beans, @component, @service, @controller..etc.!
No comments:
Post a Comment