質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

9036閲覧

Springbootで理由の分からないエラーが発生しているのを解決したい

kusogomitan

総合スコア17

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2020/10/15 06:21

編集2020/10/15 06:45

SpringbootでSQLログを出したいのですが、

エラーメッセージ org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'realDataSource' defined in class path resource [com/example/AppConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.activation.DataSource]: Factory method 'realDataSource' threw exception; nested exception is java.lang.ClassCastException: class com.zaxxer.hikari.HikariDataSource cannot be cast to class javax.activation.DataSource (com.zaxxer.hikari.HikariDataSource and javax.activation.DataSource are in unnamed module of loader 'app')

というエラーがコンソールに表示されてしまい、実行する事が出来ません
正しく動くように直したいです。

App.java package com.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import com.example.domain.Customer; import com.example.service.CustomerService; @EnableAutoConfiguration @ComponentScan public class App implements CommandLineRunner{ @Autowired CustomerService customerService; @Autowired NamedParameterJdbcTemplate jdbcTemplate; @Override public void run(String...strings)throws Exception { String sql = "SELECT id, first_name, last_name From customers WHERE id = :id"; SqlParameterSource param = new MapSqlParameterSource().addValue("id", 1); Customer result = jdbcTemplate.queryForObject(sql, param, (rs,rowNum) -> new Customer(rs.getInt("id"), rs.getString("first_name"), rs.getString("last_name")) ); System.out.println("result = " + result); } public static void main(String[] args) { SpringApplication.run(App.class, args); } }
AppConfig.java package com.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import net.sf.log4jdbc.Log4jdbcProxyDataSource; import javax.activation.DataSource; @Configuration public class AppConfig { @Autowired DataSourceProperties datasourceProperties; javax.sql.DataSource datasource; @ConfigurationProperties(prefix = "spring.datasource") @Bean(destroyMethod = "close") DataSource realDataSource() { @SuppressWarnings("rawtypes") DataSourceBuilder factory = DataSourceBuilder .create(this.datasourceProperties.getClassLoader()) .url(this.datasourceProperties.getUrl()) .username(this.datasourceProperties.getUsername()) .password(this.datasourceProperties.getPassword()); this.datasource = factory.build(); return (DataSource) this.datasource; } @Primary @Bean Log4jdbcProxyDataSource datasource() { return new Log4jdbcProxyDataSource(this.datasource); } }
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>hajiboot-layering</artifactId> <version>1.0.0-SNAPSHOT</version> <name>hajiboot-layering</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.lazyluke</groupId> <artifactId>log4jdbc-remix</artifactId> <version>0.2.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version><!--$NO-MVN-MAN-VER$--> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kusogomitan

2020/10/15 06:35

すみません、おっしゃる通りです。修正いたしました
guest

回答1

0

ベストアンサー

import javax.activation.DataSource; は不要です。
import javax.sql.DataSource; ではないかと。

理由:return (DataSource) this.datasource; のところで、javax.activation.DataSourceにキャストしてしまうからです。

投稿2020/10/15 11:15

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問