没有征求意见



我有一个简单的spring应用程序,代码如下:

方面加载器类软件包com.ishan.spring.aspx加载程序;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ishan.spring.services.ShapeService;
public class AspectLoader {
public static void main(String a[]){
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    context.registerShutdownHook();
    ShapeService service = (ShapeService) context.getBean("shapeService");
    service.draw();
}

}

ShapeService类

package com.ishan.spring.services;
import com.ishan.spring.interfaces.Shape;
public class ShapeService {
public Shape getShape() {
    return shape;
}
public void setShape(Shape shape) {
    this.shape = shape;
}
public String draw1(){
    System.out.println("String draw called");
    this.shape.draw();
    return "drawn";
}
 public int draw(){
     System.out.println("int draw called");
     draw1();
    return 1;
 }
private Shape shape;
}

圆圈类

package com.ishan.spring.impl;
import com.ishan.spring.interfaces.Shape;
public class Circle implements Shape{
private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Override
public void draw() {
    System.out.println("Circle drawn");
}

}

形状界面

package com.ishan.spring.interfaces;
public interface Shape {
public void draw();
}

日志方面

package com.ishan.spring.aspects;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(public * draw*(..))")
public void logBefore(){
    System.out.println("Advice run before method call");
}
} 

spring.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"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop      http://www.springframework.org/schema/aop/spring-aop.xsd
    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">
<aop:aspectj-autoproxy/>
<bean id="circle" class="com.ishan.spring.impl.Circle">
    <property name="name" value="myCircle"></property>
</bean>
<bean id="triangle" class="com.ishan.spring.impl.Triangle">
    <property name="name" value="myTriangle"></property>
</bean> 
<bean id="shapeService" class="com.ishan.spring.services.ShapeService">
    <property name="shape" ref="triangle"></property>
</bean> 
    <bean id="logAspect" class="com.ishan.spring.aspects.LoggingAspect"/>
</beans>

问题是我无法在shapeservice类的draw1()方法之前运行我的建议。我无法解决通配符表达式中的问题。

您的通配符表达式很好。

方面没有在draw1()方法上激发的原因是它是从draw()调用的;它在同一bean中,即简单的java方法调用。

如果从其他springbean调用draw1()或类似于调用draw()),那么方面肯定会启动。

要亲自查看,请在下方尝试

ShapeService service = (ShapeService) context.getBean("shapeService");
service.draw1();

最新更新