【发布时间】: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]);
}
}
【问题讨论】: