gpt4 book ai didi

java - org.o7planning.aboutcompany.MainController 中的字段 dataSource 需要类型为 'javax.sql.DataSource' 的 bean,但无法找到

转载 作者:行者123 更新时间:2023-12-02 02:12:50 27 4
gpt4 key购买 nike

我引用了网址:https://o7planning.org/en/11727/understanding-spring-cloud-config-client-with-example没有任何自定义,但尝试简单地运行面临以下错误的代码。谁能指导我有什么问题吗?

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-04-08 23:45:00.960 ERROR 5476 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

***************************
APPLICATION FAILED TO START
***************************

Description:

Field dataSource in org.o7planning.aboutcompany.MainController required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'


Action:

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

这里是引用代码bootstrap.properties

spring.application.name=app-about-company
spring.cloud.config.uri=http://localhost:8888
management.security.enabled=false

application.properties

server.port=7777

主 Controller

@RefreshScope
@RestController
public class MainController {
@Value("${text.copyright: Default Copyright}")
private String copyright;

@Value("${spring.datasource.driver-class-name}")
private String driverClassName;

@Value("${spring.datasource.url}")
private String url;

@Value("${spring.datasource.username}")
private String userName;

@Value("${spring.datasource.password}")
private String password;

@Autowired
private DataSource dataSource;

@RequestMapping("/showConfig")
@ResponseBody
public String showConfig() {
String configInfo = "Copy Right: " + copyright //
+ "<br/>spring.datasource.driver-class-name=" + driverClassName //
+ "<br/>spring.datasource.url=" + url //
+ "<br/>spring.datasource.username=" + userName //
+ "<br/>spring.datasource.password=" + password;

return configInfo;
}

@RequestMapping("/pingDataSource")
@ResponseBody
public String pingDataSource() {
try {
return this.dataSource.toString();
} catch (Exception e) {
e.printStackTrace();
return "Error: " + e.getMessage();
}
}
}

pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.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>
<spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

最佳答案

您缺少一个DataSource bean。根据错误消息,此 bean 的自动注册失败:

- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'

根据上述内容,您很可能需要包含 javax.transaction:javax.transaction-api:1.2 依赖项来提供 javax.transaction.TransactionManager 类。根据您读取 spring.datasource.* 连接属性的代码,您没有使用 JNDI。

您可以通过运行mvn dependency:tree来检查您是否已经具有此依赖项。由于您没有提供完整的 pom.xml,因此很难判断您已经拥有哪些依赖项。

关于java - org.o7planning.aboutcompany.MainController 中的字段 dataSource 需要类型为 'javax.sql.DataSource' 的 bean,但无法找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49721214/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com