我是Spring的新手。我已经遵循了tutorial
当前,我们在z / OS上使用DB2,因此我尝试使用与连接H2的教程相同的方式连接到DB2。但是经过一番努力,我成功地使用Maven安装了Jar文件。但是当我尝试运行此Jar时,出现此错误
申请无法开始
说明:
构造函数的参数Parameter ess.nbe.dev.essentis.services.Tgen008Service需要类型为bean的服务 找不到“ ess.nbe.dev.essentis.repo.Tgen008存储库”。
操作:
考虑定义一个类型的bean 配置中的“ ess.nbe.dev.essentis.repo.Tgen008存储库”。
我认为项目结构符合Spring要求disccussed here 。
我按照说明进行操作,我不知道出了什么问题?
这是我的代码
package ess.nbe.dev.essentis.domain;
import org.springframework.context.annotation.Bean;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class Tgen008 implements Serializable {
@Id
private String ACT_IDN_SKY;
@Column
private String ACT_RFE_NBR_TXT;
public Tgen008(String ACT_IDN_SKY, String ACT_RFE_NBR_TXT) {
this.ACT_IDN_SKY = ACT_IDN_SKY;
this.ACT_RFE_NBR_TXT = ACT_RFE_NBR_TXT;
}
public Tgen008() {
}
public String getACT_IDN_SKY() {
return ACT_IDN_SKY;
}
public void setACT_IDN_SKY(String ACT_IDN_SKY) {
this.ACT_IDN_SKY = ACT_IDN_SKY;
}
public String getACT_RFE_NBR_TXT() {
return ACT_RFE_NBR_TXT;
}
public void setACT_RFE_NBR_TXT(String ACT_RFE_NBR_TXT) {
this.ACT_RFE_NBR_TXT = ACT_RFE_NBR_TXT;
}
}
这是我的存储库:
package ess.nbe.dev.essentis.repo;
import ess.nbe.dev.essentis.domain.Tgen008;
import org.springframework.data.repository.CrudRepository;
public interface Tgen008Repository extends CrudRepository<Tgen008,String> {
}
这是我的服务
package ess.nbe.dev.essentis.services;
import ess.nbe.dev.essentis.repo.Tgen008Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Tgen008Service {
private Tgen008Repository tgen008Repository;
@Autowired
public Tgen008Service(Tgen008Repository tgen008Repository) {
this.tgen008Repository = tgen008Repository;
}
public long total() { return tgen008Repository.count();}
}
和我的MainApplication:
package ess.nbe.dev.essentis;
import ess.nbe.dev.essentis.repo.Tgen008Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.Arrays;
@SpringBootApplication
public class EssentisApplication {
public static void main(String[] args) {
SpringApplication.run(EssentisApplication.class, args);
}
@Bean
CommandLineRunner init(Tgen008Repository tgen008Repository) {
return args -> tgen008Repository.count();
}
}
和Application.properties:
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring.datasource.url=jdbc:db2://ipAddress:port/DBName
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.properties.hibernate.default_schema=Qualifier
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=select 1 from tgen008 fetch first row only
# Naming strategy
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DB2Dialect
和我的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>ess.nbe.dev</groupId>
<artifactId>essentis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>essentis</name>
<description>Program for Essentis DB</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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>
<repositories>
<repository>
<id>com.ibm.db2.jcc</id>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>file://${project.basedir}/lib/</url>
</repository>
</repositories>
<dependencies>
<!-- <dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc</artifactId>
<version>10.1</version>
</dependency>-->
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc_license_cisuz</artifactId>
<version>10.1</version>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2java</artifactId>
<version>10.1</version>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc_javax</artifactId>
<version>10.1</version>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>10.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:0)
在以下位置更改服务
@Service
public class Tgen008Service {
@Autowired
private Tgen008Repository tgen008Repository;
public long total() { return tgen008Repository.count();}
}
答案 1 :(得分:0)
我发现了问题所在,我有2个jar文件db2jcc.jar
和db2jcc4.jar
。
两者都在我的存储库中,尽管我没有在db2jcc.jar
中添加它,但该程序似乎正在使用db2jcc4.jar
而不是pom.xml
。
因此从本地存储库中删除db2jcc.jar
即可解决问题。
有关两个Jar之间差异的更多详细信息,请参见this link
答案 2 :(得分:0)
package ess.nbe.dev.essentis.repo;
import ess.nbe.dev.essentis.domain.Tgen008;
import org.springframework.data.repository.CrudRepository;
@Repository // Need to add this
public interface Tgen008Repository extends CrudRepository<Tgen008, String> {
}