基于@Aspectj 的 AOP:未调用建议
@Aspectj based AOP : Advice is not getting called
在此处阅读相关查询,但其中 none 有帮助。
这是我的 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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<bean id="advice1" class="Aspects.Advice"></bean>
<bean id="module1" class="objects.Modules">
<constructor-arg index="0"> <ref bean="resource1"/> </constructor-arg>
<constructor-arg index="1" value="10 Oct"></constructor-arg>
<constructor-arg index="2" value="11 Oct"></constructor-arg>
<property name="moduleName" value="SaleLayaway"></property>
</bean>
<bean id="resource1" class="objects.Resource">
<property name="name" value="Smruti"></property>
<property name="designation" value="PA"></property>
<property name="teamName" value="BackEnd"></property>
</bean>
</beans>
这是我的看点:
package Aspects;
@Aspect
public class Advice {
@Pointcut("execution(public void Modules.displayModule*.*(..))")
public void pointCut1()//point cut name
{
}
@Before("pointCut1()")
public void inputLogger(JoinPoint jp)
{
System.out.println(" inside advice");
System.out.println("We are gonna start service signature :"+jp.getSignature());
System.out.println("Target name: "+jp.getTarget());
}
}
这是我的主要 "Modules" 方法,有 "displayModuleInfo" 方法,我需要在其中添加建议:
package objects;
public class Modules {
private Resource rescource;
private String startDate;
private String finsihDate;
private String moduleName;
public String getModuleName() {
return moduleName;
}
public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}
public Modules(Resource rescource, String startDate, String finsihDate) {
super();
this.rescource = rescource;
this.startDate = startDate;
this.finsihDate = finsihDate;
}
public Modules(){
}
/*@Autowired
public Modules(Resource rescource) {
super();
this.rescource = rescource;
}*/
public void displayModuleInfo(){
System.out.println(" module name: "+moduleName);
System.out.println(" Resource name : "+rescource.getName()+":Designation :"+rescource.getDesignation()+" : team name :"+rescource.getTeamName());
System.out.println(" module start date :"+startDate+" : finish date : "+finsihDate);
}
}
我无法确定为什么建议不起作用。这是 o/p 我正在
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
module name: SaleLayaway
Resource name : Smruti:Designation :PA : team name :BackEnd
module start date :10 Oct : finish date : 11 Oct
我为 AOP 添加了这些 jar:
我在这里遗漏了什么明显的东西?
您的切入点声明似乎格式不正确。我创建了一个示例应用程序,它似乎应该是:
@Pointcut("execution(public void objects.Modules.displayModule*(..))")
public void pointCut1()//point cut name
{
}
此声明匹配任何方法,其中名称以 displayModule
开头,由 objects.Modules
class 声明。 IIRC 你的声明将匹配任何方法,由 class 声明,名称以 displayModule
开头,位于 Modules
包中。
如果您错过了,reference has great examples on how to create pointcuts。
在此处阅读相关查询,但其中 none 有帮助。
这是我的 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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<bean id="advice1" class="Aspects.Advice"></bean>
<bean id="module1" class="objects.Modules">
<constructor-arg index="0"> <ref bean="resource1"/> </constructor-arg>
<constructor-arg index="1" value="10 Oct"></constructor-arg>
<constructor-arg index="2" value="11 Oct"></constructor-arg>
<property name="moduleName" value="SaleLayaway"></property>
</bean>
<bean id="resource1" class="objects.Resource">
<property name="name" value="Smruti"></property>
<property name="designation" value="PA"></property>
<property name="teamName" value="BackEnd"></property>
</bean>
</beans>
这是我的看点:
package Aspects;
@Aspect
public class Advice {
@Pointcut("execution(public void Modules.displayModule*.*(..))")
public void pointCut1()//point cut name
{
}
@Before("pointCut1()")
public void inputLogger(JoinPoint jp)
{
System.out.println(" inside advice");
System.out.println("We are gonna start service signature :"+jp.getSignature());
System.out.println("Target name: "+jp.getTarget());
}
}
这是我的主要 "Modules" 方法,有 "displayModuleInfo" 方法,我需要在其中添加建议:
package objects;
public class Modules {
private Resource rescource;
private String startDate;
private String finsihDate;
private String moduleName;
public String getModuleName() {
return moduleName;
}
public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}
public Modules(Resource rescource, String startDate, String finsihDate) {
super();
this.rescource = rescource;
this.startDate = startDate;
this.finsihDate = finsihDate;
}
public Modules(){
}
/*@Autowired
public Modules(Resource rescource) {
super();
this.rescource = rescource;
}*/
public void displayModuleInfo(){
System.out.println(" module name: "+moduleName);
System.out.println(" Resource name : "+rescource.getName()+":Designation :"+rescource.getDesignation()+" : team name :"+rescource.getTeamName());
System.out.println(" module start date :"+startDate+" : finish date : "+finsihDate);
}
}
我无法确定为什么建议不起作用。这是 o/p 我正在
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
module name: SaleLayaway
Resource name : Smruti:Designation :PA : team name :BackEnd
module start date :10 Oct : finish date : 11 Oct
我为 AOP 添加了这些 jar:
我在这里遗漏了什么明显的东西?
您的切入点声明似乎格式不正确。我创建了一个示例应用程序,它似乎应该是:
@Pointcut("execution(public void objects.Modules.displayModule*(..))")
public void pointCut1()//point cut name
{
}
此声明匹配任何方法,其中名称以 displayModule
开头,由 objects.Modules
class 声明。 IIRC 你的声明将匹配任何方法,由 class 声明,名称以 displayModule
开头,位于 Modules
包中。
如果您错过了,reference has great examples on how to create pointcuts。