解决方案 »
- 目前对 hadoop 的前景了解甚少,不知道他的前途如何,有经验的给点建议啊
- JAVA
- 谁能告诉我oscache 中的cache标签的各个属性是什么意思啊??急。。。
- hibernate关联关系出错,大家帮帮忙
- struts数据显视问题
- 关于struts中的标签<bean:message>的问题??????、、
- 如何让double或者float型数据保留两位小数?
- 一个简单struts连接标记的问题
- 请问jdom中有没有查询相同元素个数的方法呀????
- jdbc问题
- EL,javax.enterprise.context.conversation.id如何才能取到值?
- 通过java控制QQ的登录按钮,运行程序就相当于点击了登录一样
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>