1.add <aop:aspectj-autoproxy/> to spring config xml

2.create a class and add @Aspect at the class

package tutorial;

import org.aspectj.lang.annotation.Aspect;

@Aspect
@Component
public class SystemArchitecture {

}

3.declare a pointcut in the class

package tutorial;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
@Component
public class SystemArchitecture {

 @Pointcut("execution(* execute(..))")
 public void executePointcut() {
 }

}

4.declare a before advise in the class and use the "executePointcut" pointcut

package tutorial;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
@Component
public class SystemArchitecture {

 @Pointcut("execution(* execute(..))")
 public void executePointcut() {
 }

 @Before("executePointcut()")
 public void before() {
  System.out.println("Before");
 }

}


5.this is already match all class when run method-name is "execute" the advise will auto run at the method before
create a demo class

package tutorial;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class Demo {

 public void execute() {
  System.out.println("hello world");
 }

 public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext(
    "applicationContext.xml");
  Demo demo = (Demo) context.getBean("demo");
  demo.execute();
 }

}


It will be output
Before
hello world


the applicationContext.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-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <context:annotation-config />
 <context:component-scan base-package="tutorial" />
 <aop:aspectj-autoproxy />

</beans>