Results 1 to 3 of 3

Thread: how to user @DeclareParents to add new interface to class and call the class's method

  1. #1
    Join Date
    Sep 2008
    Posts
    3

    Default how to user @DeclareParents to add new interface to class and call the class's method

    dear every one
    i get a class User like this
    Code:
    package com.yyhy.java.Privilege;// TODO define package
    import javax.persistence.*;
    import java.util.*;
    
    @Entity
    @Table(
    	name="USER")
    public class User {
    
    	//Attributes
    
    	private Long userID;
    	private String userName;
    	private String passWord;
    	private String remark;
    
    	//Getters and Setters
    
    	@Id
    	@Column(name="USERID",nullable=false)
    	public Long getUserID() { return this.userID; }
    	public void setUserID(Long UserID) { 
    		this.userID=UserID;
    	}
    
    	@Column(name="USERNAME",nullable=false)
    	public String getUserName() { return this.userName; }
    	public void setUserName(String UserName) { 
    		this.userName=UserName;
    	}
    
    	@Column(name="PASSWORD",nullable=false)
    	public String getPassWord() { return this.passWord; }
    	public void setPassWord(String PassWord) { 
    		this.passWord=PassWord;
    	}
    
    	@Column(name="REMARK")
    	public String getRemark() { return this.remark; }
    	public void setRemark(String ReMark) { 
    		this.remark=ReMark;
    	}
    	@Override
    	public String toString() {
    		// TODO Auto-generated method stub
    		return this.userName+this.remark;
    	}
    }
    i want to add a new method to user through Aspect,so i do like this
    Code:
    package com.yyhy.java.search.compass;
    
    public interface Compassable {
      public String getContent();
      
    }
    Code:
    package com.yyhy.java.search.compass;
    
    public class DefaultCompassableImpl implements Compassable {
    
    	@Override
    	public String getContent() {
    		return this.toString();//here is error,i want to use User.toString
    	}
    
    }
    Code:
    package com.yyhy.java.search.compass;
    import org.aspectj.lang.annotation.Aspect;
    
    import org.aspectj.lang.annotation.DeclareParents;
    @Aspect
    public class CompassableAspect {//com.yyhy.hx.*+
    @DeclareParents(value="com.yyhy.java.Privilege.User",
    			  defaultImpl=DefaultCompassableImpl.class)
    public static Compassable compassable;
    }
    this is my config
    Code:
    	<aop:aspectj-autoproxy />
        <bean id="CompassAspect" class="com.yyhy.java.search.compass.CompassableAspect" >
        </bean>
        <bean id="User" class="com.yyhy.java.Privilege.User"/>
    My test code
    Code:
    
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.FileSystemResource;
    import com.yyhy.java.Privilege.User;
    import com.yyhy.java.search.compass.Compassable;
    
    
    public class testAspectj {
    public static void main(String[] arg)
    {
    	FileSystemResource resource = new FileSystemResource("E:\\javawork\\hxCode\\src\\applicationContext.xml");
    
        XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
        User user = (User)beanFactory.getBean("User");
        user.setUserName("aaa");
        System.out.println(user.toString());
        System.out.println(user.getContent());
        Compassable a=(Compassable)user;
        System.out.println(a.getContent());
    	}
    }
    the result are:
    aaanull
    com.yyhy.java.search.compass.DefaultCompassableImp l@1128f5a
    com.yyhy.java.search.compass.DefaultCompassableImp l@1128f5a

    i want the user.getContent() returns the same as user.toString()
    what can i do?
    thanks alot

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Use an ApplicationContext instead of a BeanFactory.

    However I'm still wondering why it worked (the cast to Compassable)...

    Also there appears to be a mismatch between the full AspectJ language and the annotation based solution. With the full blown language (public aspect) it is quite easy to gain access to the adviced object, however with annotations it doesn't seem to be possible. (That is what I get from the various discussion on the AspectJ list).
    Last edited by Marten Deinum; Sep 5th, 2008 at 02:04 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Sep 2008
    Posts
    3

    Default change to aspectj,it is ok

    package com.yyhy.java.search.compass;

    import org.aspectj.lang.JoinPoint;

    public aspect CompassAOP {

    declare parents: com.yyhy.java.GenericCoder.* extends DefaultCompassableImpl;
    declare parents: com.yyhy.java.Privilege.* extends DefaultCompassableImpl;
    declare parents: com.yyhy.hx.zcdfk.* extends DefaultCompassableImpl;
    declare parents: com.yyhy.hx.jgpmk.* extends DefaultCompassableImpl;
    declare parents: com.yyhy.log.* extends DefaultCompassableImpl;

    public pointcut compass():execution(* *.getContent(..));
    Object around():compass(){
    return thisJoinPoint.getThis().toString();
    }
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •