Results 1 to 8 of 8

Thread: Setting up AOP programatically

  1. #1
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Question Setting up AOP programatically

    How can I set ut AOP proxying programatically? My application has many services and to minimize the users need to set up a large context I have created a factory which the user can get all the services from.

    This factory will set up all the dependencies it self, but I do not know how to set up the transaction injection in my factory.

    Each service uses a wrapper class to access the database and HibernateTemplate. When I do not use the factory I have set up this wrapper class in the context as a ProxyFactoryBean which uses interceptorNames which points to a TransactionInterceptor which uses a HibernateTransactionManager.

    How can I set up this complex network of AOP transaction programatically in my factory?

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

    Default

    Why would you have to use a ProxyFactoryBean?! With Spring 2.0 and 2.5 you can use the aop:config blocks which automatically creates proxies for you. Even better/simpler when you use annotations and Spring 2.5 the only thing you need is 2 lines of xml.

    IMHO you are making it way to complex for yourself...
    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
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote Originally Posted by mdeinum View Post
    Why would you have to use a ProxyFactoryBean?! With Spring 2.0 and 2.5 you can use the aop:config blocks which automatically creates proxies for you. Even better/simpler when you use annotations and Spring 2.5 the only thing you need is 2 lines of xml.

    IMHO you are making it way to complex for yourself...
    The idea is to use the factory so that those who use this code does not need to set up a huge context. They only need to configure my Factory bean. This is a self created factory.

    However I choose to configure the wrapper class that uses transaction(Spring 1 or 2) I would need to set it up in my factory class programatically.

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

    Default

    The fact that you use a factory doesn't mean that you cannot create a ApplicationContext yourself (which is only used by your factory bean), that would save you a lot of trouble and time, that way you could still use the features of spring instead of re-inventing them yourself....
    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
    Jul 2005
    Location
    Geneva (Switzerland)
    Posts
    304

    Default

    If you are unable to use annotation based configuration (which gives you a 2 line config, as mdeinum said), you can also distribute a context that your clients only have to include that context and not care about configuring it.

    I think that using Spring, but getting rid of the declarative configuration is negating part of the advantage of Spring ...

    And to go back to your initial idea of setting up AOP programatically : I have no idea ...

  6. #6
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    A TransactionManager needs a DataSource and my project is not supposed to configure a datasource, the client is.

    By letting the client set the dataSource and not setting it in the factory application context would need to set it programatically. If I configure a fully functional application context with a reference to a dataSource and the client configures the dataSource in its application context then the factory application contex will not find the datasource bean.

    This problem probably occours because the application context opened in the factory does not know of the application context of the client that created the datasource and factory bean. How can I go around this?
    Last edited by DJViking; Feb 11th, 2008 at 05:51 AM.

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    I still see no reason why you cannot use an application context. Initialize it AFTER that the client has set it's DataSource, register that programmatically in your ApplicationContext (or create an app context that contains only the datasource and register that as the parent!) and do your stuff. That way the only programmatic stuff you need to do is register the datasource...

    Hmm maybe even better, let the client register their application context with your Factory and use that as a parent context...
    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

  8. #8
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote Originally Posted by mdeinum View Post
    I still see no reason why you cannot use an application context. Initialize it AFTER that the client has set it's DataSource, register that programmatically in your ApplicationContext (or create an app context that contains only the datasource and register that as the parent!) and do your stuff. That way the only programmatic stuff you need to do is register the datasource...

    Hmm maybe even better, let the client register their application context with your Factory and use that as a parent context...
    If I perform the following:
    applicationContext = ClassPathXMLApplicationContext("myContext.xml");
    the application context will be configured and complain about a missing dataSource. How can I register the dataSource with the application context before it is initialized?

    Edit: Your last suggestion worked fine. I made my factory ApplicationContextAware and created the application context with the client application context as parent.
    Last edited by DJViking; Feb 11th, 2008 at 06:35 AM.

Posting Permissions

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