文章目录 1.模块创建和依赖引入 1.聚合模块,下面有一个myspring 2.查看父模块是否管理了子模块 3.myspring模块引入基本包 2.进行环境搭建 1.目录概览 2.UserController.java 3.UserService.java 4.UserDao.java 5.AppMain.java 6.beans.xml 7.测试 8.配置UserController.java为多例的,然后测试 9.UserService调用add方法,然后测试 10.引入Bean后置处理器 1.位置 2.MyBeanPostProcessor.java 3.UserService.java 设置初始化方法 4.beans.xml配置扫描 5.测试 6.注意事项 11.引入AOP 1.目录 2.SmartAnimal.java 3.SmartDog.java 4.SmartAnimalAspect.java 5.beans.xml开启aop注解并扫描aop包 6.AppMain.java 测试AOP 6.简单分析AOP和后置处理器的关系 3.抛出问题 1.bean是怎么注入容器的? 2.为什么加了@Autowired就能被依赖注入? 3.单例多例是怎么实现的? 4.Bean的后置处理器是怎么实现的? 5.原生Spring的AOP是如何实现的? 4.将代码放到远程仓库
1.模块创建和依赖引入
1.聚合模块,下面有一个myspring
2.查看父模块是否管理了子模块
3.myspring模块引入基本包
< dependencies> < dependency> < groupId> org.springframework</ groupId> < artifactId> spring-context</ artifactId> < version> 5.3.8</ version> </ dependency> < dependency> < groupId> org.springframework</ groupId> < artifactId> spring-aspects</ artifactId> < version> 5.3.8</ version> </ dependency>
</ dependencies>
2.进行环境搭建
1.目录概览
2.UserController.java
package com. sunxiansheng. myspring. component ; import org. springframework. stereotype. Component ;
@Component
public class UserController {
}
3.UserService.java
package com. sunxiansheng. myspring. component ; import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Component ;
@Component
public class UserService { @Autowired private UserDao userDao; public void add ( ) { System . out. println ( "UserService 调用了UserDao的add方法" ) ; userDao. add ( ) ; } }
4.UserDao.java
package com. sunxiansheng. myspring. component ; import org. springframework. stereotype. Component ;
@Component
public class UserDao { public void add ( ) { System . out. println ( "UserDao add..." ) ; } }
5.AppMain.java
package com. sunxiansheng. myspring ; import com. sunxiansheng. myspring. component. UserController ;
import com. sunxiansheng. myspring. component. UserDao ;
import com. sunxiansheng. myspring. component. UserService ;
import org. springframework. context. ApplicationContext ;
import org. springframework. context. support. ClassPathXmlApplicationContext ;
public class AppMain { public static void main ( String [ ] args) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; UserController userController1 = ( UserController ) ioc. getBean ( "userController" ) ; UserController userController2 = ( UserController ) ioc. getBean ( "userController" ) ; System . out. println ( "userController1 == userController2 ? " + ( userController1 == userController2) ) ; UserService userService = ( UserService ) ioc. getBean ( "userService" ) ; System . out. println ( "userService = " + userService) ; UserDao userDao = ( UserDao ) ioc. getBean ( "userDao" ) ; System . out. println ( "userDao = " + userDao) ; } }
6.beans.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: 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 https://www.springframework.org/schema/context/spring-context.xsd" > < context: component-scan base-package = " com.sunxiansheng.myspring.component" /> </ beans>
7.测试
8.配置UserController.java为多例的,然后测试
9.UserService调用add方法,然后测试
10.引入Bean后置处理器
1.位置
2.MyBeanPostProcessor.java
package com. sunxiansheng. myspring. process ; import org. springframework. beans. factory. config. BeanPostProcessor ;
import org. springframework. stereotype. Component ;
@Component
public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization ( Object bean, String beanName) { System . out. println ( "postProcessBeforeInitialization..." + beanName+ "=>" + bean. getClass ( ) ) ; return bean; } @Override public Object postProcessAfterInitialization ( Object bean, String beanName) { System . out. println ( "postProcessAfterInitialization..." + beanName+ "=>" + bean. getClass ( ) ) ; return bean; } }
3.UserService.java 设置初始化方法
4.beans.xml配置扫描
5.测试
6.注意事项
bean的后置处理器在初始化方法调用前后执行 触发时机为单例的第一次getBean和多例的每次getBean,也就是,每个Bean都会经过Bean的后置处理器处理
11.引入AOP
1.目录
2.SmartAnimal.java
package com. sunxiansheng. myspring. aop ;
public interface SmartAnimal { public float getSum ( float a, float b) ; public float getSub ( float a, float b) ; }
3.SmartDog.java
package com. sunxiansheng. myspring. aop ; import org. springframework. stereotype. Component ;
@Component
public class SmartDog implements SmartAnimal { @Override public float getSum ( float a, float b) { System . out. println ( "SmartDog...getSum...res=" + ( a + b) ) ; return a + b; } @Override public float getSub ( float a, float b) { System . out. println ( "SmartDog...getSub...res=" + ( a - b) ) ; return a - b; }
}
4.SmartAnimalAspect.java
package com. sunxiansheng. myspring. aop ; import org. aspectj. lang. JoinPoint ;
import org. aspectj. lang. ProceedingJoinPoint ;
import org. aspectj. lang. Signature ;
import org. aspectj. lang. annotation. * ;
import org. aspectj. lang. reflect. MethodSignature ;
import org. springframework. stereotype. Component ; import java. util. Arrays ;
@Component
@Aspect
public class SmartAnimalAspect { @Before ( value = "execution(public float com.sunxiansheng.myspring.aop.SmartDog.getSub(float, float))" ) public void before ( JoinPoint joinPoint) { Signature signature = joinPoint. getSignature ( ) ; System . out. println ( "方法执行开始-日志-方法名-" + signature. getName ( ) + "-参数" + Arrays . asList ( joinPoint. getArgs ( ) ) ) ; SmartDog target = ( SmartDog ) joinPoint. getTarget ( ) ; System . out. println ( "目标对象-" + target. getClass ( ) ) ; } @AfterReturning ( value = "execution(public float com.sunxiansheng.myspring.aop.SmartDog.getSub(float, float))" ) public void afterReturning ( JoinPoint joinPoint) { Signature signature = joinPoint. getSignature ( ) ; System . out. println ( "方法执行正常结束-日志-方法名-" + signature. getName ( ) ) ; } @AfterThrowing ( value = "execution(public float com.sunxiansheng.myspring.aop.SmartDog.getSub(float, float))" ) public void throwing ( JoinPoint joinPoint) { Signature signature = joinPoint. getSignature ( ) ; System . out. println ( "方法出现异常-日志-方法名-" + signature. getName ( ) ) ; } @After ( value = "execution(public float com.sunxiansheng.myspring.aop.SmartDog.getSub(float, float))" ) public void after ( JoinPoint joinPoint) { Signature signature = joinPoint. getSignature ( ) ; System . out. println ( "方法最终执行完毕-日志-方法名-" + signature. getName ( ) ) ; } @Around ( "execution(public float com.sunxiansheng.myspring.aop.SmartDog.getSub(float, float))" ) public Object logMethodExecution ( ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature signature = ( MethodSignature ) joinPoint. getSignature ( ) ; String methodName = signature. getMethod ( ) . getName ( ) ; String className = signature. getDeclaringType ( ) . getSimpleName ( ) ; System . out. println ( "环绕通知 method " + className + "." + methodName) ; Object targetObject = joinPoint. getTarget ( ) ; System . out. println ( "环绕通知获取目标对象:" + targetObject) ; try { System . out. println ( "环绕通知获取参数:" + Arrays . asList ( joinPoint. getArgs ( ) ) ) ; Object result = joinPoint. proceed ( ) ; System . out. println ( "环绕通知获取结果:" + result) ; return result; } catch ( Exception e) { throw e; } finally { System . out. println ( "环绕通知 method " + className + "." + methodName) ; } } }
5.beans.xml开启aop注解并扫描aop包
6.AppMain.java 测试AOP
6.简单分析AOP和后置处理器的关系
3.抛出问题
1.bean是怎么注入容器的?
2.为什么加了@Autowired就能被依赖注入?
3.单例多例是怎么实现的?
4.Bean的后置处理器是怎么实现的?
5.原生Spring的AOP是如何实现的?
4.将代码放到远程仓库
1.VCS -》 share
2.查看仓库