开发者

Retrieving bean in AspectJ class using Spring

开发者 https://www.devze.com 2023-03-05 20:43 出处:网络
I am using aspectJ class for Exception Handling aspect in Spring. i need to read values from properties files which is defined in spring bean.

I am using aspectJ class for Exception Handling aspect in Spring. i need to read values from properties files which is defined in spring bean. Presently I am reading property file using the context. is there any other option. earlier while i was using spring aop ,proxy object automatically read the properties file without accessing through the context.

Spring Config File

<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 Class(i have to use context for reading propeties here)

 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]);


}

 }


You can wire up your aspect using the static aspectOf factory method (you can't see that method, it is added by the aspectj compiler)

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

Reference:

  • aspectOf() and hasAspect() methods
  • Spring Dependency Injecting an annotated Aspect
0

精彩评论

暂无评论...
验证码 换一张
取 消