Apache Servicemix :: Bundles :: Aspectj framework
Apache Servicemix :: Bundles :: Aspectj framework
ASPECTJ is a powerful -faced -oriented -oriented programming (AOP) framework, which can integrated with Apache ServiceMix to enhance the modular and reused of the system.This article will introduce the basic concepts and usage methods of the ASPECTJ framework, and use the Java code example to demonstrate how to use it in servicemix.
ASPECTJ is based on the expansion of the Java language. It introduces new keywords and grammar rules to facilitate developers to describe and manage cross -sectional attention points.By using ASPECTJ, developers can strip the cross -sectional focus (such as logging, transaction processing, and security inspection) from the core logic of the application, and managed in a modular and reusable manner.
To use the ASPECTJ framework in Servicesmix, you need to add the relevant dependencies of the ASPECTJ framework to the construction file of the project (such as adding corresponding dependencies when using Maven for project construction).The following is a fragment of a sample pom file, which shows how to add the dependency item of the ASPECTJ framework:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.7</version>
</dependency>
After adding ASPECTJ's dependencies, you can start using ASPECTJ to define the cuttings and Join Points.The cut surface is composed of cutting points and corresponding notifications. The notification defines additional behaviors performed on the cut point.The following is a code of the sample cut surface:
import org.aspectj.lang.annotation.*;
import org.apache.servicemix.*;
import org.apache.servicemix.example.*;
@Aspect
public class LoggingAspect {
@Pointcut("execution(* org.apache.servicemix.example.MyService.sayHello(..))")
public void loggingPointcut() {}
@Before("loggingPointcut()")
public void beforeSayHello() {
System.out.println("Before sayHello");
}
@After("loggingPointcut()")
public void afterSayHello() {
System.out.println("After sayHello");
}
}
In the above example, the cut point `LoggingpointCut` defines the cut point of the` Sayhello` method of the matching `ORG.APACHE.SERVICEMIX.Example.myService`.Notification `Beforesayhello` and` Aftersayhello` Print the corresponding log messages before and after the cut point execution.
In order to enable ASPECTJ, we also need to configure the ServiceMix container to support ASPECTJ.You can add the following in the configuration file of the servicemix:
<bean id="loggingAspect" class="com.example.LoggingAspect"/>
<bean id="aspectJConfig" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
<property name="transactionManager" ref="transactionManager"/>
<property name="size" value="0"/>
<property name="transactionName" value="PROPAGATION_REQUIRES_NEW"/>
</bean>
<bean id="aspectsConfigurer" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
<property name="transactionManager" ref="transactionManager"/>
<property name="size" value="0"/>
<property name="transactionName" value="PROPAGATION_REQUIRES_NEW"/>
</bean>
In the above configuration file, the `loggingaspect` defines an instance of Aspectj cutting surface.
After completing the above configuration, the Aspectj framework can be seamlessly integrated with the ServiceMix.When the servicemix loads and runs the application, the ASPECTJ cutting surface will execute the defined notification logic on the corresponding cut point to enhance the cutting behavior.
To sum up, the ASPECTJ framework provides a powerful -oriented programming ability. By being separated from the core logic from the core logic, the system can be modularized and reused.Use ASPECTJ in servicemix, we need to add related dependencies, define the cutting surface and cut point, and finally configure servicemix to support ASPECTJ.As ASPECTJ is effective, we can flexibly enhance the servicemix application.
Notice:
The above is just a simple example of Aspectj and ServiceMix integration. It may involve more complicated usage and configuration in actual use.Please refer to the official documents of Aspectj and ServiceMix to obtain more detailed information and more advanced example code.