【问题标题】:Retrieving bean in AspectJ class using Spring使用 Spring 在 AspectJ 类中检索 bean
【发布时间】:2016-12-27 01:20:14
【问题描述】:

我在 Spring 中将 aspectJ 类用于异常处理方面。 我需要从 spring bean 中定义的属性文件中读取值。 目前我正在使用上下文读取属性文件。 有没有其他选择。 早些时候,当我使用 spring aop 时,代理对象会自动读取属性文件,而无需通过上下文访问。

弹簧配置文件

<bean id="applicationProperties"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location">
        <value>classpath:/resources/config/application.properties</value>
    </property>
</bean>

public Properties exceptionProp = null;

ExceptionHandlingAspect 类(我必须在这里使用上下文来读取属性)

 public class ExceptionHamdlingAspect{

public void setExceptionProp(Properties exceptionProp) {
    this.exceptionProp=exceptionProp;
}

 @AfterThrowing(pointcut = "ExceptionHandlingAspect()", throwing = "ex")
public void logAfterThrowingException(final JoinPoint currentJp,
        Throwable ex) throws Exception {

ApplicationContext ctx = AppContext.getApplicationContext();
this.exceptionProp=(Properties) ctx.getBean("applicationProperties");   


PropertyReader.getValueForProperty(ex.getClass().getSimpleName(),exceptionProp);
System.out.println("error values :"+errorString[0]+ errorString[1]);


}

 }

【问题讨论】:

    标签: java spring aspectj


    【解决方案1】:

    您可以使用静态 aspectOf 工厂方法连接您的方面(您看不到该方法,它是由 aspectj 编译器添加的)

    <bean class="com.YourAspect" factory-method="aspectOf">
        <property name="exceptionProp"
                  value="classpath:path/to/propfile.properties" />
    </bean>
    

    参考:

    【讨论】:

    • 我添加了一个新的question。请通过它
    猜你喜欢
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多