springboot-2-ioc
在spring环境下, ioc(控制反转 和 DI (依赖注入) 是等效的, 主要体现一种组合的松耦合思想. spring Ioc容器负责创建Bean, 并将Bean注入到所需的Bean中, 有xml, 注解, java配置, groovy配置等实现
声明bean的注解有:
@Component, 没有角色 @Controller, 为mvc的展现层 @RestController, springboot中使用, 相当于 @Controller和@ResponseBody @Service, 为Service层 @Repositoy, 为数据交互层
使用bean的注解有:
@Autowired @Resource: JSR-250 @Inject: JSR-330
1, 使用注解声明的bean的使用
这种方式比较简单, 加个注解, spring就会根据aop注解去判断并加载到context中
TestService
package com.wenbronk.config.annotation;import org.springframework.stereotype.Service;/*** Created by wenbronk on 2017/5/12.*/ @Service public class TestService {public String sayHello(String hello) {return "Hello" + hello;}}
TestController
package com.wenbronk.config.annotation;import org.springframework.stereotype.Service;import javax.annotation.Resource;/*** Created by wenbronk on 2017/5/12.*/ @Service public class TestController {@Resourceprivate TestService testService { public String sayHello(String word) {return testService.sayHello(word);}}
Config配置, 使用@SpringBootConfiguration配置, 声明当前类为一个配置类
package com.wenbronk.config.annotation;import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.ComponentScan;/*** Created by wenbronk on 2017/5/12.*/ @SpringBootConfiguration @ComponentScan(value = {"com.wenbronk.config.annotation"}) public class AnnotationConfig {}
Test
package com.wenbronk.config.annotation;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** Created by wenbronk on 2017/5/12.*/public class TestAnnotation {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfig.class);TestController user = context.getBean(TestController.class);String sayHello = user.sayHello("你好");System.out.println(sayHello);} }
2, 使用java进行配置
关键在于config配置, 要保证使用的每个bean在config类下都有@Bean加入到context中
1), TestService, TestController,
和上面的一样, 去掉 @Service就好...
2), javaconfig, 使用@Bean标签注入到context中
package com.wenbronk.config.java;import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean;/*** 除了使用上面那种方法, 还可以直接new* Created by wenbronk on 2017/5/12.*/ @SpringBootConfiguration public class JavaConfig2 {@Beanpublic FunctionService functionService() {return new FunctionService();} /*** spring容器中有的bean, 可以直接作为参数传入* @param functionService* @return*/@Beanpublic UserFunctionService userFunctionService(FunctionService functionService) {UserFunctionService userFunctionService = new UserFunctionService();userFunctionService.setFunctionService(functionService);return userFunctionService;}}
spring容器提供一个好处, 在context中管理的bean, 可通过形参的形式直接注入
3), Test
package com.wenbronk.config.java;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** Created by root on 2017/5/12.*/ public class TestJava {public static void main(String[] args ) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig2.class);TestController bean = context.getBean(TestController.class);String hello = bean.sayHello("hello");System.out.println(hello);} }
http://www.cnblogs.com/wenbronk/
转载于:https://www.cnblogs.com/wenbronk/p/6848976.html
springboot-2-ioc相关推荐
- springboot的IOC依赖注入与控制反转-举例(转载+自己整理)
这个记录是对[1]的整理和简化,便于日后遗忘时迅速恢复记忆. Spring框架有四大原则: ①使用POJO进行轻量级与最小侵入式开发 ②通过依赖注入和基于接口编程实现松耦合 ③通过AOP和默认习惯进行 ...
- SpringBoot交给IOC管理对象的几种方式
文章目录 @Component @Bean @Import @Import搭配ImportSelector @Import搭配ImportBeanDefinitionRegistrar BeanFac ...
- SpringBoot的IOC和AOP
1. IOC 控制反转(IOC)和依赖注入(DI)是同一个概念,目的在于降低系统耦合,将类的实例化工作交给Spring代理,主要用到的设计模式为工厂模式,通过Java反射机制实现类的自动注入. IoC ...
- SpringBoot之IOC
IOC(控制反转):就是程序员不用管new对象的事了,将new对象的任务交给框架来实现. 实现控制反转的方法有两种 1.配置文件管理,bean 2.注解 一.配置文件管理 1.选择resource下面 ...
- springboot分析ioc原理
1.读取bean配置信息 2.实例化bean 3.读取bean配置 信息 4.应用程序使用 hashmap缓存一份bean的信息,快速找到 2.springmvc 1.clienthttp请求过来通过 ...
- SpringBoot之配置嵌入式Servlet容器
1.概述 文章目录 1.概述 2.如何修改SpringBoot的默认配置 3.定制和修改Servlet容器的相关配置 4.注册Servlet三大组件 5.替换为其他嵌入式Servlet容器 6.嵌入式 ...
- 后端学习 - SpringBoot
SpringBoot 是整合 Spring 技术栈的一站式框架,是简化 Spring 技术栈的快速开发脚手架 约定大于配置 文章目录 一 概述 1 第一个 SpringBoot 项目 2 Spring ...
- SpringBoot 就这一篇全搞定
一.Hello Spring Boot 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 微服务 ...
- 从头撸到脚,SpringBoot 就一篇全搞定!
一.Hello Spring Boot 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 微服务 ...
- springboot系列课程笔记-第四章-WEB开发
四.Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可 ...
最新文章
- 人工神经网络背后的数学原理!
- Oracle数据库名与Oracle实例名的关系
- 【机器学习】线性回归之Normal Equation(矩阵求导与线性代数视角)
- UVALive 6511 Term Project
- 泰坦尼克号生存预测入门
- 使用base64编码把背景添加到CSS文件中
- Pandas 生成随机整数序列
- 贝叶斯网络模型具体作用
- 一篇文章让你拥有用不完的ip代理
- 【人工智能】人工智能是20世纪50年代中期兴起的一门新兴边缘科学
- [转载]使用 Abbot 框架自动化测试 Eclipse 插件的用户界面,第 1 部分
- AGV项目底层总结二
- adobe photoshop cs6 安装
- win32gui恢复小化窗口,前置窗口
- STL ++iter与iter++区别
- python 成语接龙1-爬去四字成语
- 十分钟教你写个软件防火墙!powershell。
- 9款用HTML5/CSS3制作的动物、人物动画
- Discuz被挂马的处理经验,Dz为什么会被挂马
- mysql usleep_PHP 暂停函数 sleep() 与 usleep() 的区别