1.在Spring配置文件中添加 (红色部分)
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
2.编写切面类
//定义一个切面
@Aspect
public class BeforeDeviceTest {
//匹配serviceImpl下所有类的所有方法执行切入点
@Before("execution(* serviceImpl.*.*(..))") //@before 为在方法之前插入切入点 authority()为动作
public void authority()
{
System.out.println("模拟权限检测");
}
}
3.对需要进行切面操作的类中用 @Component Annotation进行标注
@Component
public class StudentMessServiceImpl implements StudentMessService
{
}
4.spring配置文件中配置自动搜索bean组件 自动搜索切面类
//配置自动搜索bean组件 自动搜索切面类
<context:component-scan base-package="serviceImpl,device">
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
//启动@AspectJ支持
<aop:aspectj-autoproxy/>