我是初学者的新手。在我将一个类移动到不同的包(其他包含'Application')后,无法实例化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();
}
}
}
当“应用程序”和“特许经营”类位于同一个程序包中时,我可以调出服务器。但是,当我将类'Franchise'移动到另一个包中时,如下所示,我有一个例外:无法实例化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();
}
}
}
如果我想将这个类移到另一个包中,我该如何解决这个问题?
谢谢!
编辑:我找到了解决方案 当我删除以下标记时,我可以将该类放入单独的包中。 @组态 @Profile({“default”})
但我不知道为什么......
答案 0 :(得分:0)
在我看来,您的Franchise类是包私有(java类的默认可见性)。这将解释一切(并且不需要涉及Spring或编译器以外的任何东西)。要修复它,只需将您的课程声明为“公开”。
Marten也是正确的,因为@Configuration可能不是你想要的特许经营权(但在这种情况下它是无害的)。