Announcement Announcement Module
Collapse
No announcement yet.
need help in this first aop pointcut Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • need help in this first aop pointcut

    Hi I'm trying to implement Pointcut but the Application context automatically destroies itself I have no idea why.

    I have this MyInterceptor.java to set the Pointcut
    Code:
    @Aspect
    public class MyInterceptor {
    	@Pointcut("execution (* spring.service.impl.PersonServiceBean.*(..))")
    	private void anyMethod() {}	
    
    	@Before("anyMethod()")
    	public void doAccessCheck() 
    	{
    		System.out.println("before advice");
    	}
    
    }
    Any my beans2.xml file:
    Code:
    <?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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation=" http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    	
    <bean id="personService" class="spring.service.impl.PersonServiceBean"></bean>
    
    <bean id="myInterceptor" class="spring.service.MyInterceptor"></bean>
    
    </beans>
    Any my junit test file:
    Code:
    public class SpringAOPTest {
    
    	@BeforeClass
    	public static void setUpBeforeClass() throws Exception {
    	}
    
    	@Test
    	public void interceptorTest() {
    		
    		ApplicationContext cxt;
    		
    		cxt = new ClassPathXmlApplicationContext("beans2.xml");
    		PersonService personService = (PersonService)cxt.getBean("personService");
    		
    		personService.save();
    	}
    
    }
    myPersonServiceBean has a save method which just prints a line just for testing.
    the console output is:

    Code:
    Apr 04, 2012 2:08:12 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19422d2: display name [org.springframework.context.support.ClassPathXmlApplicationContext@19422d2]; startup date [Wed Apr 04 14:08:12 EDT 2012]; root of context hierarchy
    Apr 04, 2012 2:08:12 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [beans2.xml]
    Apr 04, 2012 2:08:13 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
    INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@19422d2]: org.springframework.beans.factory.support.DefaultListableBeanFactory@f6925f
    Apr 04, 2012 2:08:13 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f6925f: defining beans [org.springframework.aop.config.internalAutoProxyCreator,personService,myInterceptor]; root of factory hierarchy
    
    Apr 04, 2012 2:08:13 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
    INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f6925f: defining beans [org.springframework.aop.config.internalAutoProxyCreator,personService,myInterceptor]; root of factory hierarchy
    usually it will just stop at Pre-instantiating..... but now it destroySingletons right after it is initiated. I have tested a little but since I'm new to this, I have completely no idea what went wrong. Can somebody help! Thank you in advace

  • #2
    Hi,

    have you tried putting some system outs inside the person service itself? Do these get printed when you execute the test?
    I'm asking this because I don't think there's any error that prevents the Spring startup. My guess is that the app context is regularly created, then the test execution ends and the app context gets regularly destroyed.
    First thing to point out is that this is not the way to use JUnit with Spring. You should read the unit testing with Spring chapter in the reference guide and do something like this:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:beans2.xml"})
    public class SpringAOPTest
    {
            @Autowired
    	private PersonServiceInterface service;
    
    	@Test
    	public void testSave()
    	{
    		service.save();
    	}
    }
    And your PersonServiceBean should implement the PersonServiceInterface where the save() method is declared.
    Second thing to point out is, like you've done it aop isn't going to work because beans you get by programmatically calling context.getBean don't get directly proxied, so no Spring aop.

    I hope this was helpful.

    Comment

    Working...
    X