Announcement Announcement Module
Collapse
No announcement yet.
Reference to bean prevents weaving Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reference to bean prevents weaving

    I have a scenario where a REFERENCE to a bean within TransactionCallbackWithoutResult in a test causes the bean not to get weaved.

    Basic aspectj transaction setup:

    Code:
        <tx:annotation-driven mode="aspectj"/>
        
        <bean id="transactionManager"
            class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    Trivial bean:

    Code:
    @Transactional
    public class InstrumentMaster {
        @PersistenceContext
        private EntityManager em;
    
        public Instrument findInstrument(InstrumentType type, InstrumentName name) {
            return (Instrument) em.createNamedQuery(...).getSingleResult();
        }
    }
    Trivial test:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"/META-INF/domain-test.ctx.xml"})
    public class InstrumentMasterTest {
    
        ...
    
        @Test
        public void findInstrument() {
            // Invocation of @Transactional method
            Instrument instrument = instrumentMaster.findInstrument(...);
            assertThat(...)
        }
    
        public void referenceTransactionalBean() {
            new TransactionCallbackWithoutResult() {
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    // *****
                    // If this reference is commented out, InstrumentMaster is weaved.
                    // If this reference is moved outside the inner class, InstrumentMaster is weaved
                    InstrumentMaster m = instrumentMaster;
                }
            };
    }

    See the "//****" commented line above. Simply referencing "instrumentMaster" within the inner class declaration causes the class NOT to be weaved.

    If the body of the inner class is extracted into a top-level method and invoked from within the inner class, then the bean IS weaved. eg:

    Code:
        public void referenceTransactionalBean() {
            new TransactionCallbackWithoutResult() {
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    // InstrumentMaster IS weaved
                    doReferenceTransactionalBean();
                }
            };
        }
    
        private void doReferenceTransactionalBean() {
                InstrumentMaster m = instrumentMaster;        
        }
    It may very well be that this is not specific to how the spring aspects are defined, but rather an aspectj bug. I an running within the new spring-eclipse which uses aspectj 6.x.

    Has anyone ever seen such behavior?

    cheers

    -barry

  • #2
    Barry,

    I wonder if this is somehow related to http://jira.springframework.org/browse/SPR-4576.

    What kind of AspectJ weaving you are using?

    It looks like you isolated the problem in a test case. If you can, please raise an issue along with that test case.

    -Ramnivas

    Comment

    Working...
    X