Announcement Announcement Module
No announcement yet.
need help in this first aop pointcut Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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 to set the Pointcut
    public class MyInterceptor {
    	@Pointcut("execution (* spring.service.impl.PersonServiceBean.*(..))")
    	private void anyMethod() {}	
    	public void doAccessCheck() 
    		System.out.println("before advice");
    Any my beans2.xml file:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    <bean id="personService" class="spring.service.impl.PersonServiceBean"></bean>
    <bean id="myInterceptor" class="spring.service.MyInterceptor"></bean>
    Any my junit test file:
    public class SpringAOPTest {
    	public static void setUpBeforeClass() throws Exception {
    	public void interceptorTest() {
    		ApplicationContext cxt;
    		cxt = new ClassPathXmlApplicationContext("beans2.xml");
    		PersonService personService = (PersonService)cxt.getBean("personService");
    myPersonServiceBean has a save method which just prints a line just for testing.
    the console output is:

    Apr 04, 2012 2:08:12 PM prepareRefresh
    INFO: Refreshing display name []; 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 obtainFreshBeanFactory
    INFO: Bean factory for application context []:
    Apr 04, 2012 2:08:13 PM preInstantiateSingletons
    INFO: Pre-instantiating singletons in defining beans [org.springframework.aop.config.internalAutoProxyCreator,personService,myInterceptor]; root of factory hierarchy
    Apr 04, 2012 2:08:13 PM destroySingletons
    INFO: Destroying singletons in 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

    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:

    public class SpringAOPTest
    	private PersonServiceInterface service;
    	public void testSave()
    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.