PDA

View Full Version : Architecture Question



sfwalter
Mar 3rd, 2005, 08:32 PM
I am migrating an older dao-based application to the Spring Framework. I have a pojo mode object called "ContentItem" and one of the properties is called "status". When the status is changed I want to fire off an event, to send an email. or update a row in table based on the status.

My question is how can I get access to the ApplicationContext from with the "ContentItem" model object so I can fire an event to my listeners? Inside my applicationContext.xml I have references to my service object and my dao object, but not the model objects? I followed the design patterns of the Spring in Action book where you access your model objects from the DAO layer.

Are there any best practices around this. A bad solution would be to create an Singleton object that holds an instance of ApplicationContext and therefore I can access to my ApplicationContext from in any Java object, not just ones that are defined in my applicationContext.xml.

I would appreciate any suggestions. Thanks in advance.

scott

csing.ng
Mar 4th, 2005, 12:34 AM
use AOP

Costin Leau
Mar 4th, 2005, 02:36 AM
I see several approaches to your problem, both based on AOP:

1. Spring AOP
You can intercept the setter method of your pojo and each time it's called with a certain parameter (or if it returns an agreed value) it should start the email logic. However for that you have to define the POJO inside the Spring ApplicationContext. Most application that use Hibernate do not contain the model objects because the model object do not have to be injected - they are created by the application or retrieved from the database - there is no need for managing them.

2. Hibernate AOP
This applies only if you are using Hibernate - you can create an interceptor that will always check the updated objects - in case the property of the POJO (which hits the database) is changed you start doing your logic. I assumed here that the POJO (ContentItem) is saved to the database.

Alarmnummer
Mar 4th, 2005, 03:04 AM
Warning:
if you fire a property change before the transaction is closed, and the transaction is rollbacked, there could be an atomic problem.

sfwalter
Mar 4th, 2005, 12:06 PM
I am using Hibernate, how do I get Hibernate to get the beans from my ApplicationContext. I attempted to add the following to my application context:

<bean id="contentItem" class="com.scottwalter.yoursite.model.ContentItem" init-method="init" lazy-init="true"/>

However my "init" method is never called.

I am struggling wit how to integrate my beans that Hibernate manages into my application context.

Any help would be appreciated.

Alarmnummer
Mar 4th, 2005, 01:25 PM
I am using Hibernate, how do I get Hibernate to get the beans from my ApplicationContext. I attempted to add the following to my application context:

<bean id="contentItem" class="com.scottwalter.yoursite.model.ContentItem" init-method="init" lazy-init="true"/>

However my "init" method is never called.

Answer to your question:
Do you retrieve the bean from the application context? If yes.. the bean should be constructed and your init method called.

Not the answer to your question but the answer to your needs:
Your ContentItem should not be mentioned in the application context. In the applicationcontext you should construct your dao`s, services etc. And those objects know how to deal with your entities... Individual entities don`t belong to the applicationcontext.

Please check a tutorial for Spring and you will get your answers.. See spring at this moment as glue... nothing more.. nothing less.

sfwalter
Mar 4th, 2005, 02:47 PM
Based on this thread http://forum.springframework.com/viewtopic.php?t=301 I got the following classes out of the sandbox:

org.springframework.beans.factory.support.Dependen cyInjectionAspectSupport
org.springframework.orm.hibernate.support.ChainedI nterceptorSupport
org.springframework.orm.hibernate.support.Dependen cyInjectionInterceptorFactoryBean

Then I added the following to my applicationContext

<bean id="sessionFactory" lazy-init="true"
class="org.springframework.orm.hibernate.LocalSessionFact oryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>

<!-- Set up the Dependency Injector-->
<property name="entityInterceptor"><ref local="dependencyInjectionInterceptor"/></property>

<property name="mappingResources">
<list>
<!--ommitted-->
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
net.sf.hibernate.dialect.HSQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
</bean>

<bean id="dependencyInjectionInterceptor"
class="org.springframework.orm.hibernate.support.Dependen cyInjectionInterceptorFactoryBean">
<property name="sessionFactoryName"><value>sessionFactory</value></property>
<property name="defaultAutowireMode"><value>0</value></property>
<property name="managedClassNamesToPrototypeNames">
<props>
<prop key="com.scottwalter.yoursite.model.ContentItem">contentItem</prop>
</props>
</property>
</bean>

At this point, my model class can implement ApplicationContextAware