- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我创建了一个自定义存储库。在这个存储库中,我需要访问标准存储库(findAll)的一些方法,所以我添加了
我使用了 Spring Boot
@EntityScan(basePackageClasses = {UserAgendaApplication.class, Jsr310JpaConverters.class})
@SpringBootApplication
public class UserAgendaApplication {
public static void main(String[] args) {
SpringApplication.run(UserAgendaApplication.class, args);
}
}
public interface UsersRepositoryCustom {
public Page<Users> customSearch(UserSearch UserSearch, Pageable page);
}
public interface UsersRepository extends JpaRepository<Users, Integer>, JpaSpecificationExecutor<Users>, UsersRepositoryCustom {
}
@Repository
public class UsersRepositoryImpl implements UsersRepositoryCustom {
@PersistenceContext
private EntityManager em;
private final UsersRepository usersRepository;
@Autowired
public UsersRepositoryImpl(final UsersRepository usersRepository){
this.usersRepository=usersRepository;
}
@Override
public Page<Users> customSearch(UserSearch UserSearch, Pageable page) {
...
return usersRepository.findAll(specification, page);
}
}
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
public UsersServiceImpl(UsersRepository usersRepository) {
this.usersRepository = usersRepository;
}
private UsersRepository usersRepository;
@Override
public Page<Users> customSearch(UserSearch userSearch, Pageable page) {
usersRepository.customSearch(userSearch, page);
...
}
}
在 Controller 中
@Controller
public class UsersController {
private final UsersService usersService;
@Autowired
public UsersController( final usersService usersService){
this.usersService=usersService;
}
}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersController': Unsatisfied dependency expressed through field 'usersService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersServiceImpl' defined in file [/home/alpha/Development/project/UserAgenda/build/classes/main/com/alpha/userAgenda/service/UsersServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'usersRepositoryImpl': Bean with name 'usersRepositoryImpl' has been injected into other beans [usersRepository] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
我尝试在 Controller 和 UsersRepositoryImpl 中的 setUsersRepository 上放置 Autowiring ,但得到相同的错误
编辑
public abstract class UsersRepositoryCustom extends SimpleJpaRepository<Users, Integer>
public Page<Users> customSearch(UserSearch userSearch, Pageable page) {
...
}
因为我使用SimpleJpaRepository,所以我不再需要UserRepository,但我需要在UersRepositoryCustom中添加此代码
private final EntityManager entityManager;
private final JpaEntityInformation<Users, Integer> entityInformation;
@Autowired
public UsersRepositoryCustom(final JpaEntityInformation<Users, Integer> entityInformation,
final EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
this.entityInformation = entityInformation;
}
在实现 UsersService 的 UsersServiceImpl 中,我有
@Autowired
public UsersRepositoryCustom usersRepositoryCustom;
我明白了
.UsersRepositoryImpl 中构造函数的参数 0 需要类型为“org.springframework.data.jpa.repository.support.JpaEntityInformation”的 bean,但无法找到。
编辑2
梯度配置
buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
mainClassName = 'com.alpha.useragenda.UserAgendaApplication'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'org.hsqldb', name: 'hsqldb', version: '2.0.0'
compile group: 'org.postgresql', name: 'postgresql', version: '42.1.1'
compile group: 'org.webjars', name: 'bootstrap', version: '3.3.7'
compile group: 'org.webjars.bower', name: 'bootstrap-table', version: '1.11.1'
compile('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
tasks.withType(JavaCompile) {
options.compilerArgs = ["-Xlint:unchecked", "-Xlint:deprecation", "-parameters"]
}
最佳答案
我认为你最好的办法就是使用这个
package stack.overflow;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
public interface UsersCrud extends CrudRepository<Users, Long> {
//My other query prototypes here
@Query("select c1 from t1")
public Object customQuery();
}
通过这种方式,您可以以最小的努力获得特殊查询。否则,我建议遵循OP评论中给出的建议(保留存储库并处理服务中的特殊情况
关于java - UnsatisfiedDependencyException 在构造函数上 Autowiring 并且设置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44760505/
这更像是一个最佳实践类型的问题。 我听过很多次: a) 在 Spring 中 Autowiring 时,最佳做法是 Autowiring 接口(interface)“而不是”实现。 和.. b) 我还
我正在查看工作区中的一些旧示例。我看不出怎么样由于没有 @Autowired, Autowiring 完成。 Spring boot + facebook 默认配置。 @Controller @Req
事实似乎并非如此。我曾经认为 XML 配置是为了覆盖注释。但是当我在XML配置中设置autowire =“no”时,bean的@Autowired注释属性仍然有效。我不再确定 XML autowire
为什么需要 Autowiring ? Autowiring 概念的解释是什么?@autowired Spring Framework 中的注释. 最佳答案 不需要 Autowiring ,只是方便。
来自this Spring documentation我知道当我使用@Bean时,默认值已经相当于: @Bean(autowire = Autowire.NO) (Default) No autowi
遇到了一个奇怪的要求。我需要将唯一的错误 ID 附加到 log4j 消息并将该消息 ID 返回给接口(interface)。所以,我虽然让我们创建一个 spring 服务,就像这样 public cl
这个问题已经有答案了: @Autowire failing with @Repository (3 个回答) 已关闭 4 年前。 我有一个类“ReportEverythingForm”,它拒绝通过自动
我是 Spring 的新手。我正面临 Spring-Boot 的问题。我正在尝试将一个字段从外部配置文件 Autowiring 到一个 Autowiring 的 bean 中。我有以下类(class)
我有一个带有存储库的 Spring Boot 应用程序。 我还使用@Service并扩展其中的存储库。 当我尝试 @Autowired 我拥有的服务时: Caused by: org.springfr
我有一个接口(interface)C,想要访问另外两个类中的getClassName()。访问 a.getClassName() 时,method1() 中出现异常。 public interface
我遇到了一个奇怪的问题,其中注入(inject)了 @Autowire 的 Component 在一个类中可用,但在另一个类中不可用。 我在Account和Agreement类的属性network中使
考虑以下示例代码: public class SmallCar { private CarEngine carEngine; @Autowired public SmallCa
autowire = "no"和 autowire = "default"有什么区别?如果它们相同,那么为什么我们有这 2 个选项。 最佳答案 Beans The default is "defaul
我已将项目更改为使用注释而不是 xml 文件,但这会增加应用程序部署时间。现在我正在寻找减少它的方法。 按类型 Autowiring 和按名称 Autowiring 之间有性能差异吗? 热烈欢迎任何其
我有一个与 Web 插件一起使用的 spring boot 应用程序。 在一节课中我有: package com.test.company @Component @RestController pub
我有一个可以执行某些操作的系统。该系统使用以下方法为每个对象创建一个单独的线程: stp.scheduleWithFixedDelay((EditSite) ctx.getBean("EditSite
我正在尝试自动连接存储库,但它无法工作。我已经为此苦苦挣扎了一个星期,但我似乎无法弄清楚。有趣的是,当我注释掉人员存储库的 Autowiring 时,程序可以正常工作并正确编译,但是一旦我尝试 Aut
意味着如果具有所需类型的 bean 不超过 1 个,bean 的所有字段将自动注入(inject)依赖项。 问题是当使用注解时它是如何工作的,它到底能不能工作。 我的测试表明即使我使用 @Resou
我有一个 Autowiring 其他 bean 的组件: @Component public class MyComponent { @Autowired private Enviro
这是我的类代码,其中有 @Autowired 字段: 测试A @ContextConfiguration("classpath:spring.xml") public abstract class T
我是一名优秀的程序员,十分优秀!