-
Notifications
You must be signed in to change notification settings - Fork 56
Sample spring boot工程详解
Shengyuan 卢声远 edited this page Mar 1, 2021
·
1 revision
实际上DAS不依赖于Spring Boot,本文旨在展示DAS在Spring Boot上最佳实践。
其实核心在于如何把DasClient
实例配置为Spring。DasClient
是线程安全且无状态的java实例,因此很适合配置成Spring的单例bean。
我们参照Sample工程,来看一下怎么把DasClient
实例配置为Spring bean。
@Configuration
public class DasConfiguration {
@Bean(name = "simpleDas")
public DasClient getDasClient() throws SQLException {
return DasClientFactory.getClient("MySqlSimple");
}
}
这是一个定义DasClient
bean的类。你也可以添加多个DasClient
bean连接不同的逻辑数据库。
@Qualifier("simpleDas")
@Autowired
DasClient simpleDao;
这里是把DasClient
bean注入到TestComponent
实例。
我们在TestComponent
里实现了InitializingBean
接口,用于展示DasClient
bean被注入之后跑和main方法一样的DAS的操作。
当运行启动ApplicationSample
的时候,你将看控制台同样可以打出如下信息:
Tom was inserted.
Tomy was updated.
The record was deleted.