spring-boot - SpringBoot - Websphere - Java 配置 - web.xml - 资源引用

标签 spring-boot websphere

我有一个 Web 应用程序,其中在 web.xml 中定义了 3 个 jndi 资源 1 个用于数据库,2 个用于动态缓存

如何在 Spring boot 中将其转换为 Java 配置。

以下是应用程序中的示例资源引用配置

<resource-ref>
    <description>Resource reference for database</description>
    <res-ref-name>jdbc/dbname</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

<resource-ref id="cache1">
    <description>cache1 description</description>
    <res-ref-name>cache/cache1</res-ref-name>
    <res-type>com.ibm.websphere.cache.DistributedMap</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

<resource-ref id="cache2">
    <description>cache2 description</description>
    <res-ref-name>cache/cache2</res-ref-name>
    <res-type>com.ibm.websphere.cache.DistributedMap</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

谢谢

最佳答案

Spring Boot 中的多个数据库

更多关于/字体:baeldung

Spring Boot可以简化上面的配置。

默认情况下,Spring Boot 将使用以 spring.datasource.* 为前缀的配置属性实例化其默认数据源:

spring.datasource.jdbcUrl = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]

我们现在想继续使用相同的方式来配置第二个数据源,但使用不同的属性命名空间:

spring.second-datasource.jdbcUrl = [url]
spring.second-datasource.username = [username]
spring.second-datasource.password = [password]

因为我们希望 Spring Boot 自动配置选择那些不同的属性(并实例化两个不同的数据源),所以我们将定义两个类似于前面部分的配置类:

    @Configuration
    @PropertySource({"classpath:persistence-multiple-db-boot.properties"})
    @EnableJpaRepositories(
       basePackages = "com.baeldung.multipledb.dao.user",
       entityManagerFactoryRef = "userEntityManager",
       transactionManagerRef = "userTransactionManager")
     public class PersistenceUserAutoConfiguration {

    @Primary
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource userDataSource() {
      return DataSourceBuilder.create().build();
    }
   // userEntityManager bean 

   // userTransactionManager bean
}

.

 @Configuration
 @PropertySource({"classpath:persistence-multiple-db-boot.properties"})
 @EnableJpaRepositories(
   basePackages = "com.baeldung.multipledb.dao.product", 
   entityManagerFactoryRef = "productEntityManager", 
   transactionManagerRef = "productTransactionManager")
public class PersistenceProductAutoConfiguration {

@Bean
@ConfigurationProperties(prefix="spring.second-datasource")
public DataSource productDataSource() {
    return DataSourceBuilder.create().build();
}

// productEntityManager bean 

// productTransactionManager bean
}

现在我们已经根据引导自动配置约定在 persistence-multiple-db-boot.properties 中定义了数据源属性。

有趣的部分是使用@ConfigurationProperties 注释数据源bean 创建方法。我们只需要指定相应的配置前缀即可。在此方法中,我们使用 DataSourceBuilder,Spring Boot 将自动处理其余部分。

但是如何将配置的属性注入(inject)到数据源配置中呢?

当调用 DataSourceBuilder 的 build() 方法时,它会调用其私有(private)的 bind() 方法:

public T build() {
    Class<? extends DataSource> type = getType();
    DataSource result = BeanUtils.instantiateClass(type);
    maybeGetDriverClassName();
    bind(result);
    return (T) result;
 }

这个私有(private)方法执行了很多自动配置魔法,将解析的配置绑定(bind)到实际的 DataSource 实例:

private void bind(DataSource result) {
    ConfigurationPropertySource source = new 
 MapConfigurationPropertySource(this.properties);
    ConfigurationPropertyNameAliases aliases = new 
ConfigurationPropertyNameAliases();
    aliases.addAliases("url", "jdbc-url");
    aliases.addAliases("username", "user");
    Binder binder = new Binder(source.withAliases(aliases));
    binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result));
}

虽然我们自己不必接触任何这些代码,但了解 Spring Boot 自动配置的幕后情况仍然很有用。

除此之外,事务管理器和实体管理器 bean 配置与标准 Spring 应用程序相同。

关于spring-boot - SpringBoot - Websphere - Java 配置 - web.xml - 资源引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51924369/

相关文章:

java - 应用服务器和 Spring 上下文加载

java - 使用 Java 的 WebSphere 的 SSL 握手错误

java - WAS9 服务器中的 org.apache.cxf.bus.extension.ExtensionException

spring-boot - Spring Boot 是嵌入式 Tomcat 还是 Jetty 生产级

java - 微服务 - RestTemplate UnknownHostException

rest - 如何从 spring boot 资源文件夹中嵌入图像和附加文件

java - HAProxy应用服务器响应其他端口

java - 如何获取.ear文件在应用服务器中的路径?

java - 在 JBOSS EAP 6.1 上部署 spring boot

spring-boot - SSEemitter 与 Spring Boot 中的 Flux