import org.springframework.context.annotation.Configuration;

@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并

用于构建bean定义,初始化Spring容器

Spring Boot不需要在xml配置注解扫描,需要你保证你的启动Spring Boot main入口,在这些类的上层包就行。

  • @Configuation等价于<Beans></Beans>
  • @Bean等价于<Bean></Bean>
@Configuration
public class ExampleConfiguration { @Value("com.mysql.jdbc.Driver")
private String driverClassName; @Value("jdbc://xxxx.xx.xxx/xx")
private String driverUrl; @Value("root")
private String driverUsername; @Value("123456")
private String driverPassword; @Bean(name = "dataSource")
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(driverUrl);
dataSource.setUsername(driverUsername);
dataSource.setPassword(driverPassword);
return dataSource;
} @Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
} }

@Autowired

private DataSource dataSource;

这个dataSource就是我们在ExampleConfiguration中配的DataSource

@Component注解也会当做配置类,但是并不会为其生成CGLIB代理Class,所以执行了两次new操作,所以是不同的对象。当时@Configuration注解时,生成当前对象的子类Class,并对方法拦截,第二次new时直接从BeanFactory之中获取对象,所以得到的是同一个对象。

最新文章

  1. 小div布局之卡片堆叠(card-stacking)
  2. hadoop安装及配置入门篇
  3. (原创) 巩固理解I2C协议(MCU,经验)
  4. oracle ORA-12514: TNS: no listener 解决方案
  5. 测试驱动开发(Test-Driven Development,简称TDD)--单元测试--&gt;提高代码质量
  6. 做一个App前需要考虑的几件事
  7. Yii2的view需要链接跳转
  8. 看看这些JavaScript题目你会做吗?
  9. [POJ 3788] Interior Points of Lattice Polygons
  10. full-background.js
  11. NotePad++ 配置C/C++编译环境
  12. Java内存模型JMM 高并发原子性可见性有序性简介 多线程中篇(十)
  13. HAOI2019+十二省联考 游记
  14. Linux系统调用列表(转)
  15. linux的基本操作(NFS服务配置)
  16. dokuwiki 安装配置
  17. Git:合并分支冲突问题
  18. NOIP水题测试(2017082301)
  19. 【翻译】std::list::remove - C++ Reference
  20. java redis基本操作

热门文章

  1. Codeforces 346D Robot Control DP spfa 01BFS
  2. 【LeetCode】拓扑排序
  3. 【Muduo库】【base】基本类
  4. string参考
  5. Python爬虫实战——反爬机制的解决策略【阿里】
  6. jq enter键发送
  7. Cache技术―OSCache
  8. Java 基础 - System.arraycopy() 浅拷贝 深拷贝
  9. LUOGU P2408 不同子串个数(后缀数组)
  10. contest-20191021