02点睛Spring4.1-Java Config
转载:https://www.iteye.com/blog/wiselyman-2210376
2.1 java config
- spring的java config主要使用@Configuration和@Bean两个注解;
- 使用@Configuration注解在类上声明是一个配置类(相当于一个spring的配置xml);
- 使用@Bean注解在方法上,返回值是一个类的实例,并声明这个返回值是spring的一个bean,bean的name是方法名;
2.2 关于@Bean和@Component,@Service,@Repository,@Controller
@Component,@Service,@Repository,@Controller注解在一个类上之后,这个类也成为spring容器中的bean,使用@Bean注解也是,感觉使用@Bean注解是不是更麻烦呢?既然效果是等同的,那什么时候使用@Bean什么时候使用
@Component,@Service,@Repository,@Controller系列呢?这个原则就和我们当初混用xml配置和
@Component,@Service,@Repository,@Controller时候一样:系统的全局配置(数据库配置,spring mvc配置,spring security配置等)使用java config(xml),业务相关的bean使用@Component,@Service,@Repository,@Controller系列。在后面我们讲到一些全局配置的时候我们就会使用Spring的java config
2.3 演示
2.3.1 创建一个properties(test.properties)文件作为配置
wisely.word = World
2.3.2 创建一个java class
package com.wisely.javaconfig;
public class DemoService {
private String word;
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String sayHello(){
return "Hello "+this.word;
}
}
2.3.3 创建java config配置类
package com.wisely.javaconfig; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment; @Configuration //声明是一个配置类
@PropertySource("com/wisely/javaconfig/test.properties")
public class DemoConfig { @Bean //声明是一个bean
public DemoService demoBean(Environment environment){
DemoService demoService = new DemoService();
demoService.setWord(environment.getProperty("wisely.word"));
return demoService;
}
}
2.3.4 测试-初始化spring容器
package com.wisely.javaconfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
//设定此包下的类被注册成spring的bean,
//包含@Configuration,@Component,@Service,@Repository,@Controller
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.javaconfig");
DemoService demoService = context.getBean(DemoService.class);
System.out.println(demoService.sayHello());
context.close();
}
}
输出结果:Hello World
02点睛Spring4.1-Java Config的更多相关文章
- 04点睛Spring4.1-资源调用
转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...
- 18点睛Spring4.1-Meta Annotation
18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...
- Spring Security Java Config Preview--官方
原文地址:[1]https://spring.io/blog/2013/07/02/spring-security-java-config-preview-introduction/ [2]https ...
- 14点睛Spring4.1-脚本编程
转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...
- 00点睛Spring4.1-环境搭建
转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...
- 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.测试验证 三.总结 ...
随机推荐
- VSCompile
VS2012加载失败 No exports were found that match the constraint 开始->运行->devenv.exe /resetuserdata-& ...
- editplus 支持lua语言语法高亮显示
找到自己的安装目录 建一个这个名字的文件 里面写上 #TITLE=LUA ; LUA syntax file written by ES-Computing. ; This file is requi ...
- 飞扬的小鸟 DP
飞扬的小鸟 DP 细节有点恶心的DP,设\(f[i][j]\)表示横坐标为\(i\)(从\(0\)开始)高度为\(j\)时,屏幕点击的最小次数为\(f[i][j]\),转移便很好写了,这里要注意枚举当 ...
- Codevs 1070 普通递归关系(矩阵乘法)
1070 普通递归关系 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 考虑以下定义在非负整数n上的递归关系 f(n) = f0 ...
- 2017.10.6 国庆清北 D6T3 字符串
题目描述 如果把一个字符串从头到尾翻转后和原字符串相等,我们称之为回文串,比如“aabaa”.“())(”.“2017102”. 如果一个字符串存在两个出现过的字母出现的次数相等,我们称之为好 的字符 ...
- Django系列目录
一:搭建自己的博客系列 搭建自己的博客(一):前期准备 搭建自己的博客(二):创建表,创建超级用户 搭建自己的博客(三):简单搭建首页和详情页 搭建自己的博客(四):优化首页和详情页 搭建自己的 ...
- 【golang】使用rpcx不指定tags报错 undefined: serverplugin.ConsulRegisterPlugin
为了避免引入不必要的库, rpcx采用了 Go 条件编译 的特性, 你可以只引入必要的特性. 比如你只使用 etcd 作为注册中心的时候, 你不希望引入 consul.zookeeper相关的库,你需 ...
- 网格布局 grid
推荐阅读:http://www.ruanyifeng.com/blog/2019/03/grid-layout-tutorial.html 1. 启动网格布局 div { display: grid; ...
- MySQL 常用字段类型与对应的Java类型
varchar 不定长字符串 字符串或是没有合适类型时,可以选择它作为字段类型 对应Java中的String int bigint 数值 一般以int作为数字的默认选择,数值很大时使用bigint 对 ...
- eclipse 创建c/c++ 工程
新建 注意选择如下选项,c和c++ 都一样的 然后,编译运行 参考: https://blog.csdn.net/u013610133/article/details/72857870 https:/ ...