@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. PAT B1031查验身份证

    一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...

  2. Native方法的使用

    Java不是完美的,Java的不足除了体现在运行速度上要比传统的C++慢许多之外,Java无法直接访问到操作系统底层(如系统硬件等),为此Java使用native方法来扩展Java程序的功能. 可以将 ...

  3. 大数据学习之路之ambari配置(四)

    试了很多遍,内存还是不够,电脑不太行的,不建议用ambari!!! 放弃了

  4. java对象有什么重要的?

    3.历史上讲,对象有什么重要的?  [新手可忽略不影响继续学习]早期的编程主要是面向过程的编程,处理的问题都相对的简单,比较过程化,换句话说,就是一步一步从开始到结束,比如第一步进入电梯,第二步关门, ...

  5. JavaScript实现有农历和节气节假日的日历

    运行效果: 源代码: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content ...

  6. 动态div点击事件传递对象参数格式-草稿889

    <button type='button' style='border: 1px solid #eeeeee;color: #717070;height: 20px;border-radius: ...

  7. victoriaMetrics库之布隆过滤器

    victoriaMetrics库之布隆过滤器 代码路径:/lib/bloomfilter 概述 victoriaMetrics的vmstorage组件会接收上游传递过来的指标,在现实场景中,指标或瞬时 ...

  8. Python入门-面向对象-装饰器

    1.变量作用域 全局变量和局部变量 #变量是有作用域的,分为全局变量和局部变量 num = 100 #这是全局变量 def change(): """ 查看变量作用域 & ...

  9. 阿里云申请SSL证书 并部署到SpringBoot项目

    前提 有一台阿里云的服务器(安装了java环境) 有已经备案的域名,并且域名绑定上面的服务器 申请SSL证书 申请教程:https://blog.csdn.net/yunweifun/article/ ...

  10. 【FAQ】应用集成HMS Core部分服务出现“ 6003报错”情况的解决方法来啦

    背景 开发者在应用中集成HMS Core部分服务时,android sdk 以及flutter等跨平台sdk,会出现编译打包后,运行报6003错误码的情况.根据查询可以得知,错误代码 6003 表示证 ...