spring
Lei_Da_Gou
这个作者很懒,什么都没留下…
展开
-
02、组件注册-@Configuration&@Bean给容器中注册组件
package com.lei.study_09_13.bean;/** * * * @author LeiLei * @date 2019/9/13 */public class Person { private String name; private int age; public String getName() { ...原创 2019-09-13 19:03:03 · 154 阅读 · 0 评论 -
16、生命周期-BeanPostProcessor原理
原理分析:protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged(new Privi...原创 2019-09-15 16:47:47 · 171 阅读 · 0 评论 -
17、生命周期-BeanPostProcessor在Spring底层的使用
前提说明: 上一篇提到的,BeanPostProcessor两个方法其实就是分别在init、afterProperties方法前后执行。而spirng内部定义了几个BeanPostProcessor,如下图。这里我用ApplicationContextAwareProcessor来说明processor在spring底层的使用。测试代码:package c...原创 2019-09-17 00:36:54 · 158 阅读 · 0 评论 -
18、属性赋值-@Value赋值(一)
/** * 方式一:直接赋值 */@Value("LeiDaGou")private String name;/** * 方式二:使用SpEl表达式 */@Value("#{100-10}")private int age;总结:1.简单赋值,直接给值2.使用SpringEl表达式#{}...原创 2019-09-17 19:56:38 · 323 阅读 · 0 评论 -
19、@Value属性赋值(二)-@PropertySource加载外部配置文件
package com.lei.study_09_17.bean;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/** * * * @author LeiLei * @date 2019/9/17 */@Co...原创 2019-09-17 20:01:43 · 149 阅读 · 0 评论 -
20、自动装配-@Autowired&@Qualifier&@Primary
package com.lei.study_09_17.dao;import org.springframework.stereotype.Repository;/** * @author LeiLei * @description ${DESCRIPTION} * @date 2019/9/17 *///@Repositorypublic class BookDao {...原创 2019-09-17 20:46:18 · 121 阅读 · 0 评论 -
21、自动装配-@Resource&@Inject
@Resource和@Inject注解也可以实现自动装配 @Resource : JSR250规范,默认是按照id装配的,如果ID找不到则装配有@Primary注解的(视频中老师说这个是不支持@Primary是错的,事实 是id比@Primary优先级高而已) @Inject : JSR3...原创 2019-09-17 21:10:05 · 103 阅读 · 0 评论 -
22、@Autowired自动装配其它用法、构造器位置的自动装配
* 写在属性上:属性注入成功* 写在方法上:参数注入成功* 写在参数上:参数注入失败 (构造方法的参数上和普通方法的参数上都失败,因此这里的参数应该是@Bean注解方法的参数上,但是@Bean注解 方法参数上的@Atowired是可以省略的)* 写在构造器上:参数注入成功 (可以省略@Autowired,前提是该方法只能有这一个构造器,且无参构造器都不能写)...原创 2019-09-17 22:10:35 · 176 阅读 · 0 评论 -
23、自动装配-Aware注入Spring底层组件&原理
package com.lei.study_09_18.bean;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanNameAware;import org.springframework.context.ApplicationContext;imp...原创 2019-09-18 21:32:46 · 181 阅读 · 0 评论 -
24、自动装配-@Profile环境搭建
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --><dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version&...原创 2019-09-18 22:26:57 · 127 阅读 · 0 评论 -
25、自动装配-@Profile根据环境注册bean
spring环境生效的两种方式:1.调用Spring的ApplicationContext的无参构造器@Testpublic void testProfile2() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.getE...原创 2019-09-18 22:29:43 · 128 阅读 · 0 评论 -
26、IOC-小结
上图圈的注解在Spring底层用的很多。下面会使用范例:原创 2019-09-18 22:43:42 · 113 阅读 · 0 评论 -
15、生命周期-BeanPostProcessor-后置处理器
package com.lei.study_09_15.processor;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;/** * 后置处理器 * * @author LeiLei * @dat...原创 2019-09-15 15:29:26 · 177 阅读 · 0 评论 -
14、生命周期-@PostConstruct&@PreDestroy
package com.lei.study_09_15.config;import com.lei.study_09_15.bean.Cat;import com.lei.study_09_15.bean.Dog;import com.lei.study_09_15.bean.Person;import org.springframework.context.annotation.B...原创 2019-09-15 14:58:57 · 167 阅读 · 0 评论 -
03、组件注册-@ComponentScan-自动扫描组件&指定扫描规则
package com.lei.study_09_13.config;import com.lei.study_09_13.bean.Person;import org.springframework.context.annotation.*;import org.springframework.stereotype.Controller;/** * * * @author ...原创 2019-09-13 20:19:12 · 172 阅读 · 0 评论 -
04、组件注册-自定义TypeFilter指定过滤规则
package com.lei.study_09_13.config;import org.springframework.core.io.Resource;import org.springframework.core.type.AnnotationMetadata;import org.springframework.core.type.ClassMetadata;import ...原创 2019-09-13 20:50:55 · 260 阅读 · 0 评论 -
05、组件注册-@Scope-设置组件作用域
package com.lei.study_09_13.bean;/** * * * @author LeiLei * @date 2019/9/13 */public class Person { private String name; private int age; public String getName() { retur...原创 2019-09-13 21:18:17 · 144 阅读 · 0 评论 -
06、组件注册-@Lazy懒加载
@Lazy@Beanpublic Person person() { System.out.println("person初始化完成。。。"); return new Person("小明",24);}总结:1.懒加载一般时针对单实例bean来说的,单实例bean默认容器初始化加载,如果想让容器启动快一点就启用懒加载。启用后该单例bean就是多实例bean一...原创 2019-09-13 21:26:00 · 201 阅读 · 0 评论 -
07、组件注册-@Conditional-按照条件注册bean
package com.lei.study_09_14.condition;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.support.BeanDefinitionRegistry;imp...原创 2019-09-14 20:43:41 · 176 阅读 · 0 评论 -
08、组件注册-@Import-给容器中快速导入一个组件
@Configuration@Import({Lei.class, Color.class})public class BeanConfig3 { //@Conditional({WindowCondition.class}) @Bean public Person bill() { return new Person("bill gates",6...原创 2019-09-14 21:21:05 · 128 阅读 · 0 评论 -
09、组件注册-@Import-使用ImportSelector
/** * MyImportSelector装配组件 * * @author LeiLei * @date 2019/9/14 */public class MyImportSelector implements ImportSelector { /** * * @param importingClassMetadata 可获取@Import注解标注类...原创 2019-09-14 21:45:07 · 152 阅读 · 0 评论 -
10、组件注册-@Import-使用ImportBeanDefinitionRegistrar
@Import({Lei.class, Color.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})package com.lei.study_09_14.importSelector;import org.springframework.beans.factory.config.B...原创 2019-09-14 22:22:42 · 196 阅读 · 0 评论 -
11、组件注册-使用FactoryBean注册组件
package com.lei.study_09_14.importSelector;import com.lei.study_09_14.bean.Color;import org.springframework.beans.factory.FactoryBean;/** * 工厂bean * * @author LeiLei * @date 2019/9/14 */p...原创 2019-09-14 22:46:55 · 153 阅读 · 0 评论 -
12、生命周期-@Bean指定初始化和销毁方法
package com.lei.study_09_15.bean;/** * * * @author LeiLei * @date 2019/9/15 */public class Person { private String name; private int age; public void init() { System....原创 2019-09-15 14:32:49 · 144 阅读 · 0 评论 -
13、生命周期-InitializingBean和DisposableBean
package com.lei.study_09_15.bean;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;/** * * * @author LeiLei * @date 2019/9/15...原创 2019-09-15 14:43:46 · 121 阅读 · 0 评论 -
27、AOP-AOP功能测试
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version></dependency...原创 2019-09-19 23:27:37 · 199 阅读 · 0 评论