指示符 | 作用 |
---|---|
bean | 匹配指定类型的方法 |
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.ssttisme</groupId>
<artifactId>spring_aop_expression</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!--AOP的实现基于AspectJ框架 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
</dependencies>
</project>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--配置组件扫描 -->
<context:component-scan base-package="com" />
<!--启用aop为bean创建动态代理对象-->
<aop:aspectj-autoproxy/>
</beans>
package com.service;
public interface RoleService {
void save(String role);
int update(String role);
}
package com.service.impl;
import org.springframework.stereotype.Component;
import com.service.RoleService;
@Component
public class RoleServiceImpl implements RoleService{
@Override
public void save(String role) {
System.out.println("save role "+role);
}
@Override
public int update(String role) {
System.out.println("update role "+role);
return 1;
}
}
package com.aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**切面*/
@Aspect
@Component
public class LogManager {
@Before("bean(*ServiceImpl)")
public void beforeAdvice(){
System.out.println("开始日志。。。");
}
@After("bean(roleServiceImpl)")
public void afterAdvice(){
System.out.println("结束日志。。。");
}
}
package com.app;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.RoleService;
public class RunApp {
private static ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext();
//初始化
public static void init(){
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
}
//释放资源
public static void destory(){
ctx.close();
}
public static void main(String[] args) {
init();
RoleService roleService=ctx.getBean("roleServiceImpl",RoleService.class);
roleService.update("normal user");
destory();
}
}
运行结果
开始日志。。。
update role normal user
结束日志。。。