Announcement Announcement Module
Collapse
No announcement yet.
Injecting aspect into domain object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting aspect into domain object

    Good day!

    I want to introduce extra functionality (tags) into my domain (Account, Operation) object using aspect. So it would be possible to add tags and query for them, e.g. domain object would implement interface Taggable: addTag(), setTags(), getTags()....

    When I have a bean in spring container then it works well (Account instance of Taggable == true), but if I create it using operator new() then it fails.

    I also tried using @Configurable on domain object - it works well for injecting dependencies, but it does not introduce my advice.

    Is there a way to solve this issue?
    Thanks

    Here is some of my code:
    applicationContext.tags.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xsi:schemaLocation=
                 "http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans.xsd
                  http://www.springframework.org/schema/aop
                  http://www.springframework.org/schema/aop/spring-aop.xsd">
    
      <aop:spring-configured />
    
      <aop:aspectj-autoproxy />
    
      <aop:config proxy-target-class="true">
    
        <aop:aspect id="tagAspect" ref="com.gb.tags.aspect.EntityTagging">
          <aop:declare-parents
            types-matching="com.gb.account.model.CashAccount"
            implement-interface="com.gb.tags.aspect.Taggable"
            default-impl="com.gb.tags.aspect.TaggableImpl"/>
        </aop:aspect>
    
      </aop:config>
    
      <bean class="com.gb.tags.aspect.EntityTagging" />
    
      <bean class="com.gb.account.model.CashAccount"
        scope="prototype">
        <property name="name" value="test" />
      </bean>
    
    </beans>
    TagMixinTest.java
    Code:
    public class TagMixinTest extends AbstractSingleSpringContextTests {
    
        @Override
        protected String[] getConfigLocations() {
            return new String[] { "applicationContext.tags.xml", };
        }
    
        public void testTagMixinBean() {
            CashAccount account = (CashAccount) getApplicationContext().getBean(
                    "com.gb.account.model.CashAccount");
            assertNotNull(account);
            assertEquals("test", account.getName());
            assertTrue(account instanceof Taggable);
        }
    
        public void testTagMixinPojo() {
            CashAccount account = new CashAccount();
            assertNotNull(account);
            assertEquals("test", account.getName());
            assertTrue(account instanceof Taggable);
        }
    
    }
    testTagMixinBean() works well, but testTagMixinPojo() fails on assertTrue(account instanceof Taggable). It means that property "name" is set to necessary value, but aspect does not work for object created with new().

    Class com.gb.account.model.CashAccount hase @Confugurable annotation, and interface com.gb.tags.aspect.Taggable and class com.gb.tags.aspect.TaggableImpl are just stubs, com.gb.tags.aspect.EntityTagging is an @Aspect.
    Last edited by altosz; Sep 12th, 2007, 03:11 AM. Reason: add code example

  • #2
    Hi!

    Do you run your unittests from inside eclipse? I ask since I have had some problems with getting @Configured to work when running unittests inside eclipse, but I found some hints in the folowing thread witch made be sort out this issues:

    http://forum.springframework.org/sho...7&postcount=13

    Comment


    • #3
      Hi!

      Thanks for your reply

      Yes I do run tests from Eclipse, but to be sure that @Configurable works there's an assertion in each test: assertEquals("test", account.getName()) which checks that property (account.name) was set according to bean in application context.

      The thing that does not work is: aspect (EntityTagging) is not introduced in domain object created with new().

      By the way if I run tests from maven I get the same result.

      Comment


      • #4
        Same here

        Originally posted by altosz View Post
        Hi!

        Thanks for your reply

        Yes I do run tests from Eclipse, but to be sure that @Configurable works there's an assertion in each test: assertEquals("test", account.getName()) which checks that property (account.name) was set according to bean in application context.

        The thing that does not work is: aspect (EntityTagging) is not introduced in domain object created with new().

        By the way if I run tests from maven I get the same result.
        I've got the same problem.
        Using compiletime weaving - and the @Configurable stuff has been set (I assert it after instanciating with new as well ) - but the interceptors are never applied.

        The classes I'm advising don't implement interfaces - does yours? (Although this shouldn't be a problem as I'm setting proxy-classes to true as well).

        Probably a bug in spring.

        I'm sure the pointcut definitions are OK as they match other beans defined with similar pointcuts (e.g. syntactically the same - but other packages.)

        Have you registered a JIRA issue for this?

        Comment


        • #5
          Hi, maybe it's a bug... but another thing is @Configurable maybe not intended to inject aspect, just dependencies... What I'm trying to do is to implement the same functionality - to have an aspect which should inject necessary aspects when creating domain object with new().

          If you have any other ideas or suggestions I would gladly discuss them.

          And how can I post a JIRA issue? (I'm new to it).

          Thanks.

          Comment


          • #6
            Originally posted by altosz View Post
            Hi, maybe it's a bug... but another thing is @Configurable maybe not intended to inject aspect, just dependencies... What I'm trying to do is to implement the same functionality - to have an aspect which should inject necessary aspects when creating domain object with new().

            If you have any other ideas or suggestions I would gladly discuss them.

            And how can I post a JIRA issue? (I'm new to it).

            Thanks.
            OK - didn't read your post thouroughly enough.
            I got my stuff (e.g. joinpoints) to work now - I used @spectJ expressions instead of JDkRexep based ones.

            Spring JIRA lives here: http://opensource.atlassian.com/proj...Dashboard.jspa

            Comment


            • #7
              davidkarlsen, could you post some example code?
              Thanks

              Comment


              • #8
                need help

                hey altosz,
                Can you please tell me if you were able to resolve the issue. I am having the same issue. All injections are working fine in pojo, only the aspects arent getting applied. How to solve this.

                Comment


                • #9
                  Yes, I did.
                  I can post some code tomorrow when I am at work.

                  Comment


                  • #10
                    That will be great. Thanks a lot. I am thinking that I am doing something very stupid cos I followed all the steps in spring documentation and yet couldnt get it to work. Anyways I will be waiting for your post.

                    Comment

                    Working...
                    X