Results 1 to 10 of 10

Thread: BeanFactoryPostProcessor and overwritting aspects - very strange behaviour

  1. #1
    Join Date
    Nov 2012
    Posts
    11

    Question BeanFactoryPostProcessor and overwritting aspects - very strange behaviour

    Hello,

    I have defined two aspects in my basic applicationContext xml file:
    Code:
    <aop:config>
    	<aop:aspect id="decoratorAspect" ref="decorator">
    		<aop:pointcut expression="execution(* com.example.*.*(..))" id="pointcutDecorator"/>
    		<aop:before method="before" pointcut-ref="pointcutDecorator"/>
    		<aop:after-returning method="after" pointcut-ref="pointcutDecorator"/>
    	</aop:aspect>
    
    	<aop:aspect id="loggingAspect" ref="logging">
    		<aop:pointcut expression="execution(* com.example.*.*(..))" id="pointcutLogging"/>
    		<aop:before method="before" pointcut-ref="pointcutLogging"/>
    		<aop:after-returning method="after" pointcut-ref="pointcutLogging"/>
    	</aop:aspect>
    </aop:config>
    The pointcut expression for loggingAspect is different for different instances of application. So I want to overwrite the loggingAspect in BeanFactoryPostProcessor. So I take new loggingAspect definition:
    Code:
    <aop:config>
    		<aop:aspect id="loggingAspect" ref="logging">
    			<aop:pointcut expression="execution(* com.example.specified.*(..))" id="pointcutLogging"/>
    			<aop:before method="before" pointcut-ref="pointcutLogging"/>
    			<aop:after-returning method="after" pointcut-ref="pointcutLogging"/>
    		</aop:aspect>
    	</aop:config>
    and register it in beanFactory. But then only the loggingAspect is invoked. The decoratorAspect is not invoked. How can I overwrite or add new aspects to the applicationContext?

    Thanks for your help

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You are overwriting the full aop:config block ...

    Also why do you want this... Why not simply define the expression in a properties file and use a PropertyPlaceHolder to configure it... Saves you overwriting a lot of stuff.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2012
    Posts
    11

    Default

    OK, but what in case that I need to add new aspects?

    Thanks Marten.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Why would you want to add new aspects and I wouldn't use an BeanFactoryPostProcessor to do that. The easiest is to externalize the configuration. Make the aspect-context.xml (or where ever your aspects are defined) external to your application (on the file system) that way you can always (re)configure the aspects on a per application install basis. It also saves you hacking around with a BeanFactoryPostProcessor trying to achieve your goal (try to keep it simple is my suggestion basically).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Nov 2012
    Posts
    11

    Default

    I have two aspects which have to always enabled and then I have few aspects which depends on instance of app. Please correct me if I understand it wrong - if I will have aspect configuration in separate xml file and then import it in main applicationContext xml file then in this file I have to have all aspects definitions (the basic two and additional aspects), so I will have several xml files and all of them will contain two basic aspects definitions.
    And what in case when I import xml file in another file and both of them contains <aop:config> block? Will it be merged or overwriten?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You can have multiple aop:config blocks without problems so you can simply add the 2 always present aspects to your normal configuration (although I find it dubious that the pointcut changes for the same application!). Then simply import an xml file containing the additional aspects. Or simply create 2 files one with the basic ones (if you really need to change the pointcut) and one with additional aspects.

    Configuration that changes between instances of the application should be part of the core of the application but should be configurable (like properties for instance). Hence my suggestion for putting them into an external file.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Nov 2012
    Posts
    11

    Default

    Ok, so I can have multiple aop:config blocks in xml but when I use BeanFactoryPostProcessor then aop:config is overwritten, right?

    Thank you very much Marten

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    It basically depends on how you implemented the BeanFactoryPostProcessor if you simply override beans they get replaced, t he spring namespace handlers have detection code in them which discover if beans have already been registered or not.

    But as mentioned I wouldn't use a BFFP for this I would simply use an external xml file. That way it is only configuration and you don't have additional code to maintain.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Nov 2012
    Posts
    11

    Default

    I have one more question. Although my aspect has id="loggingAspect" the bean created has name org.springframework.aop.aspectj.AspectJPointcutAdv isor#0. Is it possible that my aspect bean will have the name from id ("loggingAspect")?

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Your aspect has that name there are however some other beans created which have generated bean names.

    I still go with my advice to use external files which is a lot easier then shoehorning some BFFP in place.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •