AOP advices in conjuction with TX
need a little mental help to get my brain oriented right with AOP/TX in spring....
recently ran into an error that popped up while using a service inside an AOP advice. namely that the service might not be initialized prior to injecting into the advice/pointcut (typical allowInit warning). the service is proxied as transactional by using the autoproxy on a transactionManager pairing up methods with transaction types. assuming AOP is used behind the scenes. and that when I define my AOP advice the order of which advice (the spring ready made transaction AOP and my own) is unknown.
to get around the problem i simply marked the service bean with lazy-init equal to true so that it is built only when needed. not at init time (i assume?).
wondering when using the TX advices with spring 2.0 if one can specify order precedence so the there isn't a conflict of when the service is made into a transactional bean? or am i off the deep end? assuming that the root cause of my origal error was due to unknown order in kicking off AOP advices (trans/my own).
guess i am looking for a general blueprint for how to configure transactional beans (services) and AOP advices acting on those services (beans).
thanks in advance / matthew
forgot to post the most important part (transaction)
forgot the most important configuration (namely the transaction advices):
management XML file
Code:
<!-- Hibernate TM (active in non-container) *installation parameter -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--:Transaction attributes for Services -->
<bean id="transactionAttributes" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="get*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="page*">PROPAGATION_REQUIRED</prop>
<prop key="batch*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- Transaction Interceptor for Services -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributes"/>
</property>
</bean>
<!-- AutoProxy for Services -->
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<value>transactionInterceptor</value>
</property>
<property name="beanNames">
<value>*Service</value>
</property>
</bean>
suggestions for reworking
any suggestions for reworking the configuration?
thought at the Order interface might be the problem. but basically i want to apply business logic dynamically to the service without coding directly in the service implementation. ie. the extra logic that the advice adds against the service may not be necessary in 4/5 months so there is no point in adding it inside the service implementation.
/ matthew
This may be too little too late, but...here is what I found to be the problem when I
This may be too little too late, but...here is what I found to be the problem when I recieved this error....
(you know which error I mean):
Code:
Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor': Cannot create inner bean '(inner bean)' while setting bean property 'advice'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'loggerInterceptor' while setting bean property 'aspectBean'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'loggerInterceptor': Bean with name 'loggerInterceptor' has been injected into other beans [(inner bean)#2, (inner bean)#4] in its raw version as part of a circular reference, but has eventually been wrapped (for example as part of auto-proxy creation). This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
I was matching the Interceptor itself when I received that error!
I believe there is another word for this, it is called Circlular Reference
Try placing the interceptor in a different package from the one you are matching. And DO NOT match it with a pointcut.
Thank you,
Andrew J. Leer