wuheng %!s(int64=2) %!d(string=hai) anos
pai
achega
5d42e4d891

+ 1 - 1
day01/src/main/resources/users.xml

@@ -4,7 +4,7 @@
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
     <!-- 我们在这里创建对象是为了 让Spring框架接手对象的创建管理权限, IOC 又叫控制反转 -->
-    <bean id="persion" class="com.lovecoding.beans.Persion" >
+    <bean id="persion" class="com.lovecoding.beans.Person" >
         <!-- DI 注入 -->
 <!--        <property name="user" ref="user"  />-->
 <!--连级引用 并赋值-->

+ 19 - 0
day02/pom.xml

@@ -28,6 +28,25 @@
             <artifactId>spring-beans</artifactId>
             <version>5.3.22</version>
         </dependency>
+<!-- Spring Aop 接口Jar包 -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-aop</artifactId>
+            <version>5.3.22</version>
+        </dependency>
+<!--   Spring Aop实现jar包     -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-aspects</artifactId>
+            <version>5.3.22</version>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>RELEASE</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 
 </project>

+ 11 - 0
day02/src/main/java/com/lovecoding/SpringConfig.java

@@ -0,0 +1,11 @@
+package com.lovecoding;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
+
+@Configuration
+@ComponentScan("com.lovecoding")
+@EnableAspectJAutoProxy
+public class SpringConfig {
+}

+ 6 - 0
day02/src/main/java/com/lovecoding/aop/Counter.java

@@ -0,0 +1,6 @@
+package com.lovecoding.aop;
+
+public interface Counter {
+    int add(int a, int b);
+    int sub(int a, int b);
+}

+ 36 - 0
day02/src/main/java/com/lovecoding/aop/CounterAop.java

@@ -0,0 +1,36 @@
+package com.lovecoding.aop;
+
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+
+/**
+ * 代理对象, 它代替我们去执行业务代码
+ * 代替我们去 调用加减法
+ * 静态代理
+ * 因为代理对象是 编写到代码中的, 不是运行时创建的, 所以叫静态代理
+ * 还有动态代理  动态代理是不需要固定代理对象的
+ * 用的技术就是 反射
+ */
+@Component
+public class CounterAop implements Counter {
+
+    @Resource(name = "countterImpl")
+    private Counter counter;
+
+    @Override
+    public int add(int a, int b) {
+        System.out.println( "[日志]加法参数:" + a + " 参数:" + b );
+        int n = counter.add(a, b);
+        System.out.println( "[日志]加法计算结构:" + n );
+        return n;
+    }
+
+    @Override
+    public int sub(int a, int b) {
+        System.out.println( "[日志]减法参数:" + a + " 参数:" + b );
+        int n = counter.sub(a, b);
+        System.out.println( "[日志]减法计算结构:" + n );
+        return n;
+    }
+}

+ 24 - 0
day02/src/main/java/com/lovecoding/aop/CounterPorxy.java

@@ -0,0 +1,24 @@
+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.springframework.stereotype.Component;
+
+@Aspect
+@Component
+public class CounterPorxy {
+
+    /**
+     * 接收通知的方法
+     * Before 前置通知
+     * After 后置通知
+     * AfterReturning 返回通知
+     */
+    @Before("execution(* com.lovecoding.aop.CountterImpl.*(..))")
+    public void add(){
+        System.out.println( "即将执行加法操作" );
+    }
+
+}

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

@@ -0,0 +1,20 @@
+package com.lovecoding.aop;
+
+import org.springframework.stereotype.Component;
+
+@Component("countterImpl")
+public class CountterImpl implements Counter {
+    @Override
+    public int add(int a, int b) {
+        int n = a + b;
+        System.out.println( "加法计算结果:" + n );
+        return n;
+    }
+
+    @Override
+    public int sub(int a, int b) {
+        int n = a - b;
+        System.out.println( "减法计算结果:" + n );
+        return n;
+    }
+}

+ 21 - 0
day02/src/main/java/com/lovecoding/aop/TestAop.java

@@ -0,0 +1,21 @@
+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);
+
+//        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);
+
+    }
+}

+ 38 - 12
day02/src/main/java/com/lovecoding/controller/LoginController.java

@@ -1,8 +1,13 @@
 package com.lovecoding.controller;
 
 
+import com.lovecoding.SpringConfig;
 import com.lovecoding.service.LoginService;
+import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.stereotype.Component;
 
@@ -14,8 +19,13 @@ public class LoginController {
     //第一种方法
     //使用属性注入  两行代码解决问题
     //默认是按类型匹配 如果我们使用 属性注入代码最简介
-//    @Autowired
-//    private LoginService loginService;
+        //@Autowired
+//    @Qualifier("LoginServiceImpl1")
+
+    //使用 Resource 默认使用Bean对象ID名注入
+    //如果这个名字 查不到, 则改用 类型注入
+    @Resource
+    private LoginService loginService;
 
     //第二种方法
     //SET方法添加注解 注入对象
@@ -28,22 +38,38 @@ public class LoginController {
 
     //第三种注入方法
     //我们需要 保留构造器, 并且使用注解 我们依然需要 五行代码
-    private LoginService loginService;
-    @Autowired
-    public LoginController(LoginService loginService) {
-        this.loginService = loginService;
-    }
+//    private LoginService loginService;
+//    @Autowired
+//    public LoginController(LoginService loginService) {
+//        this.loginService = loginService;
+//    }
+
+
 
     public void login(){
+        //控制
+        loginService.login();
+        //控制
         loginService.login();
+        //控制
     }
 
+
+
     public static void main(String[] args) {
-        ClassPathXmlApplicationContext context =
-                new ClassPathXmlApplicationContext("anno.xml");
-        LoginController bean = context.getBean(LoginController.class);
-        //System.out.println( bean );
-        bean.login();
+
+
+//        ClassPathXmlApplicationContext context =
+//                new ClassPathXmlApplicationContext("anno.xml");
+//        LoginController bean = context.getBean(LoginController.class);
+
+        //启动Ioc入口
+        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
+
+        LoginController loginService = context.getBean(LoginController.class);
+
+        loginService.login();
+       //
 
     }
 }

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

@@ -4,10 +4,9 @@ import com.lovecoding.dao.LoginDao;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-@Service
+@Service("loginService")
 public class LoginServiceImpl implements LoginService {
 
-
     LoginDao loginDao;
 
     @Autowired

+ 11 - 11
day02/src/main/resources/anno.xml

@@ -1,18 +1,18 @@
-<?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"-->
+<!--       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-->
 
-">
+<!--">-->
 
-<context:component-scan base-package="com.lovecoding">
-</context:component-scan>
+<!--<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>
+<!--</beans>-->

+ 3 - 3
demo01/src/main/resources/login.xml

@@ -3,8 +3,8 @@
        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">
 
-    <bean id="loginController" class="com.LoginController" autowire="byType" />
-    <bean id="loginService" class="com.LoginServiceImpl" autowire="byType" />
-    <bean id="loginDao" class="com.LoginDao" />
+    <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>