@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. vue日历(纯 js,没用任何插件和组件)

    效果图: 代码:   <template> <div class="calender"> <div class="top"> ...

  2. vue 多级组件数据传递

    A包含B组件,B包含C组件   那么A 传递到C 组件可以通过 在B组件中绑定 $attrs 具体代码可以参见github: https://github.com/qiaoqiao10001/vueA ...

  3. 搭建springboot集成mybatis

    1.new project创建新项目选择spring initializr: 2.选择依赖需要选择web.mybatis.mysql就够了,后续需要其他的直接pom引入依赖就好了: 3.自己在java ...

  4. C++内存空间管理

    C++内存空间管理 1.C++内存机制 1.栈(Stack),函数中的局部变量,由编译器负责分配释放,函数结束,变量释放. 2.堆(Heap),通过new 申请的内存,由delete或delete[] ...

  5. Python数据展示 - 生成表格图片

    前言 前一篇文章介绍了推送信息到企业微信群里,其中一个项目推送的信息是使用Python自动生成的表格,本文来讲讲如何用Python生成表格图片. 选一个合适库 Python最大的优点就是第三方库丰富, ...

  6. C++五子棋(五)——实现AI落子

    AI思考落子点 在之前我们已经实现计算权值了,现在要想让AI落子,应根据之前的计算结果使棋子落在分值最大点上.当然可能会出现多个分值相同的最大点,这时在其中随机取一个点落下即可. chessData. ...

  7. HTML5有哪些新特性

    (一)  语义标签 <header>表示页面中一个内容区块或整个页面的标题. <section>页面中的一个内容区块,如章节.页眉.页脚或页面的其他地方,可以和h1.h2--元 ...

  8. javascript中的宏任务和微任务(一)

    一.宏任务和微任务有哪些 宏任务:setTimeout,setInterval,ajax,dom,宏任务是由浏览器提供的 微任务:promise,async/await,微任务是由es6提供的 二.微 ...

  9. k8s client-go源码分析 informer源码分析(1)-概要分析

    k8s informer概述 我们都知道可以使用k8s的Clientset来获取所有的原生资源对象,那么怎么能持续的获取集群的所有资源对象,或监听集群的资源对象数据的变化呢?这里不需要轮询去不断执行L ...

  10. HCNP Routing&Switching之代理ARP

    前文我们了解了端口隔离相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/16186451.html:今天我们来聊一聊ARP代理相关话题: 端口隔离之破解之 ...