Spring 容器介绍
Spring 框架的实现依赖 IoC (反向控制) 原则,更为形象的称呼是 DI (依赖注入)。相对于 Bean 直接构造依赖的对象,Spring 框架则根据 Bean之间的依赖关系创建对象,并注入到 Bean 中。
public UserServiceImpl implements UserService {
private CreditUserService creditUserService = new CreditUserService();
public void order(...) {
...
creditUserService.addCredit(4);
}
}
对于上述代码,creditUserService 实例是在 UserService 对象中创建的,创建方式比较简单,调用了构造函数。如果 creditUserService 本身构造方式比较复杂,而且是个单例对象,那么会使用工厂类来实现进一步优化。
private CreditUserService creditUserService = CreditUserServiceFactory.getService();
在 Spring 框架中,如果你的 Bean 是通过 Spring 来管理的,Spring 容器则会帮你完成这些事情,开发人员需要做的仅仅是声明依赖关系。
@Service
public CrediUserServiceImpl implements CreditUserService {
public void addCredit(int score) {
}
}
@Service
public UserService {
@Autowired
private CreditUserService creditUserService;
public void order(...) {
...
creditUserService.addCredit(4);
}
}
Bean 通过注解 @Service 声明为一个 Spring 管理的 Bean, Spring 容器会扫名 classpath 下的所有类,找到带有 @Service 注解的 UserService 类,并根据 Spring 注解对其进行初始化和增强,如果发现此类属性 creditUserService 也有 @Autowired,则会从 Spring 容器里找一个已经初始化好的 CreditUserService ,如果没有,则先初始化。
如何成为一个 Spring 容器管理的 Bean ? 在 Spring Boot 中依靠注解,如在类上的注解 @Controller, @Service, @Configuration 等,方法上的注解 @Bean。
Spring 常用注解
Spring 提供了多个注解声明 Bean 为 Spring 管理的 Bean,注解不同代表的含义不一样,但对于 Spring 容器来说,都是 Spring 管理的 Bean。
- Controller: 声明此类是一个 MVC 类, 通常与 @RequestMapping 一起使用。
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/get/{id}")
public String getUser(@PathVariable String id) {
}
}
- Service: 声明此类是一个业务处理类,通常与
@Transactional一起配合使用
@Service
@Transactional
public Class UserServiceImpl implements UserService {
public void order(...) {
}
}
- Repository: 声明此类是一个数据库或者其他NoSQL访问类。
@Repository
public class UserDao implements CrudDao<User,String> {
}
- RestController 同Controller 用于REST服务
- Component 声明此类是一个 Spring 管理的类,通常用于无法用上述注解描述的Spring管理类
- Configuration 声明此类是一个配置类,通常与注解
@Bean配合使用。
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
public DataSource datasource(Environment env) {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
ds.setUsername(env.getProperty("spring.datasource.username"));
ds.setPassword(env.getProperty("spring.datasource.password"));
return ds;
}
}
如上,DataSourceConfig 是一个 Spring 容器配置类,配置了 HikariDataSource
Spring 容器介绍的更多相关文章
- Spring容器技术内幕之BeanWrapper类介绍
引言 org.springframework.beans.BeanWrapper是Spring框架中重要的组件类.BeanWrapper相当于一个代理器,Spring委托BeanWrapperwanc ...
- Spring容器技术内幕之BeanDefinition类介绍
引言 org.springframework.beans.factory.config.BeanDefinition是配置文件< bean >元素标签在容器中地内部表示.< bean ...
- Spring容器的refresh()介绍
Spring容器的refresh()[创建刷新]; 1.prepareRefresh()刷新前的预处理; 1).initPropertySources()初始化一些属性设置;子类自定义个性化的属性设置 ...
- [翻译]Spring框架参考文档(V4.3.3)-第二章Spring框架介绍 2.1 2.2 翻译--2.3待继续
英文链接:http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/overview.ht ...
- Spring 读书笔记-----使用Spring容器(一)
pring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spri ...
- Spring读书笔记-----使用Spring容器(二)
一.使用ApplicationContext 前面介绍了,我们一般不会使用BeanFactory实例作为Spring容器,而是使用ApplicationContext实例作为容器,它增强了BeanFa ...
- 在listener或者工具中使用spring容器中的bean实例
在项目中经常遇见需要在Listener中或者工具中使用Spring容器中的bean实例,由于bean不能在stataic的类中使用. 介绍一种方式: public class SpringTool { ...
- Spring框架:Spring容器具体解释
Spring容器 Spring容器能够帮助你管理所有的Bean对象.专业术语称之为IoC控制反转.在传统的程序中.对象的生成都是由开发人员完毕的.而在控制反转中,对象的生成所有都交给框架完毕.这种优点 ...
- (转)Spring读书笔记-----使用Spring容器(二)
一.使用ApplicationContext 前面介绍了,我们一般不会使用BeanFactory实例作为Spring容器,而是使用ApplicationContext实例作为容器,它增强了BeanFa ...
随机推荐
- For update带来的思考
For update or not 起源 之所以想写这个专题,是因为最近在做一个抢占任务的实现.假设数据库很多个任务,在抢占发生之前任务的状态都是FREE.现在假设同时有一堆抢占线程开始工作,抢占 ...
- 问题集录--Java高级软件工程师面试考纲(转)
如果要应聘高级开发工程师职务,仅仅懂得Java的基础知识是远远不够的,还必须懂得常用数据结构.算法.网络.操作系统等知识.因此本文不会讲解具体的技术,笔者综合自己应聘各大公司的经历,整理了一份大公司对 ...
- LinqProvider系列(三)如何实现自己的Linq Provider?
这篇文章将在前人的肩上,继续完成实现Linq Provider的任务. 首先,我们列出linq语法的解析过程: linq本质上就是把我们惯用的语法糖,变成了一颗表达式树,然后由不同的linq Prov ...
- @Value失效的问题
@Value 会在@Controller中失效,失效原因涉及源码问题就不一一叙述了,一般加上@Service,@Component就能解决.如果是在Controller中使用建议新建一个配置类,然后在 ...
- 为什么不要 "lock(this)" ? lock object 并是readonly(转载)
一. 为什么要lock,lock了什么? 当我们使用线程的时候,效率最高的方式当然是异步,即各个线程同时运行,其间不相互依赖和等待.但当不同的线程都需要访问某个资源的时候,就需要同步机制了,也就是 ...
- python之协程gevent模块
Gevent官网文档地址:http://www.gevent.org/contents.html 进程.线程.协程区分 我们通常所说的协程Coroutine其实是corporate routine的缩 ...
- 小tip: 某简单的字符重叠与图形生成----张鑫旭
引言 字符重叠不是什么稀奇的东西. 如1像素错位模拟阴影效果: 或者powerFloat中展示的带边框三角: 以及其他很多. 但是技术这东西不是豆腐,老了可以吃,臭了也可以吃:那我这里还拿着个说事作甚 ...
- Code Signal_练习题_reverseParentheses
You have a string s that consists of English letters, punctuation marks, whitespace characters, and ...
- CSS-带尖角的对话框
效果图: box1的代码: .box{ position: relative; width: 200px; height: 200px; border: 2px solid #000; backgro ...
- 2017年值得学习的3个CSS特性
原文:https://bitsofco.de/3-new-css-features-to-learn-in-2017/译文:http://caibaojian.com/3-new-css-featur ...