方面类:
@Aspect
public class LoggingAspect {
@Before("e1() && e2()")
public void loggingAdvice(){
System.out.println("before execution of the method");
}
@Pointcut("execution(public String com.spring.Employee.getName())")
public void e1(){}
@Pointcut("execution(public String com.spring.Department.getName())")
public void e2(){}
}
客户端类:
公共类 AspectClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Employee emp = (Employee) context.getBean("employee");
System.out.println(emp.getEmpId());
System.out.println(emp.getName());
System.out.println(emp.getDepartment().getName());
}
**Config file:**
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id= "employee" class="com.spring.Employee" autowire="byName">
<property name="empId" value="7329" />
<property name="name" value="Sagar" />
</bean>
<bean id= "department" class="com.spring.Department" >
<property name="name" value="ApplicationManagement" />
<property name="typeOfProjects" value="Maintenance" />
</bean>
<bean class="com.spring.LoggingAspect"/>
<aop:aspectj-autoproxy />
<context:annotation-config />
</beans>
解释:
@Before("e1() && e2()") 当我单独调用 e1() 或 e2() 时,它可以工作,但不能同时调用。我没有收到任何错误。只是没有调用建议。我正在使用 spring 3.2.3 AspectJ 和 AOP 联盟 jar 文件