下面列出了怎么用javax.annotation.sql.DataSourceDefinition的API类实例代码及写法,或者点击链接到github查看源代码。
private DataSource createDelegate(DataSourceDefinition dataSourceDefinition) {
HikariConfig config = new HikariConfig();
if (dataSourceDefinition.url() != null) {
config.setJdbcUrl(dataSourceDefinition.url());
}
if (dataSourceDefinition.user() != null) {
config.setUsername(dataSourceDefinition.user());
config.setPassword(dataSourceDefinition.password());
}
if (dataSourceDefinition.maxPoolSize() > 0) {
config.setMaximumPoolSize(dataSourceDefinition.maxPoolSize());
}
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
return wrap(new HikariDataSource(config));
}
public void registerDataSources(@Observes AfterBeanDiscovery afterBeanDiscovery) {
DataSourceDefinition defaultDataSource = dataSourceDefinitions.stream()
.filter(dsd -> dsd.name().equals(EntityManagerFactoryProvider.DEFAULT_EMF))
.findFirst()
.orElse(null);
Bean<ws.ament.hammock.jpa.DataSourceDefinition> defaultBean = beanDelegates
.stream()
.filter(b -> b.getName().equals(EntityManagerFactoryProvider.DEFAULT_EMF))
.findFirst()
.orElse(null);
dataSourceDefinitions.stream().map(dataSourceDefinitionDataSourceDefinitionFunction)
.map(dataSourceDefinition -> new DataSourceDelegateBean(dataSourceDefinition.getName(),
() -> dataSourceDefinition))
.forEach(afterBeanDiscovery::addBean);
beanDelegates.stream().map(bean -> new DataSourceDelegateBean(bean.getName(), () -> bean.create(null)))
.forEach(afterBeanDiscovery::addBean);
databaseProducers.stream().map(producer -> new DataSourceDelegateBean(producer, create(producer)))
.forEach(afterBeanDiscovery::addBean);
if (defaultBean == null && defaultDataSource == null) {
afterBeanDiscovery.addBean(new DefaultDataSourceBean());
}
}
public HammockDataSource(DataSourceDefinition dataSourceDefinition) {
this.name = dataSourceDefinition.name();
this.delegate = createDelegate(dataSourceDefinition);
}
public void findDataSourceDefinition(@Observes @WithAnnotations(DataSourceDefinition.class) ProcessAnnotatedType<?> pat) {
DataSourceDefinition annotation = pat.getAnnotatedType().getJavaClass().getAnnotation(DataSourceDefinition.class);
dataSourceDefinitions.add(annotation);
}
public void findDataSourceDefinitionBeans(@Observes ProcessBean<ws.ament.hammock.jpa.DataSourceDefinition> processBean) {
beanDelegates.add(processBean.getBean());
}
public void findDataSourceProducers(@Observes ProcessProducer<?, ws.ament.hammock.jpa.DataSourceDefinition> processProducer) {
Database database = processProducer.getAnnotatedMember().getAnnotation(Database.class);
databaseProducers.add(database.value());
}
private Supplier<ws.ament.hammock.jpa.DataSourceDefinition> create(String name) {
return () -> CDI.current().select(ws.ament.hammock.jpa.DataSourceDefinition.class)
.select(database(name))
.get();
}
public void detectDataSourceDefinition(@Observes @WithAnnotations(DataSourceDefinition.class) ProcessAnnotatedType<?> patEvent) {
AnnotatedType at = patEvent.getAnnotatedType();
dataSourceDefinition = at.getAnnotation(DataSourceDefinition.class);
}