B_第04章:spring之aop


AOP基础:

    AOP的重要概念:
        1,横切关注点
        2,切面
        3,连接点
        4,切入点
        5,通知, 前置,后置,异常,最终,环绕。
        6,目标对象
        7,织入
        8,引入


    AOP的实现:1,经典的基于代理的AOP。2,Aspectj基于XML的配置


1,经典的基于代理的AOP
    public interface IAOPServices {
        public String withAopMethod()throws Exception;
        public String withNoAopMethod() throws Exception;
    }

    public class AOPServicesImpl implements IAOPServices{
        private String description;
        public String getDescription() {return description;}
        public void setDescription(String description) {this.description = description;}

        public String withAopMethod() throws Exception {
            System.out.println("AOP函数运行了");
            if (description.trim().length() == 0) {
                throw new Exception("内容不能为空");
            }
            return null;
        }

        public String withNoAopMethod() throws Exception {
            System.out.println("无AOP函数运行了");
            return null;
        }
    }

    public class AopInterceptor implements AfterReturningAdvice, MethodBeforeAdvice, ThrowsAdvice {
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("方法"+method.getName()+",运行结果的返回值为:"+o);
        }
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("执行MethodBeforeAdvice,即将执行方法:"+method.getName());
            if (o instanceof AOPServicesImpl) {
            }
        }
        public void afterThrowing(Exception exception) {
            System.out.println("抛出异常"+exception.getMessage());
        }
        public void afterThrowing(Method method,Object[] args,Object target,Exception ex) {
            System.out.println("方法:"+method.getName()+"抛出异常"+ex.getMessage());
        }
    }

    配置文件:
            <bean id="aopInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
                <property name="advice">
                    <bean class="org.example.demoa.AopInterceptor"/>
                </property>
                <property name="mappedName" value="withAopMethod"/>
            </bean>

            <bean id="aopService" class="org.springframework.aop.framework.ProxyFactoryBean">
                <property name="interceptorNames">
                    <list>
                        <value>aopInterceptor</value>
                    </list>
                </property>
                <property name="target">
                    <bean class="org.example.demoa.AOPServicesImpl">
                        <property name="description" value="basicAop"/>
                    </bean>
                </property>

            </bean>

    运行:
        public static void main(String[] args) throws Exception {
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
            BeanFactory beanFactory=applicationContext;
        
            IAOPServices services=(IAOPServices) beanFactory.getBean("aopService");
            services.withAopMethod();
            System.out.println("=========");
            services.withNoAopMethod();
        }


B_第04章:spring之aop

上一篇:在docker中安装mysql并使用


下一篇:K8s 开始