spring java config 初探
Java Config 注解
spring java config作为同xml配置形式的另一种表达形式,使用的场景越来越多,在新版本的spring boot中
大量使用,今天我们来看下用到的主要注解有哪些。
- Configuration: 继承Component注解,默认解析为spring的顶级BeanFactory。作用于类上面,等价于applicationContext.xml文件
- Bean :作用在方法上面,方法的返回值为类实例。等同于元素,但是没有scope等元素,猜测可能是此处由代码控制scope。
- ComponentScan:类级注解,用于配置扫描的包路径。等同于context:component-scan/元素。主要使用basePackages元素。
接下来看一个实例
//注释声明容器
@Configuration
public class HelloWorldConfig {
//配置一个id=helloBean的bean,并返回对应的实例
@Bean(name = "helloBean")
public HelloWorld helloWorld() {
return new HelloWorldImpl();
}
}
这个写法等同于
<bean id ="helloBean" class="cn.lee.jason.hello.impl.HelloWorldImpl"></bean>
配置结束了,使用方面基本没有大的差异。
JavaConfig方式,使用AnnotationConfigApplicationContext类加载JavaConfig配置信息
class TestHelloConfig{
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
}
如果需要注册加载多个BeanFactory的时候
class TestHelloConfig{
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class);
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
}
在某种程度上面,java config简化了xml配置,但也导致修改场景丢失了一定的灵活性。面对快速搭建测试环境时比较方面快捷。
spring java config 初探的更多相关文章
- 你真的懂Spring Java Config 吗?Full @Configuration vs lite @Bean mode
Full @Configuration和lite @Bean mode 是 Spring Java Config 中两个非常有意思的概念. 先来看一下官方文档关于这两者的相关内容: The @Bean ...
- spring java config配置搭建工程资料收集(网文)
https://blog.csdn.net/poorcoder_/article/details/70231779 https://github.com/lovelyCoder/springsecur ...
- Spring注解@Configuration和Java Config
1.从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中.但是,仍然允许使用经典的XML方式来定义bea ...
- Spring 4 and MyBatis Java Config
TL;DR With the Java Config enhancements in Spring 4, you no longer need xml to configure MyBatis for ...
- Spring Security4实例(Java config版)——ajax登录,自定义验证
本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...
- Spring Security4实例(Java config 版) —— Remember-Me
本文源码请看这里 相关文章: Spring Security4实例(Java config版)--ajax登录,自定义验证 Spring Security提供了两种remember-me的实现,一种是 ...
- Spring知识点回顾(01)Java Config
Spring知识点回顾(01) 一.Java Config 1.服务和服务注入 2.Java 注解 :功能更强一些 3.测试验证 二.注解注入 1.服务和服务注入 2.配置加载 3.测试验证 三.总结 ...
- Spring Web工程web.xml零配置即使用Java Config + Annotation
摘要: 在Spring 3.0之前,我们工程中常用Bean都是通过XML形式的文件注解的,少了还可以,但是数量多,关系复杂到后期就很难维护了,所以在3.x之后Spring官方推荐使用Java Conf ...
- Spring MVC 的 Java Config ( 非 XML ) 配置方式
索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml web/pom.xml web.xml WebInitializer.java ...
随机推荐
- IOS开发数据存储篇—IOS中的几种数据存储方式
IOS开发数据存储篇—IOS中的几种数据存储方式 发表于2016/4/5 21:02:09 421人阅读 分类: 数据存储 在项目开发当中,我们经常会对一些数据进行本地缓存处理.离线缓存的数据一般都 ...
- Mybatis ,框架
什么是mybatis MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis使用简单的XML ...
- 关于content-type请求头的说明
Content-Type请求头的作用,用于标记请求体数据的格式,如: 1. Content-Type:application/x-www-form-urlencoded 请求体:b'pwd=123&a ...
- 内网最小化安装CentOS后,想安装ISO文件中的包怎么办呢?
昨日公司测试人员需要升级公司服务器Python,发现公司服务器上缺失了各种各样的包.比如open-ssl,python-deve等 1.查看你的Centos版本 lsb_release -a 2.上传 ...
- TCP/IP初识(一)
TCP/IP学习记录,如有错误请指正,谢谢!!! 什么是TCP/IP协议? TCP/IP协议族分为四层(另一个名字是Internet协议族(Internet Protocol Suite)):链路层. ...
- HDU1398 Square Coins(生成函数)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- YouCompleteMe
需要配套的.vimrc :sw:.h与.cpp切换 Issue:YouCompleteMe unavailable no module named future cd .vim/Vundle/YouC ...
- Linux下安装 nginx
安装依赖 yum install gcc yum install pcre-devel yum install zlib zlib-devel yum install openssl openssl- ...
- MySQL---视图、触发器
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( S ...
- react组件间传值详解
一.父子组件间传值 <1>父传子 父组件: