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
TagMixinTest.javaCode:<?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>
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().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); } }
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.


Reply With Quote
