AOP development of Spring (XML based on AspectJ)

Posted by stry_cat on Tue, 12 Nov 2019 19:08:44 +0100

Introduction to Spring's AOP:

AOP idea was first proposed by AOP alliance. Spring is the best framework to use this idea

Spring's AOP has its own implementation (very cumbersome) Aspect is an AOP framework. Spring introduces aspect as its own AOP development.

Two sets of AOP development methods in Spring

◆ traditional way of Spring (abandoned).

◆ Spring AoP development based on Aspect (using)

 

Related terms in the development of AOP:

 

 

 

Less nonsense, let's code!

1. Import the corresponding jar package

2. Introduce the configuration file of Spring

Create applicationContext.xml under src

<?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:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd"> 
        <!-- bean definitions here -->
        


</beans>

3. Write target class

Create classes and interfaces

package com.rick.aop.demo1;

public interface ProductDao {
	public void save();
	public void update();
	public void find();
	public void delete();

}
package com.rick.aop.demo1;

public class ProductDaoImpl implements ProductDao{

	@Override
	public void save() {
		System.out.println("Add...");
	}
	@Override
	public void update() {
		System.out.println("Product updates...");
	}
	@Override
	public void find() {
		System.out.println("Product search...");
	}
	@Override
	public void delete() {
		System.out.println("Goods delete...");
	}

}

4. Configuration of target class

<! -- configure target object: enhanced object -- >
        <bean id="productDao" class = "com.rick.aop.demo1.ProductDaoImpl"></bean>

5. Integrate Junit unit test

Introduce spring-test.jar

package com.rick.aop.demo1;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo {

	@Resource(name = "productDao")
	private ProductDao productDao;

	@Test
	public void demo1() {
		productDao.save();
		productDao.update();
		productDao.delete();
		productDao.find();
	}
}

Write cut class

package com.rick.aop.demo1;

/*
 * Section class
 */
public class MyAspectXml {
	//Preamplifier
	public void checkPri() {
		System.out.println("Permission check===========");
	}
}

Configuration: passing the facet class to spring management

 

<!-- Configure facet classes -->
<bean id="myAspectXml" class = "com.rick.aop.demo1.MyAspectXml"></bean>
<!-- Conduct aop Configuration -->
<aop:config>
	<!-- Configure pointcut expressions:Which classes and methods need to be enhanced -->
	<aop:pointcut expression="execution(*com.rick.aop.demo1.ProductDaoImpl.save(..))" id="pointcut1"/>
	<!-- Configuration section -->
	<aop:aspect ref="myAspectXml">
		<aop:before method="checkPri" pointcut-ref="pointcut1" />
	</aop:aspect>
</aop:config>

Test:

Notification type

  • Lead notification can get entry point information
  • Post notification can get the return value
  • Circular notification can organize the execution of target methods

Pointcut expression

 

Other enhanced configurations:

Upper code

modify

 

test

 

Topics: Spring xml Junit less