@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 常用注解与使用的更多相关文章

  1. Spring Ioc 常用注解

    在开发中spring ioc的配置方式有多种方式,常用的一般都是byName.byType .以及其自动装配可以查看http://www.cnblogs.com/gubai/p/9140840.htm ...

  2. Spring MVC学习总结(2)——Spring MVC常用注解说明

        使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能. 02     之前在使用Struts2实现MVC的注解时,是借助struts2-convention这个插件,如今我们使 ...

  3. Spring学习总结(2)——Spring的常用注解

    本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). ? 1 <context:compon ...

  4. Spring Boot 常用注解汇总

    一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...

  5. Spring Boot常用注解总结

    Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...

  6. 接近8000字的Spring/SpringBoot常用注解总结!安排!

    0.前言 大家好,我是 Guide 哥!这是我的 221 篇优质原创文章.如需转载,请在文首注明地址,蟹蟹! 本文已经收录进我的 75K Star 的 Java 开源项目 JavaGuide:http ...

  7. Spring/SpringBoot常用注解总结

    转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...

  8. Spring IoC 公共注解详解

    前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 什么是公共注解?公共注解就是常见的Java ...

  9. Spring IOC 常用的注解

    一.@Bean 1.配置类 @Configuration public class MainConfig { @Bean public Person person(){ return new Pers ...

随机推荐

  1. ubantu14.04系统设置无法正常使用

    方法一:执行命令: sudo apt-get install ubuntu-desktop方法二:如果系统设置打不开,请重新安装gnome-control-centersudo apt-get ins ...

  2. script标签中defer和async的区别(稀土掘金学习)

    如果没有defer或async属性,浏览器会立即加载并执行相应的脚本.它不会等待后续加载的文档元素,读取到就会开始加载和执行,这样就阻塞了后续文档的加载. 下图可以直观的看出三者之间的区别: 其中蓝色 ...

  3. Vue src动态引入图片不显示问题

    使用vue动态引入图片显示失败,查看控制台,发现图片返回类型为text/html,这里我的图片是从后台服务器上获取的,如果你没有使用Vue的devServer.proxy 进行代理,可以光速移步百度 ...

  4. python---希尔排序的实现

    def shell_sort(alist): """希尔排序""" n = len(alist) gap = n // 2 # 插入算法执行 ...

  5. 解决一次calico异常情况,pod之间访问pod ip不通

    k8s 集群采用二进制安装,cni网络插件用calico通讯问题描述:发现有些pod不是很正常例如: ht13.node正常系统采样 [root@ht6 ~]# cat /etc/redhat-rel ...

  6. nfs客户端的一次处理

    为什么要说这个呢,由于节点环境不一致,导致在重建pod时,我们暂且叫该pod为 cxpod,cxpod所在宿主机出现了问题现象如下:一.cxpod始终处于创建中 ContainerCreating [ ...

  7. 如何使用 python 爬取酷我在线音乐

    前言 写这篇博客的初衷是加深自己对网络请求发送和响应的理解,仅供学习使用,请勿用于非法用途!文明爬虫,从我做起.下面进入正题. 获取歌曲信息列表 在酷我的搜索框中输入关键词 aiko,回车之后可以看到 ...

  8. Git上传本地仓库文件到Gitee(Github同理)

    前言:本来想把最近的代码更新到Github上,但是校园网打不开,于是决定暂时先更新到Gitee中,Github中的操作也同理. 1. 创建云仓库: 就是在Gitee/Github上创建仓库,这里不演示 ...

  9. 数仓建模—建模工具PdMan(CHINER)介绍

    数据仓库系列文章(持续更新) 数仓架构发展史 数仓建模方法论 数仓建模分层理论 数仓建模-宽表的设计 数仓建模-指标体系 数据仓库之拉链表 数仓-数据集成 数仓-数据集市 数仓-商业智能系统 数仓-埋 ...

  10. 终极套娃 2.0|云原生 PaaS 平台的可观测性实践分享

    某个周一上午,小涛像往常一样泡上一杯热咖啡 ️,准备打开项目协同开始新一天的工作,突然隔壁的小文喊道:"快看,用户支持群里炸锅了 -" 用户 A:"Git 服务有点问题, ...