随笔分类 -  Spring

摘要:Spring 还支持使用@Resource(JSR250)和@Inject(JSR330)[Java规范的注解] AutowiredAnnotationBeanPostProcessor (后置处理器)完成解析自动装配功能 @Resource 可以和@Autowired一样实现自动注入功能,默认是按 阅读全文
posted @ 2020-03-09 16:34 Arbitrary233 阅读(236) 评论(0) 推荐(0) 编辑
摘要:自动装配-@Autowired&@Qualifier&@Primary 自动装配:Spring 利用依赖注入(DI),完成对IOC容器中各个依赖关系赋值 @Autowired :自动注入 默认优先按照类型去容器中找对应的组件,applicationContext.getBean(BookReposi 阅读全文
posted @ 2020-03-09 16:21 Arbitrary233 阅读(271) 评论(0) 推荐(0) 编辑
摘要:1、创建person类 public class Person { /* * 使用@Value赋值: * 1、基本数值 * 2、可以写SpEl;#{} * 3、可以写${}、取出配置文件中(例如properties文件)的值(运行环境变量里面的值) */ @Value("张三") private S 阅读全文
posted @ 2020-03-09 15:55 Arbitrary233 阅读(479) 评论(0) 推荐(0) 编辑
摘要:1、创建Person类 public class Person { private String name; private Integer age; public Person() { super(); } public Person(String name, Integer age) { sup 阅读全文
posted @ 2020-03-08 15:03 Arbitrary233 阅读(867) 评论(0) 推荐(0) 编辑
摘要:1、创建Bird类并注入容器中 @Component public class Bird { public Bird() { System.out.println("Bird的构造方法执行了"); } //对象构造并属性赋值后执行 @PostConstruct public void PostCon 阅读全文
posted @ 2020-03-08 11:54 Arbitrary233 阅读(295) 评论(0) 推荐(0) 编辑
摘要:1、创建Bird类 @Component public class Bird { public Bird() { System.out.println("Bird的构造方法执行了"); } //对象构造并属性赋值后执行 @PostConstruct public void PostConstruct 阅读全文
posted @ 2020-03-08 11:23 Arbitrary233 阅读(182) 评论(0) 推荐(0) 编辑
摘要:1、创建Tiger类实现InitializingBean,DisposableBean接口,并通过@Component将该组件注入 @Component public class Tiger implements InitializingBean,DisposableBean{ public Tig 阅读全文
posted @ 2020-03-08 11:06 Arbitrary233 阅读(340) 评论(0) 推荐(0) 编辑
摘要:构造(对象创建) * 单实例:在每次容器启动的时候创建对象 * 多实例:在每次获取的时候创建对象初始化: 对象创建完成,并赋值好,调用初始化方法 销毁: 单实例:容器关闭的时候 多实例:容器不会不会管理这个bean的销毁方法 1、创建一个Dog类 public class Dog { public 阅读全文
posted @ 2020-03-08 10:41 Arbitrary233 阅读(2613) 评论(0) 推荐(1) 编辑
摘要:1、创建一个Color类 2、再创建自定义工厂类(实现FactoryBean接口) //创建一个spring定义的FactoryBean public class ColorFactoryBean implements FactoryBean<Color> { //返回一个Color对象,这个对象会 阅读全文
posted @ 2020-03-07 23:03 Arbitrary233 阅读(621) 评论(0) 推荐(0) 编辑
摘要:一、@Import方式一:直接导入 1、创建两个类测试 2、在spring全局配置类中进行配置(@Import可导一个和多个组件,导入后组件的id为全类名) //配置类==配置文件 @Configuration //告诉Spring这是一个配置类 @Import({Color.class,Anima 阅读全文
posted @ 2020-03-07 22:05 Arbitrary233 阅读(219) 评论(0) 推荐(0) 编辑
摘要:@Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean。 @Conditional的定义: //此注解可以标注在类和方法上 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention 阅读全文
posted @ 2020-03-07 20:56 Arbitrary233 阅读(285) 评论(0) 推荐(0) 编辑
摘要:1、spring主配置类注册一个Person组件 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.s 阅读全文
posted @ 2020-03-07 16:58 Arbitrary233 阅读(382) 评论(0) 推荐(1) 编辑
摘要:1、分别创建一个Person类以及controller、service、dao左测试 controller、service、dao分别加上注解 2、在主配置类中使用@ComponentScan指定扫描规则 import org.springframework.context.annotation.B 阅读全文
posted @ 2020-03-07 16:21 Arbitrary233 阅读(575) 评论(0) 推荐(1) 编辑
摘要:1、创建一个maven工程 2、创建一个Person类 3、导入spring-context依赖 导入依赖后: 一:配置文件注册一个Bean的方式: 1、创建一个全局spring配置文件 2、在配置文件中注册一个Person <?xml version="1.0" encoding="UTF-8"? 阅读全文
posted @ 2020-03-07 14:59 Arbitrary233 阅读(203) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示