• 周四. 10 月 3rd, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Introduce enhanced spring AOP to add new methods and functions to the target bean

King Wang

1 月 3, 2022

Introducing enhancements -Spring AOP Give the target bean Add new method 、 function

Introducing enhancements : You can add methods to existing classes 、 Logic .
Let’s take a simple one Person class , Can only run, Give him one more fly Methods .

 Insert picture description here

  • IPerson
public interface IPerson {

void run();
}
  • Person
public class Person implements IPerson {

@Override
public void run() {

System.out.println(" You can run ...");
}
}

The above is a very simple interface 、 Implementation class , To have a run Method .

We also have a definition fly The interface of :

  • IFlying
public interface IFlying {

void fly();
}

Now our goal is not to modify IPerson、Person To make the Person Have fly The function of . Here you can use Spring AOP Dynamic introduction of enhanced notification implementation .

Inherit DelegatingIntroductionInterceptor

 Insert picture description here
Give Way FlyImpl Expand DelegatingIntroductionInterceptor To achieve

  • FlyImpl
public class FlyImpl extends DelegatingIntroductionInterceptor implements IFlying {

@Override
public void fly() {

System.out.println(" I can fly ...");
}
}

Configure agent

  • application.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!-- original bean-->
<bean id="person" class="org.byron4j.demo.Person"/>
<!-- Introducing enhancements , bring person Have flyde Method -->
<bean id="fly" class="org.byron4j.demo.FlyImpl"/>
<!-- To configure ProxyFactoryBean-->
<bean id="proxyBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- Interface introduced -->
<property name="interfaces" value="org.byron4j.demo.IFlying"/>
<!-- Containing enhanced logic bean-->
<property name="interceptorNames" value="fly"/>
<!-- Goal enhancement bean-->
<property name="target" ref="person"/>
<property name="proxyTargetClass" value="true"/>
</bean>
</beans>

pom.xml

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>compile</scope>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>

test

@Test
public void test(){

ApplicationContext applicationContext
= new ClassPathXmlApplicationContext("application.xml");
// What you get is person
IPerson person = ((IPerson) applicationContext.getBean("proxyBean"));
IFlying flying = (IFlying) person;
flying.fly(); // Output : I can fly ...
}

发表回复