【发布时间】:2016-09-04 03:49:51
【问题描述】:
我的 spring boot 应用程序中有这些类(spring hibernate/data/jpa/web):
pkg 实体:
public interface Base {
// getter/setter methods
}
@MappedSuperclass
public abstract class AbsBase implements Base {
// common fields & getter/setter methods
}
@Entity(name = "Config")
@Table(name = "config")
public class Config extends AbsBase implements Base, Serializable {
//additional fields & getter/setter methods
}
pkg 存储库:
@NoRepositoryBean
public interface BaseRepository<T extends Base> extends JpaRepository<T, Integer> {}
@Repository
public interface ConfigRepository extends BaseRepository<Config> {
//Queries
}
pkg 服务:
@Transactional
public interface BaseService<T extends Base> {
@Transactional void add(T obj);
@Transactional void edit(T obj);
//Etc..
}
public abstract class AbsBaseService<T extends Base> implements BaseService<T> {
private BaseRepository<T> repository;
public AbsBaseService(BaseRepository<T> repository) {
super();
this.repository = repository;
}
@Override public void add(T obj) { repository.save(obj); }
@Override public void edit(T obj) { repository.save(obj); }
// Etc
}
@Service
public class ConfigService extends AbsBaseService<Config> implements BaseService<Config> {
private ConfigRepository repository;
@Autowired
public ConfigService(ConfigRepository repository) {
super(repository);
this.repository = repository;
}
// Etc.
}
如果我不创建任何控制器所有工作,但如果我创建一个控制器:
pkg 控制器:
public interface BaseController<T extends Base> { // methods }
public abstract class AbsBaseController<T extends Base> implements BaseController<T> {
private BaseService<T> service;
public AbsBaseController(BaseService<T> service) {
this.service = service;
}
@Override
@RequestMapping(value = "/Add", method = RequestMethod.POST)
public String addAction(@ModelAttribute("entity") T entity, BindingResult result, Model model,
final RedirectAttributes redirectAttributes) {
service.add(entity, result);
if (result.hasErrors()) {
return addUrl;
}
}
@Controller
public class ConfigController extends AbsBaseController<Config> implements BaseController<Config> {
private ConfigService configService;
@Autowired
public ConfigController(ConfigService configService) {
super(configService);
this.configService = configService;
}
}
我收到此错误(在 ConfigController 中出现错误 autowired ConfigService):
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configControllerImpl' defined in file [C:\workspace-sts\prueba_boot_thymeleaf\target\classes\prueba\controller\config\ConfigControllerImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [prueba.service.config.ConfigServiceImpl]: No qualifying bean of type [prueba.service.config.ConfigServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [prueba.service.config.ConfigServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at prueba.BootThymeleafApplication.main(BootThymeleafApplication.java:12) [classes/:na]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [prueba.service.config.ConfigServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
... 19 common frames omitted
第一个问题是: 我已经注释了@Transaction BaseService 接口及其方法。这是正确的?在 AbsBaseService 类中更好?
我看到 configService 构造函数在异常之前执行。 为什么不自动接线?
更新
我需要构造函数@Autowired,因为我在抽象/超类通用服务/控制器中实现了常用方法。
继承树: AbsBaseXXXX --> AbsBaseCodeNameXXXX --> AbsBaseCodeNamePeriodXXXX
Config 从 AbsBase 扩展并实现 Base。 ConfigService 从 AbsBaseService 扩展并实现 BaseService(使用 ConfigRepository) ConfigController 从 AbsBaseController 扩展并实现 BaseController(使用 ConfigService)
我有一个通用方法实现的通用抽象类: 实体接口: Base、BaseCodeName、BaseCodeNamePeriod....
抽象通用实体类: AbsBase、AbsBaseCodeName、AbsBaseCodeNamePeriod....
通用接口: BaseRepository、BaseCodeNameRepository、BaseCodeNamePeriodRepository、...
抽象通用服务(他们需要相应的通用存储库): AbsBaseService, AbsBaseCodeNameService,AbsBaseCodeNamePeriodService,...
抽象的通用控制器(他们需要相应的通用服务): AbsBaseController 等...
【问题讨论】:
-
你在哪里启动 Spring boot(包)的类,你的控制器和服务在哪里?也许根本没有扫描 Service bean
标签: spring constructor spring-boot autowired