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

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

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

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Apache Tomcat

Apache TomcatはApache Software Foundation (ASF)で開発されたオープンソースのWebコンテナです。

Q&A

解決済

1回答

3485閲覧

【Spring・DBCP2】(内容変更)BasicDataSourceが@Autowiredでインスタンス化できていない

退会済みユーザー

退会済みユーザー

総合スコア0

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Apache Tomcat

Apache TomcatはApache Software Foundation (ASF)で開発されたオープンソースのWebコンテナです。

0グッド

0クリップ

投稿2019/06/28 13:22

編集2019/06/29 14:04

前提・実現したいこと

Spring Securityで、テーブルの登録内容を利用してログインできるようにしたい。

発生している問題・エラーメッセージ

Tomcatの起動時に下記のエラーが発生しています。
なお、IDEはeclipseで、Tomcatはeclipseに登録したものを起動しています。

※(6/29追記)はじめは、MyBatis-Springの@MapperScanが正常動作していないと考え投稿した質問だったのですが、それ以前の、BasicDataSourceを@Autowiredでインスタンス化する段階でエラーになっていることがわかりました。
そのため、内容を全面的に変更させていただきます。

なお、エラーメッセージを全て掲載しようとしたところ文字数オーバーとなってしまったため、スタックトレースを一部省略させていただきました。ご容赦ください。

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.apache.commons.dbcp2.BasicDataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ... 40 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.apache.commons.dbcp2.BasicDataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1213) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ... 45 more

該当のソースコード

下記のような構成です。

  • DataSourceの設定クラス:test.spring.config.DataSourceConfig
  • Spring Securityの設定クラス:test.spring.config.WebSecurityConfig
  • 社員情報テーブル

####DataSourceConfig

package spring.test.config; import org.apache.commons.dbcp2.BasicDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.mybatis.spring.annotation.MapperScan; @Configuration @MapperScan("org.step.kintai.mapper") @EnableTransactionManagement public class DataSourceConfig { @Bean public BasicDataSource dataSource() { BasicDataSource basicDataSource = new BasicDataSource(); basicDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); basicDataSource.setUrl( "jdbc:mysql://localhost:3306/holiday?characterEncoding=UTF-8&serverTimezone=JST&useSSL=false"); basicDataSource.setUsername("admin01"); basicDataSource.setPassword("Step2911"); return basicDataSource; } }

####WebSecurityConfig

package spring.test.config; import org.apache.commons.dbcp2.BasicDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import org.springframework.security.web.util.matcher.RequestMatcher; @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(10); } @Bean public RequestMatcher logoutRequestMatcher() { return new AntPathRequestMatcher("/logout"); } @Autowired private BasicDataSource dataSource; @Autowired public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.jdbcAuthentication().dataSource(dataSource) .authoritiesByUsernameQuery( "select mail_address as username, 'USER' as authority from personnel " + " where personnel_id = ?") .usersByUsernameQuery( "select mail_address as username, password, true as enabled from personnel " + " where personnel_id = ?") .passwordEncoder(passwordEncoder()); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/login", "/logout").permitAll().anyRequest() .authenticated(); http.formLogin().loginPage("/login"); http.logout().logoutRequestMatcher(logoutRequestMatcher()).invalidateHttpSession(true).and() .csrf(); } }

personnelテーブル(必要なカラムのみ)

mysql> SHOW COLUMNS FROM personnel;

FieldTypeNullKeyDefaultExtra
personnel_idsmallint(4) unsigned zerofillNOPRINULLauto_increment
mail_addressvarchar(254)NONULL
passwordvarchar(100)NONULL

17 rows in set (0.08 sec)

試したこと

エラーメッセージから、

  • WebSecurityConfigクラスインスタンスが生成できていない
  • BasicDataSourceクラスのBean定義が出来ていない

ことは分かったのですが、その原因や対処法が全く分かりませんでした。
こちらでは上記のソースコード・テーブル定義の情報で十分であると考えましたが、上記以外に必要なものなどありましたらご指摘ください。

補足情報(FW/ツールのバージョンなど)

JDK: OpenJDK 11.0.2
Spring Framework 5.1.5.RELEASE
アプリケーションサーバ:Tomcat 9.0.17
RDBMS: MySQL 8.0.16

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

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

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

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

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

guest

回答1

0

ベストアンサー

なかなか回答が頂けなかったため、他所で質問して解決しました。

WebSecurityConfigクラスに"@ComponentScan("spring.test")"の記載がなかったため、DataSourceConfigクラスをConfigurationクラスとして認識出来ていなかったようです。

投稿2019/06/30 12:21

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問