- //一个通告或建议的接口
- public interface Advice {
- void afterMethod(Method method);
- void beforeMethod(Method method);
- }
- public class MyAdvice implements Advice{
- private long start;
- @Override
- public void afterMethod(Method method) {
- System.out.println("运行时间计算结束!!");
- System.out.println("共耗时:"+(System.currentTimeMillis()-start));
- System.out.println("------------------------");
- }
- @Override
- public void beforeMethod(Method method) {
- System.out.println("开始计算运行时间:");
- start=System.currentTimeMillis();
- System.out.println("所执行的方法:"+method.getName());
- }
- }
- Properties文件的配置
- #xxx=java.util.ArrayList
- xxx=aopframework.ProxyFactoryBean
- xxx.advice=aopframework.MyAdvice
- xxx.target=java.util.ArrayList
- /**
- * 负责创建目标类或代理类的实例对象,并通过配置文件实现切换
- *
- */
- public class BeanFactory {
- private Properties prop;
- public BeanFactory(InputStream is){
- prop=new Properties();
- try {prop.load(is);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public Object getBean(String propkey){
- String className=prop.getProperty(propkey);
- Object bean=null;
- try {
- bean=Class.forName(className).newInstance();
- if(bean instanceof ProxyFactoryBean){
- String adviceName=prop.getProperty(propkey+".advice");
- String targetName=prop.getProperty(propkey+".target");
- try {
- Advice advice = (Advice) Class.forName(adviceName).newInstance();
- Object target = Class.forName(targetName).newInstance();
- ProxyFactoryBean beanproxy=(ProxyFactoryBean)bean;
- beanproxy.setAdvice(advice);
- beanproxy.setTarget(target);
- bean=beanproxy.getProxy();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return bean;
- }
- }
- /**
- * 充当封装生成动态代理的工厂
- */
- public class ProxyFactoryBean {
- private Advice advice;
- private Object target;
- public Object getProxy() {
- Object coll=(Object) Proxy.newProxyInstance(
- target.getClass().getClassLoader(),
- target.getClass().getInterfaces(),
- new InvocationHandler(){
- @Override
- public Object invoke(Object proxy, Method method,
- Object[] args) throws Throwable {
- advice.beforeMethod(method);
- Object obj=method.invoke(target, args);
- advice.afterMethod(method);
- return obj;
- }
- });
- return coll;
- }
- public Advice getAdvice() {
- return advice;
- }
- public void setAdvice(Advice advice) {
- this.advice = advice;
- }
- public Object getTarget() {
- return target;
- }
- public void setTarget(Object target) {
- this.target = target;
- }
- }
- public class Test {
- public static void main(String[] args) {
- InputStream is=Test.class.getClassLoader().getResourceAsStream("config.properties");
- BeanFactory factory=new BeanFactory(is);
- Collection coll=(Collection) factory.getBean("xxx");
- coll.add("aaa");
- coll.add("222");
- System.out.println(coll.size());
- }
- /**
- * 运行结果
- 开始计算运行时间:
- 所执行的方法:add
- 运行时间计算结束!!
- 共耗时:0
- ------------------------
- 开始计算运行时间:
- 所执行的方法:add
- 运行时间计算结束!!
- 共耗时:0
- ------------------------
- 开始计算运行时间:
- 所执行的方法:size
- 运行时间计算结束!!
- 共耗时:0
- ------------------------
- 2
- */
- }