【问题标题】:How to query Oracle SQL with Spring Boot WAR?如何使用 Spring Boot WAR 查询 Oracle SQL?
【发布时间】:2017-02-01 05:27:51
【问题描述】:

按照 Spring Boot 参考指南,我设置了一个 Hello World 示例。我的工作场所使用 Ant,因此我基于 https://www.mkyong.com/ant/ant-spring-mvc-and-war-file-example/ 实现了 build.xml。当部署到 WebLogic 12c 服务器上时,生成的 WAR 文件可以正常工作。注意:根据本指南,.properties 文件将复制到 ${web.classes.dir}

现在,我想通过 JNDI 查询服务器的 Oracle SQL 数据库。遵循 Spring Boot 参考和指南的各个部分,这是我目前修改的代码:

@SpringBootApplication
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer {
  @Autowired
  private static JdbcTemplate jdbcTemplate;

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
  }

  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }

  public static void update(String query) {
    jdbcTemplate.update(query);
  }
}

@RestController
@RequestMapping("/query")
public class CrudController {

  @RequestMapping(value="/update", method=RequestMethod.GET)
  public String update(@PathVariable String tableName, /* other params */) {
    // Generates query from params
    Application.update(query);
    return query;
  }
}

我还添加了一个 application.properties 文件,其中包含符合 Spring 参考指南的单行:

spring.datasource.jndi-name=jndiName

此时,WAR 仍然可以部署到服务器上,但是当我转到 http://ipaddr:port/appName/query/update?params 时,我得到了 NullPointerException。我已经单独验证update() 正确生成了一个语法有效的 SQL 查询,所以我怀疑我的数据库配置有误。

连接到 JNDI 数据库并执行查询的正确方法是什么?

编辑

根据 Strelok 的回答更新我的代码后,我尝试在 WebLogic 服务器上运行更新后的 WAR 文件,随后引发以下异常:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crudController': Injection of autowired dependencies failed; ...
...
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate main.java.controllers.CrudController.jdbcTemplate; ...
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionExcept‌​ion: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(req‌​uired=true)}

我尝试将@Autowired 注释修改为@Autowired(required=true),但这并没有改变任何东西。我该如何解决这个问题?

【问题讨论】:

  • 自动装配不适用于 static 字段。此外,您不应该使用静态方法来调用/实现功能。

标签: spring spring-boot database-connection jndi


【解决方案1】:

您的 JdbcTemplate 是静态的,位于您的应用程序类中,而它应该属于您的控制器。这就是你应该使用它的地方。

Spring 不能直接注入静态字段,因此请确保 Spring 注入的任何内容都不是静态的。您的代码应如下所示:

Application.java

@SpringBootApplication
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
  }

  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }
}

CrudController.java

@RestController
@RequestMapping("/query")
public class CrudController {

  @Autowired
  private JdbcTemplate jdbcTemplate;


  @RequestMapping(value="/update", method=RequestMethod.GET)
  public String update(@PathVariable String tableName, /* other params */) {
    jdbcTemplate.update(query);
    return query;
  }
}

【讨论】:

  • 感谢您的回答,但尝试后我遇到了一个新错误。请检查我的问题的更新部分。
  • @thegreatjedi 您可能缺少一些依赖项和/或配置设置。使用--debug 选项运行您的应用程序,并使用将转储到日志中的AUTO CONFIGURATION REPORT 更新您的问题。还包括你的 ant 构建脚本中的 ivy 依赖项。
猜你喜欢
  • 2018-06-02
  • 2020-02-26
  • 1970-01-01
  • 2019-12-10
  • 2022-09-22
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多