zoukankan      html  css  js  c++  java
  • spring-01

    spring-01

    1、spring的简介
      Spring 是一个开源框架,为简化企业级应用开发而生。Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能。Spring 是一个 IOC 和 AOP 容器框架。
    IOC容器:控制反转, 反转控制, 反向控制
      将对象的生命周期控制权利,交给Spring容器管理。原有开发方式,对象生命周期由程序员通过代码控制.。如: new关键字. setter方法调用, xxx = null.
    1)控制反转中控制指的是:控制类的对象
    2)控制反转中反转指的是转交给 Spring 负责
    3)IoC 最大的作用:解耦(程序员不需要管理对象.解除了对象管理和程序员之间的耦合)
    2、Spring的DI:依赖注入
      当一个类(A)中需要依赖另一个类(B)对象时,把 B 赋值给 A 的过程就叫做依赖注入
    3、spring的注入的种类和方式
    1)构造方法注入(通过构造方法的参数,为对象的属性赋值)
    2)setter注入(Spring容器针对bean对象的property实现属性注入.面向getter方法和setter方法实现的属性数据赋值. 每次为属性赋值,调用的是setXxxx方法)
    3)静态工厂的方法注入
    4)实例工厂的方法注入

      1 //字符串注入
      2 private String msg;
      3 //int类型注入
      4 private int flag;
      5 //String类型数组注入    
      6 private String[] array;
      7 //int类型数组注入
      8 private int[] array2;
      9 //list注入
     10 private List<String> list;
     11 //其他对象类型
     12 private List<UserDao> uds;
     13 //set
     14 private Set<String> sets;
     15 //map
     16 private Map<String, String> maps;
     17 //Properties
     18 private Properties prop;
     19     
     20 public Properties getProp() {
     21     return prop;
     22 }
     23 
     24 public void setProp(Properties prop) {
     25     Set<Object> keys = prop.keySet();
     26     for (Object obj : keys) {
     27         String key = (String)obj;
     28         System.out.println(key + " " +prop.getProperty(key));
     29     }
     30     this.prop = prop;
     31 }
     32 
     33 public Map<String, String> getMaps() {
     34     return maps;
     35 }
     36 
     37 public void setMaps(Map<String, String> maps) {
     38     Set<String> keys = maps.keySet();
     39     for (String key : keys) {
     40         System.out.println(key+" "+ maps.get(key));
     41     }
     42     this.maps = maps;
     43 }
     44 
     45 public Set<String> getSets() {
     46     return sets;
     47 }
     48 
     49 public void setSets(Set<String> sets) {
     50     for (String set : sets) {
     51         System.out.println(set);
     52     }
     53     this.sets = sets;
     54 }
     55 
     56 public List<UserDao> getUds() {
     57     return uds;
     58 }
     59 
     60 public void setUds(List<UserDao> uds) {
     61     for (UserDao ud : uds) {
     62         System.out.println(ud);
     63     }
     64     this.uds = uds;
     65 }
     66 
     67 public List<String> getList() {
     68     return list;
     69 }
     70 
     71 public void setList(List<String> list) {
     72     for (String lists : list) {
     73         System.out.println(lists);
     74     }
     75     this.list = list;
     76 }
     77 
     78 public int[] getArray2() {
     79     return array2;
     80 }
     81 
     82 public void setArray2(int[] array2) {
     83     for (int i : array2) {
     84         System.out.println(i);
     85     }
     86     this.array2 = array2;
     87 }
     88 
     89 public String[] getArray() {
     90     return array;
     91 }
     92 
     93 public void setArray(String[] array) {
     94     for (String arrays : array) {
     95         System.out.println(arrays);
     96     }
     97     this.array = array;
     98 }
     99 
    100 public int getFlag() {
    101     return flag;
    102 }
    103 
    104 public void setFlag(int flag) {
    105     System.out.println(flag);
    106     this.flag = flag;
    107 }
    108 
    109 public String getMsg() {
    110     return msg;
    111 }
    112 
    113 public void setMsg(String msg) {
    114     System.out.println(msg);
    115     this.msg = msg;
    116 }
    View Code
     1 <!-- 属性注入用property标签  value值可以是String + 八种基本数据类型-->
     2 <property name="msg" value="字符串注入"></property>
     3 <property name="flag" value="6666"></property>
     4 <property name="array">
     5     <array>
     6         <value>dilraba</value>
     7         <value>tom</value>
     8         <value>java</value>
     9     </array>
    10 </property>
    11 <property name="array2">
    12     <array>
    13         <value>111</value>
    14         <value>222</value>
    15         <value>333</value>
    16     </array>
    17 </property>
    18 <property name="list">
    19     <list>
    20         <value>fasdfas</value>
    21         <value>fdfafgf</value>
    22     </list>
    23 </property>
    24 <property name="uds">
    25     <list>
    26         <bean class="com.boom.dao.impl.UserDaoImpl"></bean>
    27     </list>
    28 </property>
    29 <property name="sets">
    30     <set>
    31         <value>sets1</value>
    32         <value>sets2</value>
    33     </set>
    34 </property>
    35 <property name="maps">
    36     <map>
    37         <entry key="key1">
    38             <value>value1</value>
    39         </entry>
    40                 
    41         <entry key="key2">
    42             <value>value2</value>
    43         </entry>
    44     </map>
    45 </property>
    46 <property name="prop">
    47     <props>
    48         <prop key="prop1">prop11</prop>
    49         <prop key="prop2">prop22</prop>
    50     </props>
    51 </property></bean>
    View Code

    属性注入小结(必须要有set方法)
      属性注入通过property标签,注入String+八种基本数据类型用value标签,注入的是IOC容器自己的对象用ref,如果注入实体类对象,用bean标签缓存一下。
     Spring IOC注入方式用得最多的是(1)(2)种,通过Spring创建的对象默认是单例,如果需要创建多实例对象可以在<bean>标签后面添加一个属性:=

    <bean name="..." class="..." scope="prototype">

     4、面向切面编程(AOP)
      在面向对象编程(oop)思想中,我们将事物纵向抽成一个个的对象。而在面向切面编程中,我们将一个个的对象某些类似的方面横向抽成一个切面,对这个切面进行一些如权限控制、事物管理,记录日志等公用操作处理的过程就是面向切面编程的思想。AOP 底层是动态代理,如果是接口采用 JDK 动态代理,如果是类采用CGLIB 方式实现动态代理。
    一些俗语的理解:
    切面(aspect):动态代理对象
    连接点(joinpoint):AOP可以是方法,属性,构造方法,都可以拦截。spring中仅支持对方法的拦截 
    切点(pointcut):动态代理拦截的目标,如动态代理的invoke
    通知(advice):前置,后置,环绕
    Spring 切面可以应用五种类型的通知
    1)方法的前置通知(before)
    实现接口:MethodBeforeAdvice
    功能:在目标方法(被代理的对象的方法)调用前,做什么
    方法:void before(Method method, Object[] args, Object proxy)
    主要应用于:记录日志 | 开启事务 | 其他功能处理
    2)方法的后置通知(after  / after-returning)
    after:在方法执行之后调用的通知,无论方法执行是否成功。
    after-returning:仅当方法成功完成后执行的通知
    3)方法异常通知(after-throwing)
    实现接口:ThrowsAdvice
    功能:在方法抛出异常退出时执行的通知
    方法:void afterThrowing(RuntimeException e)
    4)方法环绕通知(around)
    实现接口:MethodInterceptor
    方法:Object invoke(MethodInvocation arg0) throws Throwable
    功能:在方法执行之前和之后调用的通知

    目标(target):被代理的对象
    织入(weave):目标和拦截对象捏合到一起的过程
    springAOP:面向切面编程,是将纵向执行的流程。进行横向的切面,增加额外的功能。
    切入的设计目标:使程序进行插拔式的开发,切的controller和service层之间。
    代理模式:为真正处理业务的对象,提供附属功能的类型,称为代理。在不改变真正处理业务对象的代码前提下,为方法逻辑。增加新的功能的方式。
    静态代理:代理对象专门为某一个对象或类型,提供针对性的服务支撑。功能全面强大,针对性强。缺陷是通用性降低。
    动态代理:代理对象为某一类事务提供辅助功能支撑。通用性高,针对性不足。
    静态代理代码实现
    ①定义接口:租赁接口,谁实现了这个接口,代表具有租赁能力,对外出租给第三方

     1 package com.boom.staticproxy;
     2 /**
     3  * 租赁接口,谁实现了这个接口,代表具有租赁能力,对外出租给第三方
     4  * @project_name proxy 
     5  * @class_name Rent
     6  * @author Dilraba
     7  */
     8 public interface Rent {
     9     //定义一个房源
    10     public void rent();
    11 
    12 }
    View Code

    ②业主,房屋的持有者,实现了租赁接口

     1 package com.boom.staticproxy;
     2 /**
     3  * 业主, 房屋的持有者
     4  * @project_name proxy 
     5  * @class_name Host
     6  * @author Dilraba
     7  */
     8 public class Host implements Rent{
     9 
    10     //出租的具体详情
    11     @Override
    12     public void rent() {
    13         System.out.println("签合同,交钱...");
    14     }
    15     
    16 
    17 }
    View Code

    ③代理对象,俗称二手房东等

     1 package com.boom.staticproxy;
     2 /**
     3  * 代理对象
     4  * @project_name proxy 
     5  * @class_name ProxyObject
     6  * @author Dilraba
     7  */
     8 public class ProxyObject implements Rent {
     9 
    10     private Host host;
    11     
    12     public ProxyObject() {
    13     }
    14     public ProxyObject(Host host) {
    15         this.host = host;
    16     }
    17     
    18     @Override
    19     public void rent() {
    20         System.out.println("带顾客看房子....");
    21         host.rent();
    22         System.out.println("后期的一些乱七八糟事情。。。。");
    23     }
    24     
    25 }
    View Code

    ④模拟客户租房子

     1 package com.boom.staticproxy;
     2 /**
     3  * 模拟客户租房子
     4  * @project_name proxy 
     5  * @class_name StaticProxy
     6  * @author Dilraba
     7  */
     8 public class TestProxy {
     9     public static void main(String[] args) {
    10         //创建客户
    11         Host t = new Host();
    12         //创建代理对象(二手房东)
    13         ProxyObject po = new ProxyObject(t);
    14         po.rent();
    15     }
    16 }
    View Code

    静态代理代码实现
    ①定义接口

     1 package com.boom.dynamicproxy;
     2 /**
     3  * 租赁接口,谁实现了这个接口,代表具有租赁能力,对外出租给第三方
     4  * @project_name proxy 
     5  * @class_name Rent
     6  * @author Dilraba
     7  */
     8 public interface Rent {
     9     //定义一个房源
    10     public void rent();
    11 
    12 }
    View Code

    ②实现接口

     1 package com.boom.dynamicproxy;
     2 /**
     3  * 业主, 房屋的持有者
     4  * @project_name proxy 
     5  * @class_name Host
     6  * @author Dilraba
     7  */
     8 public class Host implements Rent{
     9 
    10     //出租的具体详情
    11     @Override
    12     public void rent() {
    13         System.out.println("签合同,交钱...");
    14     }
    15     
    16 
    17 }
    View Code

    ③动态代理:实现了InvocationHandler调用其invoke方法

     1 package com.boom.dynamicproxy;
     2 
     3 import java.lang.reflect.InvocationHandler;
     4 import java.lang.reflect.Method;
     5 
     6 /**
     7  * 代理对象
     8  * @project_name proxy 
     9  * @class_name DynamicProxy
    10  * @author Dilraba
    11  */
    12 public class DynamicProxy implements InvocationHandler {
    13 
    14     private Object obj;
    15     public DynamicProxy() {
    16         
    17     }
    18     
    19     public DynamicProxy(Object obj) {
    20         super();
    21         this.obj = obj;
    22     }
    23 
    24     /**
    25      * @param proxy - 代理对象
    26      * @param method - 真实的方法, 就是要调用的最终方法. 业主出租访问的方法.
    27      * @param args - 真实方法要执行的时候,需要的参数.
    28      */
    29     @Override
    30     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    31         System.out.println("Advice...Before");
    32         System.out.println(proxy.getClass().getName());
    33         Object obj =  method.invoke(this.obj, args);
    34         System.out.println("Advice...After");
    35         return obj;
    36     }
    37     
    38 }
    View Code

    ④测试

     1 package com.boom.dynamicproxy;
     2 
     3 import java.lang.reflect.Proxy;
     4 
     5 /**
     6  * 
     7  * @project_name proxy 
     8  * @class_name TestProxy
     9  * @author Dilraba
    10  */
    11 public class TestProxy {
    12     public static void main(String[] args) {
    13         Host host = new Host();
    14         DynamicProxy dp = new DynamicProxy(host);
    15         Rent rent = (Rent)Proxy.newProxyInstance(host.getClass().getClassLoader(), host.getClass().getInterfaces(), dp);
    16         System.out.println(rent);//就是代理对象:com.boom.dynamicproxy.Host@6bc7c054
    17         rent.rent();
    18     }
    19 }
    View Code

    5、Spring中AOP的开发四种方式(Spring中对于 AOP配置底层基于动态代理)
    xml方式配置:在spring任意版本中都可以使用,缺:只能配置一个业务层,若业务层繁多做切面配置时候,配置信息繁琐,不便于管理。
    ①springAOP-xml.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!-- XSD约束的XML配置文件 -->
     3 <beans xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans
     6         http://www.springframework.org/schema/beans/spring-beans.xsd">
     7     <!-- 目标 -->
     8 <bean id="userService" class="com.boom.xml.service.impl.UserServiceImpl"></bean>
     9     <!-- 配置切面 -->
    10 <bean id="myAOP" class="com.boom.xml.aop.MyAOP"></bean>
    11 
    12 <bean id="userServieAOP" class="org.springframework.aop.framework.ProxyFactoryBean">
    13     <property name="target">
    14         <ref bean="userService"/>
    15     </property>
    16     <property name="proxyInterfaces">
    17         <value>com.boom.xml.service.UserService</value>
    18     </property>
    19     <property name="interceptorNames">
    20         <list>
    21             <value>myAOP</value>
    22         </list>
    23     </property>
    24 </bean>
    25 </beans>
    springAOP-xml

    ②接口和实现类

    1 package com.boom.xml.service;
    2 
    3 public interface UserService {
    4     
    5     void addUser();
    6 }
    interface
     1 package com.boom.xml.service.impl;
     2 
     3 import com.boom.xml.service.UserService;
     4 
     5 public class UserServiceImpl implements UserService {
     6 
     7     @Override
     8     public void addUser() {
     9         System.out.println("UserService...");
    10     }
    11     
    12 }
    implements

    ③AOP

     1 package com.boom.xml.aop;
     2 
     3 import java.lang.reflect.Method;
     4 import java.lang.reflect.Proxy;
     5 
     6 import org.aopalliance.intercept.MethodInterceptor;
     7 import org.aopalliance.intercept.MethodInvocation;
     8 import org.springframework.aop.AfterReturningAdvice;
     9 import org.springframework.aop.MethodBeforeAdvice;
    10 import org.springframework.aop.framework.ProxyFactoryBean;
    11 
    12 public class MyAOP implements MethodBeforeAdvice, AfterReturningAdvice, MethodInterceptor {
    13 
    14     @Override
    15     public Object invoke(MethodInvocation arg0) throws Throwable {
    16         System.out.println("AOP联盟...before");
    17         Object obj = arg0.proceed();
    18         System.out.println("AOP联盟...after");
    19         return obj;
    20     }
    21 
    22     @Override
    23     public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
    24         System.out.println("Spring...after");
    25         
    26     }
    27 
    28     @Override
    29     public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
    30         System.out.println("Spring...before");
    31     }
    32 
    33 }
    MyAOP

    ④测试类

     1 package com.boom.xml.test;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 import com.boom.xml.service.UserService;
     7 
     8 
     9 public class Test {
    10     public static void main(String[] args) {
    11         //启动spring框架
    12         ApplicationContext ac = new ClassPathXmlApplicationContext("springAOP-xml.xml");
    13         UserService us = (UserService)ac.getBean("userServieAOP");
    14         us.addUser();
    15         
    16     }
    17 }
    View Code

    ⑤运行结果

    spring aspect:在配置文件里配置<aop:config>,<aop:advisor>配置前置后置环绕等通知。
    ①springAOP-springaspect.xml 

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!-- XSD约束的XML配置文件 -->
     3 <!-- XSD约束的XML配置文件
     4     xmlns - xml namespace 命名空间, 默认命名空间.代表定义标签不需要写前缀.
     5     xmlns:xxx - 定义命名空间xxx. 使用标签为: <xxx:xxx>
     6  -->
     7 <beans xmlns="http://www.springframework.org/schema/beans"
     8     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     9     xmlns:aop="http://www.springframework.org/schema/aop"
    10     xmlns:tx="http://www.springframework.org/schema/tx"
    11     xsi:schemaLocation="http://www.springframework.org/schema/beans
    12         http://www.springframework.org/schema/beans/spring-beans.xsd
    13         http://www.springframework.org/schema/aop
    14         http://www.springframework.org/schema/aop/spring-aop.xsd
    15         http://www.springframework.org/schema/tx
    16         http://www.springframework.org/schema/tx/spring-tx.xsd">
    17     <!-- 目标 -->
    18 <bean id="userService" class="com.boom.springaspect.service.impl.UserServiceImpl"></bean>
    19     <!-- 配置切面 -->
    20     <bean id="MyAfterAdvice" class="com.boom.springaspect.aop.MyAfterAdvice"></bean>
    21     <bean id="MyAroundAdvice" class="com.boom.springaspect.aop.MyAroundAdvice"></bean>
    22     <bean id="MyBeforeAdvice" class="com.boom.springaspect.aop.MyBeforeAdvice"></bean>
    23     <bean id="MyThrowsAdvice" class="com.boom.springaspect.aop.MyThrowsAdvice"></bean>
    24     
    25     <aop:config>
    26         <aop:pointcut expression="execution(* com.boom.springaspect.service.*.*(..))" id="mypointcut"/>
    27         <aop:advisor advice-ref="MyBeforeAdvice" pointcut-ref="mypointcut"/>
    28         <aop:advisor advice-ref="MyAfterAdvice" pointcut-ref="mypointcut"/>
    29         <aop:advisor advice-ref="MyAroundAdvice" pointcut-ref="mypointcut"/>
    30         <aop:advisor advice-ref="MyThrowsAdvice" pointcut-ref="mypointcut"/>
    31     </aop:config>
    32 </beans>
    springAOP-springaspect

    ②接口和实现类

    1 package com.boom.springaspect.service;
    2 
    3 public interface UserService {
    4 
    5     String addUser();
    6 }
    interface
     1 package com.boom.springaspect.service.impl;
     2 
     3 import com.boom.springaspect.service.UserService;
     4 
     5 public class UserServiceImpl implements UserService {
     6 
     7     public String addUser() {
     8         System.out.println("addUser..........");
     9         return "aaa";
    10     }
    11 
    12 }
    implements

    ③AOP

     1 package com.boom.springaspect.aop;
     2 
     3 import java.lang.reflect.Method;
     4 
     5 import org.springframework.aop.MethodBeforeAdvice;
     6 
     7 public class MyBeforeAdvice implements MethodBeforeAdvice {
     8 
     9     @Override
    10     public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
    11         System.out.println("Before.......");
    12     }
    13 
    14 }
    MyBeforeAdvice
     1 package com.boom.springaspect.aop;
     2 
     3 import java.lang.reflect.Method;
     4 
     5 import org.springframework.aop.AfterReturningAdvice;
     6 
     7 public class MyAfterAdvice implements AfterReturningAdvice {
     8 
     9     @Override
    10     public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
    11         System.out.println("After....");
    12     }
    13 
    14 }
    MyAfterAdvice
     1 package com.boom.springaspect.aop;
     2 
     3 import org.aopalliance.intercept.MethodInterceptor;
     4 import org.aopalliance.intercept.MethodInvocation;
     5 
     6 public class MyAroundAdvice implements MethodInterceptor {
     7 
     8     @Override
     9     public Object invoke(MethodInvocation arg0) throws Throwable {
    10         System.out.println("AOP......Before");
    11         Object obj = arg0.proceed();
    12         System.out.println("AOP.......After");
    13         return obj;
    14     }
    15 
    16 }
    MyAroundAdvice
     1 package com.boom.springaspect.aop;
     2 
     3 import org.springframework.aop.ThrowsAdvice;
     4 
     5 public class MyThrowsAdvice implements ThrowsAdvice {
     6 
     7     public void afterThrowing(RuntimeException e){
     8         System.out.println(e.getClass().getName());
     9     }
    10 }
    MyThrowsAdvice

    ④测试

     1 package com.boom.springaspect.test;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 import com.boom.springaspect.service.UserService;
     7 
     8 
     9 public class Test {
    10 
    11     public static void main(String[] args) {
    12         ApplicationContext ac = new ClassPathXmlApplicationContext("springAOP-springaspect.xml");
    13         UserService us = (UserService)ac.getBean("userService");
    14         us.addUser();
    15     }
    16 
    17 }
    Test

    ⑤运行结果

    spring aspectj 方式:(spring 2.0以后引入)是专门做springAOP一个框架/技术,一开始并不是spring的,后来被spring收购了。AOP比xml更加通  用,简化。AspectJ不属于spring的标准技术,所以切面不需要实现spring中的通知接口。AspctJ的特点是通过方法+配置文件来定义aop
    ①springAOP-aspectj.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!-- XSD约束的XML配置文件 -->
     3 <beans xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xmlns:aop="http://www.springframework.org/schema/aop"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xsi:schemaLocation="http://www.springframework.org/schema/beans
     8         http://www.springframework.org/schema/beans/spring-beans.xsd
     9         http://www.springframework.org/schema/aop
    10         http://www.springframework.org/schema/aop/spring-aop.xsd
    11         http://www.springframework.org/schema/tx
    12         http://www.springframework.org/schema/tx/spring-tx.xsd">
    13     <!-- 目标 -->
    14     <bean id="userService" class="com.boom.aspectj.service.impl.UserServiceImpl"></bean>
    15     <!-- 配置切面 -->
    16     <bean id="myAOP" class="com.boom.aspectj.aop.MyAOPAspectj"></bean>
    17     <!-- 配置切点 -->
    18     <aop:config>
    19     <!-- 指定切面 -->
    20         <aop:aspect id="myaop" ref="myAOP">
    21         <!-- 配置通知行为 -->
    22             <aop:before method="doBefore" pointcut="execution( * com.boom.aspectj.service.*.*(..))"/>
    23             <aop:after-returning method="doAfter" returning="obj" pointcut="execution( * com.boom.aspectj.service.*.*(..))" />
    24             <aop:after-throwing method="doAfterThrowing" throwing="ex" pointcut="execution( * com.boom.aspectj.service.*.*(..))"/>
    25             <aop:around method="doAround" pointcut="execution( * com.boom.aspectj.service.*.*(..))"/>
    26         </aop:aspect>
    27     </aop:config>
    28 </beans>
    springAOP-aspectj

    ②接口和实现类

    1 package com.boom.aspectj.service;
    2 
    3 public interface UserService {
    4     
    5     void addUser();
    6 }
    interface
     1 package com.boom.aspectj.service.impl;
     2 
     3 import com.boom.aspectj.service.UserService;
     4 
     5 public class UserServiceImpl implements UserService {
     6 
     7     @Override
     8     public void addUser() {
     9         System.out.println("UserService...");
    10     }
    11     
    12 }
    implements

    ③AOP

     1 package com.boom.aspectj.aop;
     2 
     3 import org.aspectj.lang.ProceedingJoinPoint;
     4 /**
     5  * AspectJ不属于spring的标准技术,所以切面不需要实现spring中的通知接口
     6  * AspctJ的特点是通过方法+配置文件来定义aop
     7  * @project_name springAOP 
     8  * @class_name MyAOPAspectj
     9  * @author Dilraba
    10  */
    11 public class MyAOPAspectj {
    12     public void doBefore(){
    13         System.out.println("Before...");
    14     }
    15     public void doAfter(Object obj){
    16         System.out.println("After...");
    17     }
    18     public void doAfterThrowing(RuntimeException ex){
    19         System.out.println(ex.getClass().getName());
    20         System.out.println("doAfterThrowing...");
    21     }
    22     public void doAround(ProceedingJoinPoint joinPoint) throws Throwable{
    23         System.out.println("doAround...before");
    24         joinPoint.proceed();
    25         System.out.println("doAround...after");
    26     }
    27 }
    MyAOPAspectj

    ④测试

     1 package com.boom.aspectj.test;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 import com.boom.aspectj.service.UserService;
     7 
     8 
     9 public class Test {
    10 
    11     public static void main(String[] args) {
    12         ApplicationContext ac = new ClassPathXmlApplicationContext("springAOP-aspectj.xml");
    13         UserService us = (UserService)ac.getBean("userService");
    14         us.addUser();
    15         
    16     }
    17 
    18 }
    Test

    ⑤运行结果

    spring annotation:需要使用Spring的注解和AspectJ的注解(注解就是)
    ①springAOP-annotation.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!-- XSD约束的XML配置文件 -->
     3 <!-- XSD约束的XML配置文件
     4     xmlns - xml namespace 命名空间, 默认命名空间.代表定义标签不需要写前缀.
     5     xmlns:xxx - 定义命名空间xxx. 使用标签为: <xxx:xxx>
     6  -->
     7 <beans xmlns="http://www.springframework.org/schema/beans"
     8     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
     9     xmlns:context="http://www.springframework.org/schema/context"
    10     xsi:schemaLocation="http://www.springframework.org/schema/beans 
    11     http://www.springframework.org/schema/beans/spring-beans.xsd 
    12     http://www.springframework.org/schema/aop 
    13     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    14     http://www.springframework.org/schema/context  
    15     http://www.springframework.org/schema/context/spring-context.xsd">
    16     
    17     <context:component-scan base-package="com.boom.annotation"></context:component-scan>
    18     <!-- 开启注解 -->
    19     <context:annotation-config />
    20     <!-- 开启aspectj代理 -->
    21     <aop:aspectj-autoproxy />
    22 </beans>
    springAOP-annotation

    ②接口和实现类

    1 package com.boom.annotation.service;
    2 
    3 public interface UserService {
    4     void addUser();
    5 }
    interface
     1 package com.boom.annotation.service.impl;
     2 
     3 import org.springframework.stereotype.Service;
     4 
     5 import com.boom.annotation.service.UserService;
     6 
     7 @Service
     8 public class UserServiceImpl implements UserService {
     9 
    10     @Override
    11     public void addUser() {
    12         System.out.println("addUser........");
    13 
    14     }
    15 
    16 }
    implements

    ③AOP

     1 package com.boom.annotation.aop;
     2 
     3 import org.aspectj.lang.ProceedingJoinPoint;
     4 import org.aspectj.lang.annotation.After;
     5 import org.aspectj.lang.annotation.Around;
     6 import org.aspectj.lang.annotation.Aspect;
     7 import org.aspectj.lang.annotation.Before;
     8 import org.aspectj.lang.annotation.Pointcut;
     9 import org.springframework.stereotype.Component;
    10 
    11 @Aspect
    12 @Component
    13 public class MyAOP {
    14     
    15     @Pointcut("execution(* com.boom.annotation.service.*.*(..))")
    16     public void suibian(){
    17         
    18     }
    19     
    20     @Before("suibian()")
    21     public void doBefore(){
    22         System.out.println("doBefore......");
    23     }
    24     
    25     @After("suibian()")
    26     public void doAfter(){
    27         System.out.println("doAfter");
    28     }
    29     
    30     @Around("suibian()")
    31     public void around(ProceedingJoinPoint joinPoint)throws Throwable{
    32         System.out.println("around.....before");
    33         joinPoint.proceed();
    34         System.out.println("around.....after");
    35     }
    36 }
    MyAOP

    ④测试

     1 package com.boom.annotation.test;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 import com.boom.annotation.service.UserService;
     7 
     8 
     9 public class Test {
    10 
    11     public static void main(String[] args) {
    12         //spring启动
    13         ApplicationContext ac = new ClassPathXmlApplicationContext("springAOP-annotation.xml");
    14         UserService us = (UserService)ac.getBean(UserService.class);
    15         us.addUser();
    16     }
    17 
    18 }
    Test

    ⑤运行结果

  • 相关阅读:
    导热硅胶片的特性
    电子器件热设计估算技巧 1
    什么是热阻
    功率器件的热设计
    ESP8266EX 电路设计
    STM8S005 ADC采集 5位共阴数码管显示
    PCB设计-Allegro软件入门系列第九讲-Class分类和Subclass应用
    STM32 滴答时钟 SysTick 的使用 STM32F103C8T6
    SPI 串行Flash闪存W25Q128FV 的使用(STM32F407)_基础篇
    如何居中一个元素
  • 原文地址:https://www.cnblogs.com/cao-yin/p/10531414.html
Copyright © 2011-2022 走看看