如何使用Spring Boot WAR查询Oracle SQL?

时间:2017-02-01 05:27:52

标签: spring spring-boot database-connection jndi

按照Spring Boot参考指南,我已经设置了一个Hello World示例。我的工作区使用Ant,因此我基于https://www.mkyong.com/ant/ant-spring-mvc-and-war-file-example/实现了build.xml生成的WAR文件在部署到WebLogic 12c服务器上时可以正常工作。注意:根据本指南,.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),但这并没有改变任何内容。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的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;
  }
}