我在我的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等。
解决方案:
创建服务接口:IConfigService扩展BaseService
更改服务类:ConfigService扩展AbsBaseService实现IConfigService
在ControllerConfig中,将ConfigService替换为
如果我使用接口工作,但如果我使用类不工作。我不明白为什么,但它起作用了。
首先,您的大多数代码都可以简化,至少可以测试它是否运行良好:
@Transactional
public interface BaseService<T extends Base> {
void add(T obj); // No need to repeat @Transactional here, unless you want to override default behaviour (says, read_only, for instance)
void edit(T obj);
//Etc..
}
// Not much use of an Abstract class here, all methods are exposed in the interface already
@Service
public class ConfigService implements BaseService<Config> {
// Autowire the field directly. Constructor not needed anymore in this case
@Autowired
private ConfigRepository repository;
// Etc.
}
主计长将遵循与服务相同的准则。
现在应该可以了。如果不是,您需要使用完整的堆栈跟踪(或者至少是最后一个导致的
块,它才是真正有意义的那个)来更新您的问题。
我想知道以下解决方案之间的区别是什么,为什么使用解决方案2?有什么好处吗? 解决方案一: 解决方案2:
与属性自动装配相比,构造函数自动装配有什么特殊的优势吗……或者普通的优势。?优于迫使团队在Spring启动中使用构造函数自动装配……它有什么特殊的优势吗?两种类型的自动装配的优缺点
我与SpringBoot和JPA合作。我收到一个无法完成的错误。 这是我的主要课程: 这是我的班级失败的原因: 这是类: 这是错误消息: 错误创建bean的名称'请求LoggerImpl':注入自动生成的依赖失败; 无法自动关联字段:专用com。存储库。请求logdao.com。记录器。impl。RequestLoggerImpl。请求logdao;嵌套的异常是org。springframewor
我使用spring已经有一段时间了,但是今天早上我遇到了一些意想不到的行为。由于我自己无法决定这是一个理想的功能还是一个bug,所以我在这里展示它,希望我能得到一些关于为什么会发生这种情况的好解释。 简而言之,我在一个应用程序上下文中定义了多个bean,并且我使用utils创建了两个map bean:map name space,只将我的bean的一部分添加到这些map中。这两个地图有完全相同的条
我正在努力学习一本书名为《SpringMVC初学者指南》的书,我一直在努力创建存储库对象。我不断地得到一个BeanCreationException。不知道我还错过了什么。我想知道是否有人能帮我解决这个问题。 请在下面找到我的代码。谢谢 BeanCreationException XML文件: ProductCrontroller: 产品存储库: InMemoryProductRepository
我在网上做了很多搜索,但找不到一个使用自动连接构造函数进行单元测试的例子。我正在使用Spring将属性文件中的值自动关联到我的应用程序。我想对我的应用程序进行单元测试。java的start方法,但我有一个自动连接的构造函数,所以我不知道如何实例化MyApp。在没有自动连线属性的情况下,我在单元测试中这样做: 我不想模拟自动布线,因为我需要从属性文件中获取值,并且为了使事情更加复杂,我正在通过注释配