Spring AOP Program - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

Spring AOP Program

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)
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() {
}
}
view raw LoginForm.java hosted with ❤ by GitHub
 


pom.xml
<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>
view raw pom.xml hosted with ❤ by GitHub
 


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>
view raw Beans.xml hosted with ❤ by GitHub
Output:
login form
amount deposited..
logout sucessfully..
view raw javaw.exe hosted with ❤ by GitHub


Project Folder Structure 


Spring AOP

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