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-
@Inject: JSR-
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 { @Resource
private 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 { @Bean
public FunctionService functionService() {
return new FunctionService();
} /**
* spring容器中有的bean, 可以直接作为参数传入
* @param functionService
* @return
*/
@Bean
public 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/
springboot-2-ioc的更多相关文章
- SpringBoot框架——从SpringBoot看IoC容器初始化流程之方法分析
目录 一.概观Spring Boot 二.Spring Boot应用初始化 2.1 初始化入口 2.2 SpringApplication的run方法 2.3 方法分析 三.容器创建与初始化 3.1 ...
- 数据库连接不上的原因以及springBoot的ioc无法自动注入
无法自动注入解决了,数据池的连接还有问题: 错误原因1: :数据库用的是Mysql8版本,以前的配置mysql驱动包却是5.1.37版本.只需修改驱动包为8.0.11版本即可. <!-- mys ...
- springboot ---> spring ioc 注册流程 源码解析 this.prepareContext 部分
现在都是在springboot 中 集成 spirng,那我们就从springboot 开始. 一:springboot 启动main 函数 public static void main(Strin ...
- 手把手教你调试SpringBoot启动 IoC容器初始化源码,spring如何解决循环依赖
授人以鱼不如授人以渔,首先声明这篇文章并没有过多的总结和结论,主要内容是教大家如何一步一步自己手动debug调试源码,然后总结spring如何解决的循环依赖,最后,操作很简单,有手就行. 本次调试 是 ...
- 【归纳】springboot中的IOC注解:注册bean和使用bean
目前了解的springboot中IOC注解主要分为两类: 1. 注册bean:@Component和@Repository.@Service.@Controller .@Configuration 共 ...
- 尚硅谷springboot学习26-嵌入式servlet容器自动配置、启动原理
EmbeddedServletContainerAutoConfiguration:嵌入式的Servlet容器自动配置 @AutoConfigureOrder(Ordered.HIGHEST_PREC ...
- springboot超详细笔记
一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...
- springboot(八) 嵌入式Servlet容器自动配置原理和容器启动原理
1.嵌入式Servlet容器自动配置原理 1.1 在spring-boot-autoconfigure-1.5.9.RELEASE.jar => springboot自动配置依赖 jar包下,E ...
- springboot简单入门笔记
一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...
- SpringBoot系列之集成jsp模板引擎
目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...
随机推荐
- The J-Link hardware debugging Eclipse plug-in
Quicklinks If you already know what are the features of the new plug-in and just want to know how to ...
- java向数据库插入N条数据
为了测试mysql的索引,要向数据库先插入上万条数据,然后再测试.手动插入太麻烦,写了一段代码. 先上代码: package action; import java.sql.Connection; i ...
- 各位客官!鼠标点击一个Button之后究竟发生了什么?您知道么?(C#)
在谈论主题之前,让我们先简单回顾下事件的基础知识吧! 我们知道事件有发出(raises)事件的源,即event sender,也有接收事件通知(notifications)的接收者,即event re ...
- WPF 使用OCX控件速度很慢
最近公司项目,需要在wpf上面嵌入ocx控件,但是程序运行起来后,进行操作后,界面一直很卡,找了各种原因,没有找到原因,后来直接运行exe文件,速度顿时快了很多.
- asp.net web 应用站点支持域账户登录
1.IIS站点应用程序池设置管道模式为classic模式,identity设置为管理员账户 2.站点验证设置,只打开windows验证,其他都关闭 3.应用程序配置web.config配置如下: &l ...
- sql trace script
CREATE EVENT SESSION [sql_query_tracing] ON SERVER ADD EVENT sqlos.wait_info( ACTION(sqlos.schedu ...
- Tomcat绿色版启动"startup.bat"一闪问题的解决方法!
进入DOS窗口,运行"startup.bat",会出现错误提示,我是win7 64位,提示“JRE_HOME”设置不正确.于是进入环境变量配置,设置“JRE_HOME”项,随后保存 ...
- kvm虚拟化存储池配置
1.创建基于文件夹的存储池(目录) 2.定义存储池与其目录 # virsh pool-define-as vmdisk --type dir --target /data/vmfs 3.创建已定义的存 ...
- 【文文殿下】洛谷P2408 不同子串个数
题目链接https://www.luogu.org/problemnew/show/P2408 SAM裸题,大力求就行了 #include<cstdio> #include<cstr ...
- ZJOI2019 游记
Day -2 入住酒店,跟 Sooke 一个房间 酒店还是很好的呢 然后就是颓废 Sooke 和 zxy,ljx,lyt,xx 一起打 lol,我孤独的打着坦克和 MC 然后复习了一下板子 Day - ...