目錄結構:
引入的jar包
aspect.Validate.java
package aspect;
import java.text.*;
import java.util.*;
public class Validate {
public void login() {
System.out.println("登陸成功!");
}
public void writeLog() {
System.out.println("登陸時間:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
domain.American.java
package domain;
public class American implements Talker{
public void say() {
System.out.println("Hello World!");
}
}
domain.Chinese.java
package domain;
public class Chinese implements Talker{
public void say() {
System.out.println("你好,世界!");
}
}
domain.Talker
package domain;
public interface Talker {
public void say();
}
Test.Client.java
package Test;
import org.springframework.context.ApplicationContext;
import java.util.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import domain.*;
public class Client {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("Application.xml");
Talker chn = (Talker) act.getBean("chn");
chn.say();
}
}
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"
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 id="chn" class="domain.Chinese"></bean>
<bean id="validate" class="aspect.Validate"></bean>
<aop:config>
<aop:pointcut
expression="execution(* domain.Chinese.*(..))" id="pointcut" />
<aop:aspect ref="validate">
<aop:before pointcut-ref="pointcut" method="login" />
<aop:after pointcut-ref="pointcut" method="writeLog" />
</aop:aspect>
</aop:config>
</beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zlm</groupId>
<artifactId>MyMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>