配置类一@Configuration
2024-10-07 20:36:39
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之中获取对象,所以得到的是同一个对象。
最新文章
- 小div布局之卡片堆叠(card-stacking)
- hadoop安装及配置入门篇
- (原创) 巩固理解I2C协议(MCU,经验)
- oracle ORA-12514: TNS: no listener 解决方案
- 测试驱动开发(Test-Driven Development,简称TDD)--单元测试-->;提高代码质量
- 做一个App前需要考虑的几件事
- Yii2的view需要链接跳转
- 看看这些JavaScript题目你会做吗?
- [POJ 3788] Interior Points of Lattice Polygons
- full-background.js
- NotePad++ 配置C/C++编译环境
- Java内存模型JMM 高并发原子性可见性有序性简介 多线程中篇(十)
- HAOI2019+十二省联考 游记
- Linux系统调用列表(转)
- linux的基本操作(NFS服务配置)
- dokuwiki 安装配置
- Git:合并分支冲突问题
- NOIP水题测试(2017082301)
- 【翻译】std::list::remove - C++ Reference
- java redis基本操作