Java 创建bean时出错。通过构造函数参数0表示的未满足依赖关系;
现在获取此错误:Java 创建bean时出错。通过构造函数参数0表示的未满足依赖关系;,java,spring,maven,spring-boot,Java,Spring,Maven,Spring Boot,现在获取此错误: Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-06-24 15:55:37.651 ERROR 6924 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed org
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-06-24 15:55:37.651 ERROR 6924 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deliveryController' defined in file [D:\Max\JavaWorks\Diplom\OnlineShop\target\classes\com\maximmalikov\onlineshop\rest\DeliveryController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deliveryService' defined in file [D:\Max\JavaWorks\Diplom\OnlineShop\target\classes\com\maximmalikov\onlineshop\service\DeliveryService.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ordersRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.maximmalikov.onlineshop.domain.Orders com.maximmalikov.onlineshop.repository.OrdersRepository.getByUserId(com.maximmalikov.onlineshop.domain.Users)! No property userId found for type Orders! Did you mean 'users'?
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:732) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at...
Process finished with exit code 0
My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maximmalikov</groupId>
<artifactId>onlineshop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>onlineshop</name>
<description>Onlineshop project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
该项目是用Spring Boot配置的,所有配置都是通过注释单独配置的,也就是说,我在Internet上看到了很多相同的问题,但是所有的bin设置都是在xml文件中配置的。这种方法不适合本项目。帮助理解此错误,以及如何在不使用xml的情况下调整箱子。查看您的pom.xml,我注意到您有
spring boot starter数据jpa
以及hibernate entitymanager 5.2.17版。最终版
SpringBootStarter数据jpa将引入您需要的所有hibernate依赖项
因此,从pom.xml中删除此依赖项,它应该可以工作
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.17.Final</version>
</dependency>
到
而且
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, WebMvcAutoConfiguration.class})
@SpringBootApplication
public class OnlineshopApplication {
我不知道为什么您排除了@EnableAutoConfiguration,但您可以试试这个一切正常,谢谢大家的帮助。我通过删除@EnableAutoConfiguration异常解决了这个问题。OrdersRepository中有一个编写不正确的方法,需要分别从服务和控制器中删除。伙计,看起来你缺少了一些spring注释,在我的例子中,它是我的Repository类上的一个简单的@Repository标记 我在这里找到了一些信息: 还有一种情况,当您有多个bean满足一个操作时。。 在这种情况下,您可以在应用程序类中添加此注释:
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.demo.yourpackage"}) //this line**
public class SpringDependenciesExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDependenciesExampleApplication.class, args);
}
}
尝试使用
@PersistenceContext-private-EntityManager-em代码>为您服务。在这里,这种方法是无用的,因为我不需要重新定义接口方法,并返回entitymanager,因为我通过直接访问模型的D T O引用存储库。向我们展示您的类GoodsRepository。我添加了这种嗜好,只是因为在programm中没有找到实体管理器。删除依赖项后,错误仍然保持不变。是否可以尝试从OnlineshopApplication类中删除@EnableJpaRepositories(basePackages=“com.maximmalikov.onlineshop.repository”)@EnableTransactionManagement。当SpringBoot在pom中找到SpringBootStarter数据jpa时,默认情况下会添加这些配置。当自动配置开始出现故障时,我开始添加注释。最初,只有@SpringBootApplication。使用SpringBoot的原因是它可以自动处理很多配置。我也有带存储库的SpringBoot设置,但我只有@SpringBootApplication。您需要查看删除并相应地调试后出现的错误。我确信你在某些地方弄乱了配置。我删除了你建议的两个注释,现在我发出了一个不同的类似错误。我编辑我的问题,你会看到
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CharacteristicsService {
private final GoodsRepository goodsRepository;
@Service
public class CharacteristicsService {
@AutoWired
private final GoodsRepository goodsRepository;
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, WebMvcAutoConfiguration.class})
@SpringBootApplication
public class OnlineshopApplication {
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.demo.yourpackage"}) //this line**
public class SpringDependenciesExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDependenciesExampleApplication.class, args);
}
}