Announcement Announcement Module
Collapse
No announcement yet.
Pointcut by @annotation(me.some.Annotation) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pointcut by @annotation(me.some.Annotation)

    I in the project have a annotation which is responsible for a caching of a call of methods: me.some.Cached.
    Code:
    package me.some;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Cached {
    	long value();
    }
    As there is a class with a method responsible for a caching:
    Code:
    package me.some;
    /*imports excluded*/
    public class CachedManager {
    
    	public Object cached(ProceedingJoinPoint jp) throws Throwable {
    		MethodSignature signature = (MethodSignature) jp.getSignature();
    		Method m = jp.getTarget().getClass().getMethod(signature.getMethod().getName(),
    											signature.getMethod().getParameterTypes());
    		log.info("Caching: " + m);
    		return jp.proceed();
    	}
    }
    Spring config:
    Code:
    	<bean id="cachedProvider" class="me.some.ProviderImpl" scope="singleton"/>
    
    	<bean id="CachedManager" class="me.some.CachedManager" scope="singleton" />
    
    	<aop:config>
    		<aop:aspect ref="CachedManager">
    			<aop:around method="cached" pointcut="@annotation(me.some.Cached)" />
    		</aop:aspect>
    	</aop:config>
    And where it is used:
    Code:
    package me.some;
    /*imports excluded*/
    public class ProviderImpl impliments Provider {
    	@Override
    	@Cached(300000)
    	public Collection<Action> getActions(long kId) throws SQLException {
    		Collection<Action> actions = new ArrayList<Action>();
    		for (long id : getActionIds(kId))
    			actions.add(getActionById(id));
    		return actions;
    	}
    
    	@Override
    	@Cached(300000)
    	public Collection<Action> get$Actions(long kId) {
    		Collection<Action> actions = new ArrayList<Action>();
    		for (long id : CachedContextLoader.getProvider().getActionIds(kId))
    			actions.add(CachedContextLoader.getProvider().getActionById(id));
    		return actions;
    	}
    
    	@Override
    	@Cached(300000)
    	public Collection<Long> getActionIds(long kId) throws SQLException {
    		return super.getAction(kId);
    	}
    
    	@Override
    	@Cached(300000)
    	public Action getActionById(long actionId) throws SQLException {
    		return super.getActionById(actionId);
    	}
    }
    Question. By a call:
    Code:
    //~~~
    	log.info("First: ");
    	Collection<Action> actions = CachedContextLoader.getProvider().getActions(100500);
    	log.info("Second: ");
    	Collection<Action> actions = CachedContextLoader.getProvider().get$Actions(100500);
    //~~~
    On "First: " the method responsible for a caching works only on the first method:
    Code:
    Caching: public java.util.Collection me.some.ProviderImpl.getActions(long)
    On "Second: " the method responsible for a caching works on all methods:
    Code:
    Caching: public java.util.Collection me.some.ProviderImpl.get$Actions(long)
    Caching: public java.util.Collection me.some.ProviderImpl.getActionIds(long)
    Caching: public me.some.Action me.some.ProviderImpl.getActionById(long)
    Whether it is possible to make that in the first option there was a call of pointcut of a method for all calls?
    Source code: Attachment
    Attached Files
    Last edited by xema; Jun 19th, 2012, 06:07 AM.

  • #2
    И куда только катится мир...
    Что, ни кто не в курсе?

    Comment


    • #3
      Most normal solution is found.
      Code:
      public class ProviderImpl impliments Provider {
      
      	private Provider proxy;
      
      	public void setProxy(Provider proxy) {
      		this.proxy = proxy;
      	}
      
      	@Override
      	@Cached(300000)
      	public Collection<Action> getActions(long kId) throws SQLException {
      		Collection<Action> actions = new ArrayList<Action>();
      		for (long id : proxy.getActionIds(kId))
      			actions.add(proxy.getActionById(id));
      		return actions;
      	}
      ~~
      Code:
      <bean id="cachedProvider" class="me.some.ProviderImpl" scope="singleton">
      	<property name="proxy" ref="cachedProvider" />
      </provider>
      Something like this.

      Comment

      Working...
      X