Rajeev Akotkar
Rajeev Akotkar

Reputation: 1397

aop with spring 2.5 without Aspect J

I am working in a old project with spring 2.5.Application context starts with .

I need to implment logging using aop I need to log each and every class's method. tried this link : http://forum.spring.io/forum/spring-projects/aop/4769-apply-jdkregexpmethodpointcut-to-multiple-beans-how.But didnt work. and some more options.(but i felt this is taking me some where) Also, I cant use xsd so i cant use aop namespace.i cant use aspect j as well

Please guide me on how can i achieve this what should be point cut as I have tried . and * as pattern and bean names with the point cut.

Upvotes: -1

Views: 96

Answers (1)

Betlista
Betlista

Reputation: 10549

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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="debugInterceptor" class="test.DebugInterceptor" />

    <bean id="testBean" class="test.TestBean" />

    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      <property name="beanNames" value="*" />
      <property name="interceptorNames">
        <list>
          <value>debugInterceptor</value>
        </list>
      </property>
    </bean>
</beans>

TestBean

package test;

public class TestBean {

    public void foo() {
        System.out.println("foo");
    }

    void bar() {
        System.out.println("bar");
    }

}

DebugInterceptor

package test;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class DebugInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Before: invocation=[" + invocation + "]");
        Object rval = invocation.proceed();
        System.out.println("Invocation returned");
        return rval;
    }

}

AopTest

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopTest {

    public static void main(String[] args) {
        ApplicationContext ap = new ClassPathXmlApplicationContext("classpath:spring.xml");
        TestBean bean = (TestBean)ap.getBean("testBean");
        bean.foo();
        bean.bar();
    }

}

Upvotes: 0

Related Questions