简体   繁体   English

Spring Boot Weblogic 12c JNDI 数据源:注入不起作用导致 NullPointerException

[英]Spring Boot Weblogic 12c JNDI DataSources: injection not working gives NullPointerException

I had asked a question here but was incomplete and was marked as duplicate here我在这里问了一个问题但不完整并在此处被标记为重复

So based on the question already asked - one particular answer by @surasin-tancharoen seemed to be what I needed.因此,基于已经提出的问题-@surasin-tancharoen 的一个特定答案似乎正是我所需要的。

However trying that too gives me a NullPointerException since the data source is never created / injected.然而,尝试这也会给我一个NullPointerException因为数据源从未被创建/注入。

Here are the details:以下是详细信息:

In the below code I have defined 2 beans.在下面的代码中,我定义了 2 个 bean。 I have defined both datasources with @Qualifier annotation and @ConfigurationProperties to read the JNDI name from properties file.我已经用@Qualifier注释和@ConfigurationProperties定义了两个数据源,以从属性文件中读取 JNDI 名称。

@Configuration
public class DataSourceConfig {
    @Bean
    @Primary
    @Qualifier("ds1")
    @ConfigurationProperties(prefix="spring.datasource1")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Qualifier("ds2")
    @ConfigurationProperties(prefix="spring.datasource2")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

In application.properties :application.properties

spring.datasource1.jndi-name=AbcDS
spring.datasource2.jndi-name=XyzDS

Then in my DAO - I am trying to use this datasource:然后在我的 DAO 中 - 我正在尝试使用此数据源:

@Autowired
@Qualifier("ds1")
DataSource dataSource;

However the datasource is not injected since I get a NullPointerException at this line of code:但是数据源没有注入,因为我在这行代码中得到了NullPointerException

conn = dataSource.getConnection();

All of this is being attempted by deploying the Spring Boot application to Weblogic 12c所有这些都是通过将 Spring Boot 应用程序部署到 Weblogic 12c 来尝试的

The solution is related to incorrect usage of 'DataSourceBuilder' which should not be used for a JNDI datasource.该解决方案与不应该用于 JNDI 数据源的“DataSourceBuilder”的不正确使用有关。

Here is how I got it working : ( deploying spring boot war on weblogic and consuming datasources defined in weblogic )这是我如何让它工作的:(在 weblogic 上部署 spring boot war 并使用 weblogic 中定义的数据源)

First we specify the datasources defined in weblogic - here we specify the JNDI name of the datasources defined in weblogic:首先我们指定weblogic中定义的数据源——这里我们指定weblogic中定义的数据源的JNDI名称:

spring.datasource.xyz.jndi-name=XYZDS
spring.datasource.test.jndi-name=TESTDS

This is where the Datasource is created using above defined properties and exposed: We are injecting the properties from application.properties into string variables to store JNDI names of datasources.这是使用上面定义的属性创建数据源并公开的地方:我们将application.properties 中的属性注入字符串变量以存储数据源的 JNDI 名称。

@Configuration
public class DataSourceConfig {

@Value( "${spring.datasource.xyz.jndi-name}" )
private String xyzJndiName;

@Value( "${spring.datasource.test.jndi-name}" )
private String testJndiName;

@Bean(name = "XyzDataSource")
public DataSource getXyzDataSource() throws Exception {        
    JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
    return dataSourceLookup.getDataSource(xyzJndiName);
}   

@Bean(name = "TestDataSource")
public DataSource getTestDataSource() throws Exception {        
    JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
    return dataSourceLookup.getDataSource(testJndiName);
}  

A few key points to note : We should not be using 'DataSourceBuilder' for a JNDI datasource - it will not work.需要注意的几个关键点:我们应该对 JNDI 数据源使用“DataSourceBuilder”——它不会工作。

This was the reason why it was not working in my case .这就是为什么它在我的情况下不起作用的原因。

Now I am using ' JndiDataSourceLookup ' which does the trick.现在我正在使用' JndiDataSourceLookup ',它可以解决问题。

Another thing to note is that we need to use the standard @Bean annotation with the attribute 'name'.另一件需要注意的是,我们需要使用带有属性“name”的标准@Bean 注解。

This will be important in the piece of code that consumes this datasource.这在使用此数据源的代码段中很重要。 So now the datasources are created successfully and exposed.所以现在数据源已成功创建并公开。

OK time to consume :可以消费的时间:

@Repository
public class SomeDaoImpl {

@Autowired
@Qualifier("XyzDataSource")
DataSource dataSource;

@Override
public List <String> create(Employee request) {

    Connection conn = null;
    conn = dataSource.getConnection();

Here we are using the @Qualifier annotation to pickup the appropriate datasource .这里我们使用@Qualifier注释来获取适当的数据源。 Thats it - this works now.就是这样 - 这现在有效。 I tried it with the other data source - it worked as well.我用其他数据源尝试过 - 它也有效。

NOTE: I would not like to accept my own answer - so I will wait for a couple of days if anyone else has a better and more elegant solution please answer - will be happy to accept your answer rather than answering my own question and accepting !注意:我不想接受我自己的答案——所以我会等几天,如果其他人有更好、更优雅的解决方案,请回答——我会很乐意接受你的答案,而不是回答我自己的问题并接受!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM