@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,如果找不到该属性文件则忽略它。

最新文章

  1. 解决ubuntu16.04软件中心闪退的问题
  2. 登录phpmyadmin提示: #1045 无法登录 MySQL 服务器
  3. Linux Shell脚本入门--cut命令
  4. HDU 4669 Mutiples on a circle(环状DP)
  5. jQuery点击图片弹出放大可拖动图片查看
  6. JavaScript 变量克隆和判断变量类型
  7. 《大象UML》看书笔记2:
  8. Android 开发笔记___RadioButton
  9. 去掉xcode编译warning:ld: warning: directory not found for option &#39;-L
  10. mysql主主配置
  11. ubuntu 外接显示器
  12. 考虑实现一个不抛异常的swap
  13. C# 队列(Queue)和 堆栈(Stack)
  14. dart基础语法
  15. Jenkins使用TFS部署
  16. Asp.netMVC中Html.Partial,RenderPartial,Action,RenderAction区别和用法
  17. 模板模式(TemplateMethod)
  18. 读取Maven项目下resources目录下的配置文件(properties为例)
  19. 20155315 2016-2017-2 《Java程序设计》第五周学习总结
  20. HDU 5294 Tricks Device 网络流 最短路

热门文章

  1. js知识梳理6:关于函数的要点梳理(2)(作用域链和闭包)
  2. C++---变量、数据类型和运算符
  3. 第一阶段:Java基础之数组
  4. linux磁盘之回环设备
  5. NodeJS学习日报day4——模块化
  6. 几个i的幂的累加公式1^2+2^2+3^2 2~5
  7. go的调度
  8. 20202127 实验二《Python程序设计》实验报告
  9. 还在用em strong吗?快来试试 text-emphasis
  10. 『现学现忘』Git基础 — 13、Git的基础操作