解决方案 »

  1.   

    这样,你看行不行,因为要用Aspect 切入,user 类要改一下,改成有接口的,
    UserAdvisor 相当于你的 @CustomizedAnnotaion ,但是要标注在getter方法
    package com.zuxiang.aspect.service;/**
     * 使用aspect 切面拦截
     * @author zuxiang
     *
     */
    public interface UserService { void setId(String id);
    void setName(String name);
    String getId();
    String getName();

    }package com.zuxiang.aspect.service;import org.springframework.stereotype.Component;import com.zuxiang.aspect.UserAdvisor;
    /**
     * 使用aspect 切面拦截
     * @author zuxiang
     *
     */
    @Component
    public class UserServiceImpl implements UserService{ private String id;
    private String name;

    @Override
    @UserAdvisor("id-") //给返回值前面加个id-前缀
    public String getId() {
    return id;
    }
    @Override
    public void setId(String id) {
    this.id = id;
    }
    @Override
    @UserAdvisor("name-")//给返回值前面加个name-前缀
    public String getName() {
    return name;
    }
    @Override
    public void setName(String name) {
    this.name = name;
    }



    }/**
     * 
     */
    package com.zuxiang.aspect;import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;/**
     * @author zuxiang
     *
     */
    @Retention(value=RetentionPolicy.RUNTIME) 
    @Target(value=ElementType.METHOD) 
    public @interface UserAdvisor { 
    String value() default "";
    }package com.zuxiang.aspect;import java.beans.PropertyDescriptor;
    import java.lang.reflect.Method;import org.apache.commons.beanutils.PropertyUtils;
    import org.apache.commons.lang.ObjectUtils;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;@Component
    @Aspect
    public class UserAspect {

    @Before(value = "@annotation(a)" ,argNames = "a")
    public void around(JoinPoint pjp, UserAdvisor a) {
    try {
    Class<?> clazz = pjp.getTarget().getClass();
    String methodName = pjp.getSignature().getName(); 
    Method readMethod = clazz.getMethod(methodName);
    Object target = pjp.getTarget();
    String value = ObjectUtils.toString(readMethod.invoke(target));
    value = a.value() + value; //加上指定的前缀
    Method writeMethod = getWriteMethod(methodName,clazz);
    if (writeMethod != null){
    writeMethod.invoke(target, value);   //修改值                                                 
    }
    } catch (Throwable e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    private Method getWriteMethod(String readMethodName,Class<?> clazz){
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(clazz);
    for (PropertyDescriptor pd : pds){
    if (pd.getReadMethod().getName().equals(readMethodName)){
    return pd.getWriteMethod();
    }
    }
    return null;
    }
    }package com.zuxiang;import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;import com.zuxiang.aspect.service.UserService;public class Test {    public static void main(String[] args) {

         ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        
         UserService us = (UserService) ac.getBean("userServiceImpl");
        
         us.setId("1r");
         us.setName("alis");
        
         String id = us.getId();
         String name = us.getName();
        
         System.out.println(id); 
         System.out.println(name); 
        }
    }spring配置文件<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    default-autowire="byName"> <context:annotation-config />  <!-- 开启使用spring注解 -->

    <context:component-scan base-package="com.zuxiang.*" /> <!-- 指定扫描哪些包下的注解 -->
        <aop:aspectj-autoproxy /> <!-- 开启Aspectj模式的aop代理 -->
        

    </beans>