wuheng 2 years ago
parent
commit
44b85b9c96

+ 3 - 3
day02/src/main/java/com/lovecoding/SpringConfig.java

@@ -4,8 +4,8 @@ import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
-@Configuration
-@ComponentScan("com.lovecoding")
-@EnableAspectJAutoProxy
+@Configuration  //声明是配置类
+@ComponentScan("com.lovecoding")  //配置IOC扫包包名
+@EnableAspectJAutoProxy  //开启AOP自动代理
 public class SpringConfig {
 }

+ 23 - 8
day02/src/main/java/com/lovecoding/aop/TestAop.java → day02/src/main/java/com/lovecoding/aop/ConfigAop.java

@@ -3,19 +3,34 @@ package com.lovecoding.aop;
 import com.lovecoding.SpringConfig;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
-public class TestAop {
-    public static void main(String[] args) {
-        AnnotationConfigApplicationContext context =
-                new AnnotationConfigApplicationContext(SpringConfig.class);
+/**
+ * 通过配置类 配置AOP切片
+ *
+ */
+public class ConfigAop {
 
+//    /**
+//     * 静态代理 模拟AOP效果
+//     * @param args
+//     */
+//    public static void main(String[] args) {
+//        AnnotationConfigApplicationContext context =
+//                new AnnotationConfigApplicationContext(SpringConfig.class);
 //        CounterAop bean = context.getBean(CounterAop.class);
 //        bean.add( 5, 5 );
 //        bean.sub( 10, 5 );
+//    }
 
-        Counter bean = context.getBean("countterImpl", Counter.class);
-        //bean.add(5, 5);
-
-        bean.sub(5, 5);
 
+    /**
+     * 使用 Spring AOP 实现功能的切片
+     * @param args
+     */
+    public static void main(String[] args) {
+        AnnotationConfigApplicationContext context =
+                new AnnotationConfigApplicationContext(SpringConfig.class);
+        Counter bean = context.getBean("countterImpl", Counter.class);
+        bean.add(5, 5);
+        //bean.sub(5, 5);
     }
 }

+ 39 - 6
day02/src/main/java/com/lovecoding/aop/CounterPorxy.java

@@ -1,9 +1,9 @@
 package com.lovecoding.aop;
 
-import org.aspectj.lang.annotation.After;
-import org.aspectj.lang.annotation.AfterReturning;
-import org.aspectj.lang.annotation.Aspect;
-import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.*;
 import org.springframework.stereotype.Component;
 
 @Aspect
@@ -15,10 +15,43 @@ public class CounterPorxy {
      * Before 前置通知
      * After 后置通知
      * AfterReturning 返回通知
+     * Around  环绕通知
+     * AfterThrowing 异常通知
      */
     @Before("execution(* com.lovecoding.aop.CountterImpl.*(..))")
-    public void add(){
-        System.out.println( "即将执行加法操作" );
+    public void add(JoinPoint joinPoint){
+        System.out.println( "前置通知" );
+    }
+
+    @After("execution(public int com.lovecoding.aop.CountterImpl.*(..))")
+    public void AfterAdd(JoinPoint joinPoint){
+        Signature signature = joinPoint.getSignature();
+        String name = signature.getName();
+        //System.out.println( "切入点 方法" + name );
+        System.out.println( "后置通知" );
+    }
+
+    @AfterReturning("execution(public int com.lovecoding.aop.CountterImpl.*(..))")
+    public void afterReturning(JoinPoint joinPoint){
+        System.out.println( "返回通知" );
+    }
+
+    @Around("execution(public int com.lovecoding.aop.CountterImpl.*(..))")
+    public Object around( ProceedingJoinPoint joinPoint ){
+        try {
+            System.out.println( "环绕通知" );
+            return joinPoint.proceed(joinPoint.getArgs());
+        } catch (Throwable e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    @AfterThrowing("execution(public int com.lovecoding.aop.CountterImpl.*(..))")
+    public void AfterThrowing(JoinPoint joinPoint){
+        Signature signature = joinPoint.getSignature();
+        String name = signature.getName();
+        System.out.println( "方法 :" + name + "发生异常" );
     }
 
 }

+ 2 - 0
day02/src/main/java/com/lovecoding/aop/CountterImpl.java

@@ -4,6 +4,7 @@ import org.springframework.stereotype.Component;
 
 @Component("countterImpl")
 public class CountterImpl implements Counter {
+
     @Override
     public int add(int a, int b) {
         int n = a + b;
@@ -14,6 +15,7 @@ public class CountterImpl implements Counter {
     @Override
     public int sub(int a, int b) {
         int n = a - b;
+        int s = 10 / 0;
         System.out.println( "减法计算结果:" + n );
         return n;
     }

+ 16 - 0
day02/src/main/java/com/lovecoding/aop/XmlAop.java

@@ -0,0 +1,16 @@
+package com.lovecoding.aop;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * 使用 XML 配置 AOP 切片
+ *  效果和配置类无异样
+ */
+public class XmlAop {
+    public static void main(String[] args) {
+        ClassPathXmlApplicationContext con =
+                new ClassPathXmlApplicationContext("anno.xml");
+        Counter countterImpl = con.getBean("countterImpl", Counter.class);
+        countterImpl.add(6,6);
+    }
+}

+ 6 - 10
day02/src/main/java/com/lovecoding/controller/LoginController.java

@@ -20,10 +20,11 @@ public class LoginController {
     //使用属性注入  两行代码解决问题
     //默认是按类型匹配 如果我们使用 属性注入代码最简介
         //@Autowired
-//    @Qualifier("LoginServiceImpl1")
+    //@Qualifier("LoginServiceImpl1")
 
     //使用 Resource 默认使用Bean对象ID名注入
     //如果这个名字 查不到, 则改用 类型注入
+    //使用案例 如果有多个配置档, 那么就必须使用 Bean对象名字注入
     @Resource
     private LoginService loginService;
 
@@ -49,9 +50,6 @@ public class LoginController {
     public void login(){
         //控制
         loginService.login();
-        //控制
-        loginService.login();
-        //控制
     }
 
 
@@ -59,17 +57,15 @@ public class LoginController {
     public static void main(String[] args) {
 
 
+        // XML 启动IOC
 //        ClassPathXmlApplicationContext context =
 //                new ClassPathXmlApplicationContext("anno.xml");
 //        LoginController bean = context.getBean(LoginController.class);
 
-        //启动Ioc入口
-        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
-
+        // 配置类 启动Ioc入口
+        AnnotationConfigApplicationContext context =
+                new AnnotationConfigApplicationContext(SpringConfig.class);
         LoginController loginService = context.getBean(LoginController.class);
-
         loginService.login();
-       //
-
     }
 }

+ 2 - 1
day02/src/main/java/com/lovecoding/service/LoginServiceImpl.java

@@ -4,7 +4,8 @@ import com.lovecoding.dao.LoginDao;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-@Service("loginService")
+//通过 value 配置Bean对象 名字(ID)
+@Service(value = "loginService")
 public class LoginServiceImpl implements LoginService {
 
     LoginDao loginDao;

+ 25 - 16
day02/src/main/resources/anno.xml

@@ -1,18 +1,27 @@
-<!--<?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:context="http://www.springframework.org/schema/context"-->
-<!--       xsi:schemaLocation="-->
-<!--       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd-->
-<!--       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd-->
+<?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:context="http://www.springframework.org/schema/context"
+       xmlns:aop="http://www.springframework.org/schema/aop"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 
-<!--">-->
+">
 
-<!--<context:component-scan base-package="com.lovecoding">-->
-<!--</context:component-scan>-->
-
-<!--    <bean id="loginController" class="com.lovecoding.controller.LoginController" autowire="byType" />-->
-<!--    <bean id="loginService" class="com.lovecoding.service.LoginServiceImpl" autowire="byType" />-->
-<!--    <bean id="loginDao" class="com.lovecoding.dao.LoginDao" />-->
-
-<!--</beans>-->
+<context:component-scan base-package="com.lovecoding">
+</context:component-scan>
+<!--  开启AOP自动代理  -->
+    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
+    <aop:config>
+        <aop:pointcut id="counter" expression="execution(* com.lovecoding.aop.CountterImpl.*(..))" />
+        <aop:aspect ref="counterPorxy">
+            <aop:after method="AfterAdd" pointcut-ref="counter"></aop:after>
+            <aop:before method="add" pointcut-ref="counter" ></aop:before>
+            <aop:after-returning method="afterReturning" pointcut-ref="counter" ></aop:after-returning>
+            <aop:around method="around" pointcut-ref="counter" />
+            <aop:after-throwing method="AfterThrowing" pointcut-ref="counter" />
+        </aop:aspect>
+    </aop:config>
+</beans>