ASPECTJ allows you to use annotation to define facets, pointcuts, and enhanced processing. That is, by annotating the @aspect to turn a plain Java class into a slice bean and then enhance the other @component component Java class
To use the annotation aspectj, you need to go to the environment configuration (URI configuration of XML namespace of the schema, installation of Jar package)
1. Environment configuration.
1.1 Using XML Schema Configuration
FOR XML namespaces (i.e.: xmlnamespace. Shorthand: Xmlns:xsi section Add a pair of AOP schema files)
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
If you do not configure the above xmlns URI then it will throw the following "wildcard match is quite comprehensive, but cannot find the element ..." error, as shown below (solution is to add the above xmlns)
Note that Xmlns:xsi is presented as a pair of URI references (must be written in a pair, i.e. a physical location for a document that is a space name and a pattern, or an error that throws up the picture shown above), in addition, the XmlNamespace must be empty between the numerous URIs
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Space Name
Document physical space in http://www.springframework.org/schema/aop/spring-aop-2.5.xsd mode
The physical location of the document in the schema: we can find the special spring.handlers and spring.schemas two files in the Meta-inf file in the Spring-aop.xxx.jar package, they are the URI addresses that store the namespace, Spring in order to break the network can still find these files are placed in these two files, and the old version of the spring namespace URI address is also very thoughtful to give out, in order to prevent the old spring version of the URI update of the domain name, the spring Xmsns:xsi the impact of the damage.
2.jar Pack
Jar Package requirements, add the following under the usual spring jar package
Three jar packages:
AOP Alliance-xx.jar:
Aspectsweaver-xxx.jar: This jar is added @aspect annotations
Spring-aop-xxx.jar
There are four kinds of sayings on the Internet (I'll put it out for the sake of insurance)
On the basis of the above three kinds, add: Spring-aspects-xxx.jar
Note: XXX is a version of the meaning
All right, all of my jar packages are as follows, and generally there are no problems with the following JAR packages AOP running.
Added jar package environment Well, let's start our first @aspect example.
1. Create two classes, a generic class (Bean Component class is also the heart bean), one is the Aspect class service class (In fact, also a common Java class)
Package test;
Import org.springframework.stereotype.Component;
@Component //The note indicates that the class is a generic bean class, but there is no indication that he will be serviced
public class One {
public void SayHello () {
system.out.println ("Hello aject!");
}
public static void Main (string[] args) {
&nbs p; one o=new One ();
o.sayhello ();
}
}
Package test;
Import Org.aspectj.lang.annotation.Around;
Import Org.aspectj.lang.annotation.Aspect;
@Aspect//defines a facet, which means that a particular step of the business process is running, that is, the focus of the application's running process, that the focus may be crosscutting across multiple objects, and is often referred to as crosscutting concerns.
public class-{
@Around ("Execution (void One.sayhello ())")//also @around is an enhanced processing, besides the before and after
public void Test ()
{
SYSTEM.OUT.PRINTLN ("Impersonation execution permission check");
}
}
2. Write the test class
public class Test {
public static void Main (string[] args) {
ApplicationContext ac=new classpathxmlapplicationcontext ("Bean.xml");
One O=ac.getbean (One.class);
O.sayhello ();
}
}
Final output results
Question 1: This does not output Hello AJECTJ!!! Because I quoted @around annotations.
Advice (Enhanced processing): @before output enhancement statements before the output of the subject sentence output
@after output Enhancement Statements after output of the subject sentence output
@around The example above shows
Issue 2: Enhanced processing does not work when using System.out.print with run classes, and the enhanced processing is not fired if you do not use System.out.print instead of the return statement