zoukankan      html  css  js  c++  java
  • AOP 入门

    1,源码

    Application.java

    package com.bf;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    import java.util.List; 
    import java.util.Date;
    import java.util.Iterator; 
     
    import org.hibernate.HibernateException; 
    import org.hibernate.Session; 
    import org.hibernate.Transaction;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    @SuppressWarnings("unused")
    public class Application {
    
        
          public static void main(String[] args) {
              try{
                  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
                  
                  SpitterDAO dao = (SpitterDAO)context.getBean("dao");
                  
                  dao.SaveEvent();
              }catch (Throwable ex) { 
                 throw new ExceptionInInitializerError(ex); 
              }
          }
        }

    Audience.java

    package com.bf;
    
    public class Audience {
        public void takeSeats()
        {
            System.out.println("sit down");
            
        }
    }

    ApplicationContext.xml

    <?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:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
        
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="" />
            <property name="username" value="" />
            <property name="password" value="" />
        </bean>
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg ref="dataSource" />
        </bean>
        <bean id="dao" class="com.bf.JDBCDAO" autowire="byName">
            <property name="jdbcTemplate" ref="jdbcTemplate" />
        </bean>
        <bean id="Audience" class="com.bf.Audience" autowire="byName">
        </bean>
         
           <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">
                        org.hibernate.dialect.Oracle9Dialect
                    </prop>
                </props>
            </property>
            <property name="mappingResources">
                <list>
                    <value>Event.hbm.xml</value>
                </list>
            </property>
        </bean>
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>
    
        <aop:config>
            <aop:aspect ref="Audience">
                <aop:before pointcut="execution(* com.bf.SpitterDAO.SaveEvent(..))" method="takeSeats" />
            </aop:aspect>
        </aop:config>
    </beans>
    View Code

    需要的Jar列表

    spring-aop-4.2.3.RELEASE.jar
    spring-beans-4.2.3.RELEASE.jar
    spring-context-4.2.3.RELEASE.jar"
    spring-context-support-4.2.3.RELEASE.jar
    spring-core-4.2.3.RELEASE.jar
    spring-expression-4.2.3.RELEASE.jar
    commons-logging.jar
    spring-jdbc-4.2.3.RELEASE.jar"
    spring-tx-4.2.3.RELEASE.jar
    commons-dbcp-1.2.jar
    commons-pool-1.2.jar
    ojdbc14.jar
    commons-collections-3.1.jar
    antlr-2.7.7.jar
    classmate-1.3.0.jar
    dom4j-1.6.1.jar
    geronimo-jta_1.1_spec-1.1.1.jar
    hibernate-commons-annotations-5.0.1.Final.jar
    hibernate-core-5.1.0.Final.jar
    hibernate-jpa-2.1-api-1.0.0.Final.jar
    jandex-2.0.0.Final.jar
    javassist-3.20.0-GA.jar
    jboss-logging-3.3.0.Final.jar
    spring-orm-4.2.3.RELEASE.jar
    aopalliance.jar
    spring-aspects-4.2.3.RELEASE.jar
    aspectj-weaver.jar
    

    演示在SaveEvent前调Audience.takeSeats(). 

    注意SpitterDAO dao = (SpitterDAO)context.getBean("dao"); 这里用的是接口而不是实现类。

    也可以如下配置切面

        <aop:config>
            <aop:aspect ref="Audience">
                <aop:pointcut id="seat" expression="execution(* com.bf.SpitterDAO.SaveEvent(..))" ></aop:pointcut>
                <aop:before pointcut-ref="seat" method="takeSeats" />
            </aop:aspect>
        </aop:config>

    2,基本概念

    切面Aspect  

    通知和切点的结合,两者共同定义了关于切面的全部内容。

    通知Advice

    定义了切面的“什么”和“何时”。切面的“工作”被称为通知。

    连接点JoinPoint

    连接点是Application运行过程中能够插入切面的一个点。这个点可以是方法调用时、抛出异常时、甚至修改一个字段时。

    切点Pointcut

    定义了切面的“何处”,切点的定义会匹配通知要织入的一个或多个连接点。Pointcuts pick out join points. 

    参考资料:http://www.eclipse.org/aspectj/doc/released/progguide/language-joinPoints.html



    签名:删除冗余的代码最开心,找不到删除的代码最痛苦!
  • 相关阅读:
    最大值和最小值
    GetLevelDesc函数
    21. D3DSprite
    SetFileAttributes和GetFileAttributes
    24. 幕外渲染
    D3D修改view矩阵与修改world矩阵的区别
    23. 保存纹理(保存屏幕截图)
    C++之solmyr小品文
    C++二进制文件写操作
    22. 凸凹贴图
  • 原文地址:https://www.cnblogs.com/season2009/p/5261781.html
Copyright © 2011-2022 走看看