我正在尝试在Spring Boot中创建mysql数据源,但显示此错误
org.springframework.jmx.export.UnableToRegisterMBeanException:无法使用键“ getDataSource”注册MBean [org.apache.tomcat.dbcp.dbcp2.BasicDataSource@27e2d373];嵌套的异常是javax.management.InstanceAlreadyExistsException:Catalina:type = DataSource,class = javax.sql.DataSource,name =“ jdbc / otp1”
这是tomcat server.xml
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
maxActive="20"
global="jdbc/otp1"
maxIdle="0"
maxWait="10000"
name="jdbc/otp1"
password="root"
username="root"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/test"/>
tomcat context.xml
<ResourceLink name="jdbc/otp"
global="jdbc/otp1"
auth="Container" type="javax.sql.DataSource" />
enter code here
我的pom.xml
<dependencies>
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>9.0.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
这是我的Java代码
@Configuration
公共类ApiConfig {
@Bean
public DataSource getDataSource(){
JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource=dsLookup.getDataSource("jdbc/otp");
return dataSource;
}
}