Aspect Class:
@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()&amp;&amp; e2()”) 当我单独调用e1()或e2()时,它可以同时工作,但不能同时工作。 我没有得到任何错误。只是建议没有被调用。 我使用的是弹簧3.2.3 AspectJ和AOP联盟jar文件
答案 0 :(得分:2)
这是预期的。切入点永远不会匹配它将永远不会同时匹配执行e1和执行e2。而不是&&
您可能需要||
。
它基本上是一个if语句,双方都必须解析为true
a