私はスプリングブートが初めてです。クラスを別のパッケージ (「アプリケーション」を含むもの) に移動した後、Bean クラスをインスタンス化できませんでした: デフォルトのコンストラクターが見つかりません例外が発生します。
前(実行可能なコード)
package com.server;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(basePackages = {"com.server" })
@EnableAutoConfiguration
@Profile({ "default" })
@Controller
public class Application {
private static Log logger = LogFactory.getLog(Application.class);
public static void main(String[] args) {
logger.info("Starting Application...");
SpringApplication.run(Application.class, args);
}
}
http://bitwiseor.com/2013/09/20/creating-test-services-with-spring-boot/のコード
package com.server;
import java.util.Collections;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Configuration
@Controller
@Profile({ "default" })
class Franchise {
private JdbcTemplate jdbcTemplate;
@Autowired
public Franchise(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@ResponseBody
@RequestMapping("/api/franchise/{id}")
String franchiseId(@PathVariable Long id) {
try {
return jdbcTemplate.queryForMap("SELECT id, title FROM franchises WHERE id=?", id).toString();
} catch(EmptyResultDataAccessException ex) {
return Collections.EMPTY_MAP.toString();
}
}
@ResponseBody
@RequestMapping("/api/franchise")
String franchises() {
try {
return jdbcTemplate.queryForList("SELECT id, title FROM franchises").toString();
} catch(EmptyResultDataAccessException ex) {
return Collections.EMPTY_MAP.toString();
}
}
}
クラス「アプリケーション」と「フランチャイズ」が同じパッケージにある場合、サーバーを立ち上げることができます。ただし、以下に示すようにクラス「フランチャイズ」を別のパッケージに移動すると、次の例外が発生しました: Bean クラスをインスタンス化できませんでした: デフォルトのコンストラクターが見つかりません例外が発生します。
package com.server.api;
import java.util.Collections;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Configuration
@Controller
@Profile({ "default" })
class Franchise {
private JdbcTemplate jdbcTemplate;
@Autowired
public Franchise(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@ResponseBody
@RequestMapping("/api/franchise/{id}")
String franchiseId(@PathVariable Long id) {
try {
return jdbcTemplate.queryForMap("SELECT id, title FROM franchises WHERE id=?", id).toString();
} catch(EmptyResultDataAccessException ex) {
return Collections.EMPTY_MAP.toString();
}
}
@ResponseBody
@RequestMapping("/api/franchise")
String franchises() {
try {
return jdbcTemplate.queryForList("SELECT id, title FROM franchises").toString();
} catch(EmptyResultDataAccessException ex) {
return Collections.EMPTY_MAP.toString();
}
}
}
このクラスを別のパッケージに移動したい場合、どうすればこの問題を解決できますか?
ありがとう!
編集:解決策を見つけました 次のタグを削除すると、クラスを別のパッケージに入れることができます。@Configuration @Profile({ "デフォルト" })
しかし、私には理由がわかりません...