企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
在终端执行以下命令: ```shell git checkout -b 09 ``` 在 `net.zhaoxuyang.ioc.aop.AdvisedSupport` 中插入以下内容: ``` private MethodMatcher methodMatcher; public MethodMatcher getMethodMatcher() { return methodMatcher; } public void setMethodMatcher(MethodMatcher methodMatcher) { this.methodMatcher = methodMatcher; } ``` 修改 `net.zhaoxuyang.ioc.aop.TargetSource` 中的 `targetClass` 的类型: ```java private final Class<?>[] targetClass; public TargetSource( Object target, Class<?>... targetClass) { this.target = target; this.targetClass = targetClass; } public Class<?>[] getTargetClass() { return targetClass; } ``` 新增 `net.zhaoxuyang.ioc.bean.BeanPostProcessor` : ```java package net.zhaoxuyang.ioc.bean; public interface BeanPostProcessor { Object before(Object bean,String beanName) throws Exception; Object after(Object bean,String beanName) throws Exception; } ``` 新增 `net.zhaoxuyang.ioc.bean.factory.BeanFactoryAware`: ```java package net.zhaoxuyang.ioc.bean.factory; public interface BeanFactoryAware { void setBeanFactory(BeanFactory beanFactory) throws Exception; } ``` 在 `net.zhaoxuyang.ioc.bean.factory.AbstractBeanFactory` 中添加: ```java private List<BeanPostProcessor> beanPostProcessors = new LinkedList<>(); protected Object initBean(Object bean, String name) throws Exception{ Object obj = bean; for(BeanPostProcessor entry:beanPostProcessors){ obj = entry.before(bean, name); } for(BeanPostProcessor entry:beanPostProcessors){ obj = entry.after(bean, name); } return obj; } public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) throws Exception { this.beanPostProcessors.add(beanPostProcessor); } @SuppressWarnings("unchecked") public List getBeansForType(Class<?> type) throws Exception{ List beans = new LinkedList<>(); for(String key:beanDefinitionNames){ if(type.isAssignableFrom(beanDefinitionMap.get(key).getBeanClass())){ beans.add(getBean(key)); } } return beans; } ``` 创建 `net.zhaoxuyang.ioc.aop.AspectJAroundAdvice` : ```java package net.zhaoxuyang.ioc.aop; import java.lang.reflect.Method; import net.zhaoxuyang.ioc.aop.intercept.Advice; import net.zhaoxuyang.ioc.aop.intercept.MethodInterceptor; import net.zhaoxuyang.ioc.aop.intercept.MethodInvocation; import net.zhaoxuyang.ioc.bean.factory.BeanFactory; public class AspectJAroundAdvice implements Advice, MethodInterceptor{ private BeanFactory beanFactory; private Method aspectJAdviceMethod; private String aspectInstanceName; @Override public Object invoke(MethodInvocation invocation) throws Throwable { return aspectJAdviceMethod.invoke(beanFactory.getBean(aspectInstanceName), invocation); } public BeanFactory getBeanFactory() { return beanFactory; } public void setBeanFactory(BeanFactory beanFactory) { this.beanFactory = beanFactory; } public Method getAspectJAdviceMethod() { return aspectJAdviceMethod; } public void setAspectJAdviceMethod(Method aspectJAdviceMethod) { this.aspectJAdviceMethod = aspectJAdviceMethod; } public String getAspectInstanceName() { return aspectInstanceName; } public void setAspectInstanceName(String aspectInstanceName) { this.aspectInstanceName = aspectInstanceName; } } ``` 修改 `net.zhaoxuyang.ioc.aop.JdkDynamicAopProxy` : ```java package net.zhaoxuyang.ioc.aop; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import net.zhaoxuyang.ioc.aop.intercept.MethodInterceptor; public class JdkDynamicAopProxy implements AopProxy, InvocationHandler { private AdvisedSupport advised; public JdkDynamicAopProxy(AdvisedSupport advised) { this.advised = advised; } @Override public Object getProxy() { return Proxy.newProxyInstance( getClass().getClassLoader(), advised.getTargetSource().getTargetClass(), this ); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { MethodInterceptor interceptor = advised.getMethodInterceptor(); Object target = advised.getTargetSource().getTarget(); if(advised.getMethodMatcher() != null && advised.getMethodMatcher().matches(method, target.getClass())){ return interceptor.invoke(new ReflectiveMethodInvocation(target, method, args)); }else { return method.invoke(target, args); } } } ``` 新增 `net.zhaoxuyang.ioc.aop.AspectAwareAdvisorAutoProxyCreator`: ```java package net.zhaoxuyang.ioc.aop; import java.util.List; import net.zhaoxuyang.ioc.aop.intercept.MethodInterceptor; import net.zhaoxuyang.ioc.bean.BeanPostProcessor; import net.zhaoxuyang.ioc.bean.factory.AbstractBeanFactory; import net.zhaoxuyang.ioc.bean.factory.BeanFactory; import net.zhaoxuyang.ioc.bean.factory.BeanFactoryAware; public class AspectAwareAdvisorAutoProxyCreator implements BeanPostProcessor, BeanFactoryAware{ private AbstractBeanFactory beanFactory; @Override public Object before(Object bean, String beanName) throws Exception { return bean; } @Override public Object after(Object bean, String beanName) throws Exception { if(bean instanceof AspectJExpressionPointcutAdvisor){ return bean; } if(bean instanceof MethodInterceptor){ return bean; } List<AspectJExpressionPointcutAdvisor> advisors = beanFactory .getBeansForType(AspectJExpressionPointcutAdvisor.class); for(AspectJExpressionPointcutAdvisor advisor:advisors){ if(advisor.getPointcut().getClassFilter().matches(bean.getClass())){ AdvisedSupport advisedSupport = new AdvisedSupport(); advisedSupport.setMethodInterceptor((MethodInterceptor)advisor.getAdvice()); advisedSupport.setMethodMatcher(advisor.getPointcut().getMethodMatcher()); TargetSource targetSource = new TargetSource(bean,bean.getClass().getInterfaces()); advisedSupport.setTargetSource(targetSource); return new JdkDynamicAopProxy(advisedSupport).getProxy(); } } return bean; } @Override public void setBeanFactory(BeanFactory beanFactory) throws Exception { this.beanFactory = (AbstractBeanFactory) beanFactory; } } ``` 修改 `net.zhaoxuyang.ioc.context.AbstractApplicationContext`: ```java package net.zhaoxuyang.ioc.context; import java.util.List; import net.zhaoxuyang.ioc.bean.BeanDefinition; import net.zhaoxuyang.ioc.bean.BeanPostProcessor; import net.zhaoxuyang.ioc.bean.factory.AbstractBeanFactory; public abstract class AbstractApplicationContext implements ApplicationContext { protected AbstractBeanFactory beanFactory; public AbstractApplicationContext(AbstractBeanFactory beanFactory){ this.beanFactory = beanFactory; } public void refresh() throws Exception{ loadBeanDefinitions(beanFactory); registerBeanPostProcessor(beanFactory); onRefresh(); } protected abstract void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception; protected void registerBeanPostProcessor(AbstractBeanFactory beanFactory) throws Exception { List<?> beanPostProcessors = beanFactory.getBeansForType(BeanPostProcessor.class); for(Object obj:beanPostProcessors){ beanFactory.addBeanPostProcessor((BeanPostProcessor)obj); } } protected void onRefresh() throws Exception{ beanFactory.preInstantiateSingletons(); } @Override public void registerBeanDefinition(String name, BeanDefinition bd) throws Exception { throw new UnsupportedOperationException("Not supported yet."); } @Override public Object getBean(String name) throws Exception { return beanFactory.getBean(name); } } ``` 修改 `net.zhaoxuyang.ioc.context.ClassPathXmlApplicationContext`: ```java package net.zhaoxuyang.ioc.context; import java.util.Map; import net.zhaoxuyang.ioc.bean.BeanDefinition; import net.zhaoxuyang.ioc.bean.factory.AbstractBeanFactory; import net.zhaoxuyang.ioc.bean.factory.AutowireCapableBeanFactory; import net.zhaoxuyang.ioc.bean.io.ResourceLoader; import net.zhaoxuyang.ioc.bean.xml.XmlBeanDefinitionReader; public class ClassPathXmlApplicationContext extends AbstractApplicationContext{ private String configLocation; public ClassPathXmlApplicationContext(String configLocation) throws Exception{ this(configLocation, new AutowireCapableBeanFactory()); } public ClassPathXmlApplicationContext(String configLocation,AbstractBeanFactory beanFactory) throws Exception{ super(beanFactory); this.configLocation = configLocation; refresh(); } public void refresh() throws Exception{ XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new ResourceLoader()); reader.loadBeanDefinitions(configLocation); for(Map.Entry<String,BeanDefinition> entry:reader.getRegistry().entrySet()){ beanFactory.registerBeanDefinition(entry.getKey(), entry.getValue()); } } @Override protected void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new ResourceLoader()); reader.loadBeanDefinitions(configLocation); for (Map.Entry<String, BeanDefinition> entry : reader.getRegistry().entrySet()) { beanFactory.registerBeanDefinition(entry.getKey(), entry.getValue()); } } } ``` 在终端输入以下命令: ```shell $ git add . $ git commit -m 'auto-create-aop-proxy' ```