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.NoSuchBeanDefinitionException: 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(required=true)}
私が試しました@Autowired
注釈を@Autowired(required=true)
に変更しても、何も変更されませんでした。どうすれば解決できますか?
オートワイヤリングがstatic'フィールド '上では動作しません:あなたのコードは次のようになります。また、静的メソッドを使用して、関数を呼び出す/実装するべきではありません。 –