Feb 11th, 2008, 03:56 AM
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?
Feb 11th, 2008, 04:14 AM
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...
Feb 11th, 2008, 04:25 AM
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.
Originally Posted by mdeinum
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.
Feb 11th, 2008, 04:34 AM
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....
Feb 11th, 2008, 04:35 AM
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 ...
Feb 11th, 2008, 05:46 AM
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.
Feb 11th, 2008, 06:19 AM
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...
Feb 11th, 2008, 06:31 AM
If I perform the following:
Originally Posted by mdeinum
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.