随笔分类 - Spring注解驱动开发
Spring事务的传播行为
摘要:# Spring事务的七种传播行为 **首先举例事务的嵌套:** ```java ServiceA { void methodA() { ServiceB.methodB(); } } ServiceB { void methodB() { } } ``` *其中ServiceA#methodA(我
十四、【AOP】基本使用
摘要:AOP(Aspect Oriented Programming):面向切面编程。AOP是在我们原来写的代码的基础上,进行一定的包装,比如在方法执行前、方法返回后、方法抛出异常后等地方进行一定的拦截处理或者增强处理。我们需要实现一个代理来创建实例,实际运行的实例其实是生成的代理类的实例。 Spring
十三、【接口】Spring接口xxxAware
摘要:Aware接口是一个标志性接口,继承此接口的接口xxxAware的实现类,在容器创建完成后,会回调实现方法,下面举例: 有很多xxxAware接口,下面举两个例子 /** * description: 将实现xxxAware接口的Bean注册到IOC容器中的时候,会将xxxAware的实现方法进行回
十二、【注解】Spring注解@Profile
摘要:Spring为我们提供的多环境启动 配置类,注入三个不同环境的数据源,并加上注解 /** * @author zhangjianbing * @date 2020年9月23日 */ @Configuration public class MyConfig { @Bean @Profile("dev"
十一、【注解】Spring注解@Value
摘要:首先定义实体 /** * @author zhangjianbing * @date 2020年9月23日 */ @Data public class Apple { @Value("${apple.color}") private String color; @Value("红富士") priva
十、【生命周期】BeanPostProcessor后置处理器
摘要:Spring的Bean后置处理器,BeanPostProcessor主要是在类初始化之前,跟之后处理相应的事。 实体类 public class Train { public Train() { System.out.println("Train构造方法执行。。。。。。"); } public vo
九、【生命周期】注解@PostConstruct和@PreDestroy
摘要:@PostConstruct和@PreDestroy来自JSR250规范,在构造方法执行,并且赋值完成后执行PostConstruct,在容器移除对象之前执行PreDestroy。 实体类 public class Dog { public Dog() { System.out.println("D
八、【生命周期】接口InitializingBean和DisposableBean
摘要:Bean实现接口InitializingBean和DisposableBean Bean实体类 /** * @author zhangjianbing * @since 2020/9/22 */ public class BlueYes implements InitializingBean, Di
七、【生命周期】注解@Bean
摘要:@Bean(initMethod = "init", destroyMethod = "destroy")方式 Bean实体 @Getter @Setter @ToString @AllArgsConstructor public class Person { private String name
六、【接口】Spring接口FactoryBean
摘要:FactoryBean是用来向容器中注入Bean的接口。而BeanFactory是从容器中取Bean的接口。 定义Fish实体类 /** * @author zhangjianbing * @date 2020年9月23日 */ @Data public class Fish { private S
五、【注解】Spring注解@Import
摘要:@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Import { /** * {@link Configuration}, {@link ImportSelecto
四、【注解】Spring注解@Conditional
摘要:采用注解的方式来注入bean 编写config类 /** * @author zhangjianbing * time 2020/09/23 * https://www.zhangjianbing.com */ @Configuration public class ConditionConfig
三、【注解】Spring注解@Lazy
摘要:懒加载,只有当使用Bean的时候才会被实例化。 @Configuration public class PersonConfig { /** * 懒加载主要用在单例模式上 * 标注了懒加载注解,容器初始化过程中不会实例化Bean * 只有当使用的时候才会实例化Bean,且只会实例化一次 */ @La
二、【注解】Spring注解@Scope
摘要:Spring容器中的Bean默认是单例的,可以使用@Scope来设置Bean的类型 /** * @author zhangjianbing * time 2020/09/23 * https://www.zhangjianbing.com */ @Configuration public class
一、【注解】Spring注解@ComponentScan
摘要:@ComponentScan作用是将标注了@Controller、@Service、@Repository等注解的类放入IOC容器中,统一管理。 一、基本用法 Component组件: @Controller public class UserController { } @Service publ
Spring源码窥探之:注解方式的AOP原理
摘要:AOP入口代码分析 通过注解的方式来实现AOP 1. @EnableAspectJAutoProxy通过@Import注解向容器中注入了AspectJAutoProxyRegistrar这个类,而它在容器中的名字是org.springframework.aop.config.internalAuto
Spring源码窥探之:单实例Bean的创建过程
摘要:finishBeanFactoryInitialization(beanFactory);初始化剩下的所有的单实例(非懒加载)Bean(Instantiate all remaining (non-lazy-init) singletons.) 1. beanFactory.preInstantia
Spring源码窥探之:扩展原理BeanDefinitionRegistryPostProcessor
摘要:BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor,其中有两个接口,postProcessBeanDefinitionRegistry是BeanDefinitionRegistryPostProcessor自带的,postPr
Spring源码窥探之:扩展原理BeanFactoryPostProcessor
摘要:2. 配置类,其中注入了一个实体类,跟一个MyBeanFactoryPostProcessor 3. 测试类 4. 结果,可见postProcessBeanFactory方法先于构造方法执行 BeanFactoryPostProcessor调用栈:1. AnnotationConfigApplica
Spring事务源码梳理
摘要:事务增强器要用事务注解信息:AnnotationTransactionAttributeSource来解析事务注解 事务拦截器中:transactionInterceptor(),它是一个TransactionInterceptor(保存了事务属性信息和事务管理器),而TransactionInte