2017-02-01 2 views
0

Springブートのリファレンスガイドに続いて、Hello Worldの例を設定しました。私の職場ではAntを使用していますので、https://www.mkyong.com/ant/ant-spring-mvc-and-war-file-example/に基づいてbuild.xmlを実装しました。結果のWARファイルは、WebLogic 12cサーバーにデプロイすると正しく動作します。注:このガイドに従って、.propertiesファイルは${web.classes.dir}にコピーされます。SpringブートWARを使用してOracle SQLに問い合せる方法は?

ここで、JNDI経由でサーバーのOracle SQLデータベースにクエリを実行します。春ブーツが&ガイドを参照するのさまざまな部分に続いて、これは現時点での私の修正コードです:

@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.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)に変更しても、何も変更されませんでした。どうすれば解決できますか?

+3

オートワイヤリングがstatic'フィールド '上では動作しません:あなたのコードは次のようになります。また、静的メソッドを使用して、関数を呼び出す/実装するべきではありません。 –

答えて

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; 
    } 
} 
+0

答えをありがとう、しかしそれを試した後、私は新しいエラーが発生しました。私の質問の更新されたセクションを確認してください。 – thegreatjedi

+0

@thegreatjediあなたはおそらくいくつかの依存関係や設定を失っているでしょう。あなたのアプリケーションを '--debug'オプションで実行し、ログにダンプされる' AUTO CONFIGURATION REPORT'であなたの質問を更新してください。また、あなたのantビルドスクリプトからあなたのアイビーの依存関係を含める。 – Strelok

関連する問題