Announcement Announcement Module
Collapse
No announcement yet.
BeanFactoryPostProcessor and overwritting aspects - very strange behaviour Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


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

      Thanks Marten.

      Comment


      • #4
        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).

        Comment


        • #5
          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?

          Comment


          • #6
            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.

            Comment


            • #7
              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

              Comment


              • #8
                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.

                Comment


                • #9
                  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")?

                  Comment


                  • #10
                    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.

                    Comment

                    Working...
                    X