Spring AOP 切面

   面向切面编程(Aspect oriented Programming, AOP)是软件编程思想发展到一定阶段的产物,是对面向编程的有益补充。 AOP一般适用于具有横切逻辑的场合,如访问控制,事物管理,性能检测等。

AOP的目的是从系统中分离出切面,独立于业务逻辑实现,在程序执行时织入程序中运行。

Spring体系结构四个核心组件

  beans:Bean是包装我们应用程序自定义对象Object的,Object中存有数据
  core :Context在发现、建立、维护Bean之间关系所需要的一些工具,如资源的加载,资源的抽象等。 
  context Context就是一个Bean关系的集合

  expression 

AOP(Aspact Oriented Programming)面向切面编程
切面:横切关注点被模块化为特殊的类,这些类成为切面。
优点:每个关注点现在都集中于一处,而不是分散到多处代码中服务模块更简洁,服务模块只需关注核心代码。
AOP的原理:(1)采用代理机制组装起来运行,在不改变原程序的基础上对代码段进行增强处理,增强新的功能
           (2)将复杂的需求分解出不同方面,将分散在系统中的公共功能中集中解决。
连接点:  定义:连接点是一个应用执行过程中能够插入一个切面的点。
          连接点可以是调用方法时,甚至修改字段时,切面代码都可以利用这些点插入到应用的正规流程中。程序执行过程中能够应用通知的所有点。
通知:增强类中的方法就是通知。
切面(Aspect):一个模块化的横切逻辑(或横切关注点)可能会横切多个对象。
连接点:程序执行中的某个具体的执行点。
增强处理:切面在某个特定连接点上执行的代码逻辑。
切入点:对连接点的特征进行描述,可以使用正则表达式。增强处理和一个切入点表达式相关联,并在这个切入点匹配的某个链接点上运行。
目标对象:被一个或多个切面增强的对象。
AOP代代理:有Aop框架所创建的对象,实现执行增强处理方法等功能。
织入:将增强处理连接到应用程序中的类型或对象上的过程。


AOP实现日志记录,在添加用户模块之前和之后,加入自己的代码。
  
  前置增强接口 MethodBeforeAdvice 
  
  后置增强接口 AfterReturningAdvice 


先建一个实体类的包定义一个Student类,写上属性并将其进行分封装:
 private  int  id;
    private  String name;
    private  String age;
    private String email;


建一个dao的包,在该包中建立一个StudentDao的接口,并写上一个实现的方法:
public interface StudentDao {
    public  void  stu(Student  student);
}


建一个daoinpl包,在包中建一个LoggerAfter类并继承MethodBeforeAdvice接口
public class LoggBefore implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("-----------前置接口噢--------------");
    }
}


 建立LoggerAfter类并继承AfterReturningAdvice接口
public class LoggerAfter implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("---------------后置接口噢-----------------");
    }
}


建立StuDaoImpl类继承StudentDao接口
public class StuDaoImpl implements StudentDao {
    public void stu(Student student) {
        System.out.println("人生若是无误,铅笔何须橡皮!!!");
    }
}


创建service成,在该在层中创建StudentBiz接口
public interface StudentBiz {
    public  void  save(Student  stu);
}


创建service层中创建StuBiz类并继承StudentBiz接口
public class StuBiz implements StudentBiz {
    private StuDaoImpl  stuimpl;
    public void save(Student stu) {
        stuimpl.stu(stu);
    }


    public StuDaoImpl getStuimpl() {
        return stuimpl;
    }


    public void setStuimpl(StuDaoImpl stuimpl) {
        this.stuimpl = stuimpl;
    }
}


创建applicationContest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd


         http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
">


    <!--dao-->
  <bean id="studaoimpl" class="cn.happy.spring_aop.daoimpl.StuDaoImpl" ></bean>


    <!--service-->
    <bean id="servicestu" class="cn.happy.spring_aop.service.StuBiz">
        <property name="stuimpl" ref="studaoimpl"></property>
    </bean>
    <!--前置-->
    <bean id="loggbefore" class="cn.happy.spring_aop.daoimpl.LoggBefore"></bean>
    <!--后置-->
    <bean id="loggerafter" class="cn.happy.spring_aop.daoimpl.LoggerAfter"></bean>
   <!--AOP-->
<aop:config>
    <!--切点-->
   <aop:pointcut id="pution" expression="execution(* * ..service.*.*(..))"></aop:pointcut>
    <!--advisor-->
    <aop:advisor advice-ref="loggbefore" pointcut-ref="pution"></aop:advisor>
    <aop:advisor advice-ref="loggerafter" pointcut-ref="pution"></aop:advisor>
</aop:config>


</beans>


创建测试类
public class SpringAopTest {
@Test
    public  void  AopTest(){
        ApplicationContext  ctx=new ClassPathXmlApplicationContext("spplicationSpringAop.xml");
        StudentBiz   studentBiz= (StudentBiz) ctx.getBean("servicestu");
        Student  st=new Student();
        st.setName("12");
        studentBiz.save(st);
    }
}




spring容器创建的时候,会将所有在配置文件中配置的bean对象创建出来,默认bean都是单例的、
  什么是单例??
  一个类在内存中只有一个对象
  *bean成为多例?
bean的作用域
    Struts2:  Action类多例的。 Struts1 Action是单例的
       S2:  Servlet 单例的  xxx_jsp.java  底层也是通过单例多线程来处理客户端请求的
            JSP:单例的 xxx.jsp
    scope="prototype" 原型模式(N个对象):真正使用时才会创建,每获取一次,都会创建不同对象
    scope="singleton" 单例模式:容器初始化时需要使用name建,每次获取的都是同一个对象,默认值

单例:单例模式是一中常用的软件设计模式,在它的核心结构中只包含一个称为单例的特殊类,通过单例模式可以保证系统中一个类只有一个实例,即一个类只有一个对象实例。

程序员可以手动控制bean的创建时机

   Prototype:多例  形成时机是getBean()

   Stringleton:单例  容器构建完毕

  JSP和servlet都是单例的多线程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值