Spring IOC 常用注解与使用
@Component
注解@component代表spring ioc 会把这个类扫描生成Bean实例
@Component
public class Role{
@Value("1")
private Long id;
@Value("role_name_1")
private String roleName;
@Value("role_note_1")
private String note;
/***setter and getter****/
}
@Autowired
注解@Autowired代表在spring ioc 定位所有的Bean后,这个字段需要按类型来进行注入。
@Component
public class RoleImpl_1 implements RoleServer{
@Autowired
private Role role = null;
public .....
}
@Qualifier
如果一个接口被两次实现,则使用@Autowired注解来进行该接口注入会产生异常,因为@Autowired无法确定要使用的是哪一个实现类。可以使用@Qualifier注解来进行歧义消除。
@Component
public class RoleController{
@Autowired
@Qualifier("roleImple_2")
private RoleServer server = null;
public .....
}
@Bean
在注解都都是通过@component来进行装配Bean,但是@Component只能注解在类上,无法注解到方法上。而注解@Bean可以注解到方法上
@Bean(name = "dataSource")
public DataSource getDataSource(){
Properties props = new Properties();
props.setProperty("driver","com.mysql.cj.jdbc.Driver");
props.setProperty("url","jdbc:mysql://localhost:3306/db");
...
try{
dataSource = BasicDataSourceFactory.createDataSource(props);
}catch(Execption e){
e.printStackTrace();
}
return dataSource;
}
@Component
public class RoleController{
@Autowired(name = "dataSource")
private DataSource dataSource = null;
public .....
}
@ImportResource
如果我们将DataSource使用xml配置文件来进行配置,我们就无法使用注解@Bean来进行装配。这时注解@ImportResource可以进行混合装配(将第三方的xml引入进来进行装配)。
<!--dbSource.xml-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db"/>
.......
</bean>
@ComponentScan(basePackages={"com.test"})
@ImportResource({"classpath:dbSource.xml"}) //将dbSource.xml配置文件装配到Ioc中来
public class ApplicationConfig{
}
@Component
public class RoleController{
@Autowired
private DataSource dataSource = null;
public .....
}
如果有多个xml文件,我们都想引用进来,可以在dbSource.xml配置文件中使用import元素来加载它
<!--spring-dataSource.xml-->
...........
<!--dbSource.xml-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db"/>
.......
</bean>
<import resourse="spring-dataSource.xml"/>
@Profile
可以解决不同环境的切换需求,例如开发环境和测试环境不同,我们来看代码操作。
@Component
public class ProfileDataSource{
//开发环境
@Bean(name = "devDataSource")
@Profile("dev")
public DataSource getDevDataSource(){
......
}
//测试环境
@Bean(name = "testDataSource")
@Profile("test")
public DataSource getTestDataSource(){
......
}
}
当启动Java配置Profile时,可以发现两个Bean并不会加载到IOC容器中,需要自行激活Profie。我们可以使用JVM启动目录或在集成测试环境中使用@ActiveProfiles进行定义
//使用@ActiveProfiles激活Profie
@RunWith(SpringJunit4ClassRunner.class)
@ContextConfiguration(classes=ProfileTest.class)
@ActiveProfiles("dev") //激活开发环境的Profile
public class ProfileTest{
}
//使用JVM参数激活Profie
JAVA_OPTS="-Dspring.profiles.active=test"
@PropertySource
可以使用注解@PropertySource来加载属性文件(properties)。
# dataSource.properties
jdbc.database.driver=com.mysql.cj.jdbc.Driver
jdbc.database.url=jdbc:mysql://localhost:3306/db
.......
@Configuration
@PropertySource(value = {"classpath:dataSource.properties"},{ignoreResourceNotFound=true})
public class ApplicationConfig{
}
ignoreResourceNotFound=true,如果找不到该属性文件则忽略它。
Spring IOC 常用注解与使用的更多相关文章
- Spring Ioc 常用注解
在开发中spring ioc的配置方式有多种方式,常用的一般都是byName.byType .以及其自动装配可以查看http://www.cnblogs.com/gubai/p/9140840.htm ...
- Spring MVC学习总结(2)——Spring MVC常用注解说明
使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能. 02 之前在使用Struts2实现MVC的注解时,是借助struts2-convention这个插件,如今我们使 ...
- Spring学习总结(2)——Spring的常用注解
本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). ? 1 <context:compon ...
- Spring Boot 常用注解汇总
一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...
- Spring Boot常用注解总结
Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...
- 接近8000字的Spring/SpringBoot常用注解总结!安排!
0.前言 大家好,我是 Guide 哥!这是我的 221 篇优质原创文章.如需转载,请在文首注明地址,蟹蟹! 本文已经收录进我的 75K Star 的 Java 开源项目 JavaGuide:http ...
- Spring/SpringBoot常用注解总结
转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...
- Spring IoC 公共注解详解
前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 什么是公共注解?公共注解就是常见的Java ...
- Spring IOC 常用的注解
一.@Bean 1.配置类 @Configuration public class MainConfig { @Bean public Person person(){ return new Pers ...
随机推荐
- echarts中饼图的legend自定义icon图片(扇形为例)
效果图: 代码: 问题:// icon: "pin", // 这个字段控制形状 类型包括 circle,rect ,roundRect,triangle,diamond,pin,a ...
- 为什么HashMap使用红黑树而不使用AVL树
为什么HashMap使用红黑树而不使用AVL树? 红黑树适用于大量插入和删除:因为它是非严格的平衡树:只要从根节点到叶子节点的最长路径不超过最短路径的2倍,就不用进行平衡调节 AVL 树是严格的平衡树 ...
- Vue报错Cannot read property 'split' of undefined
今天在项目中处理后端返回的字符串需要使用split做一个字符串转数组的处理,之前项目都运行得好好的,今天突然出问题了,然后面向百度编程了一波,如果你也是用的异步向后端发送请求,可能你的问题和我一样,继 ...
- 使用 Jenkins 进行持续集成与发布流程图-图解
- windows安装rabbitmq踩坑实录
最近学习springcloud消息总线需要用到rabbitmq,然后安装的时候踩了一些坑,记录如下: 首先安装rabbitmq之前需要先安装erlang,因为rabbitmq服务端使用erlang写的 ...
- 一行代码,让 VS Code 内置 PDF 阅读器变成深色模式
使用 CSS/JS 简单实现 PDF 深色模式.
- vue预渲染及其cdn配置
VUE SEO方案一 - 预渲染及其cdn配置 项目接入VUE这样的框架后,看起来真是太漂亮了,奈何与MCV框架比起来,单页应用程序却满足不了SEO的业务需求,首屏渲染时间也是个问题.总不能白学VUE ...
- hutool工具类常用API整理
0.官网学习地址 https://www.hutool.cn/ 1.依赖 <dependency> <groupId>cn.hutool</groupId> < ...
- 洛谷 P2392 kkksc03考前临时抱佛脚, dp / 深搜
题目链接 P2392 kkksc03考前临时抱佛脚 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题目 dp代码 #include <iostream> #includ ...
- XCTF练习题---MISC---坚持60S
XCTF练习题---MISC---坚持60S flag:flag{DajiDali_JinwanChiji} 解题步骤: 1.观察题目,下载附件,是一个java文件 2.打开玩了一会,真鸡儿难,试着反 ...