質問編集履歴

3

誤字の修正。

2019/06/29 14:04

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -262,7 +262,7 @@
262
262
 
263
263
 
264
264
 
265
- * WebSecurityConfigクラスインスタンスが生成できてのBean定義が出来ていない
265
+ * WebSecurityConfigクラスインスタンスが生成できていない
266
266
 
267
267
  * BasicDataSourceクラスのBean定義が出来ていない
268
268
 

2

エラーの根本原因が絞り込めたため、内容修正。

2019/06/29 14:04

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- MyBatis Spring】@MapperScanの使用時に"No qualifying bean of type '〇〇' available"が発生
1
+ 【Spring・DBCP2(内容変更)BasicDataSourceが@Autowiredでインスタンス化できていない
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- MyBatis-Springを利用しタベースの内容を出力すREST APIを作成したい。
3
+ Spring Securityでブル登録内容を利用してログインできようにしたい。
4
4
 
5
5
 
6
6
 
@@ -12,11 +12,19 @@
12
12
 
13
13
 
14
14
 
15
- ```
16
-
17
- ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
18
-
19
- org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dbTestController': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'spring.test.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
15
+ ※(6/29追記)はじめは、MyBatis-Springの@MapperScanが正常動作していないと考え投稿した質問だったのですが、それ以前の、**BasicDataSourceを@Autowiredでインスタンス化する段階でエラーになっていることがわかりました。**
16
+
17
+ そのため、内容を全面的に変更させていただきます。
18
+
19
+
20
+
21
+ なお、エラーメッセージを全て掲載しようとしたところ文字数オーバーとなってしまったため、スタックトレースを一部省略させていただきました。ご容赦ください。
22
+
23
+ ```
24
+
25
+ ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
26
+
27
+ 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)}
20
28
 
21
29
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
22
30
 
@@ -30,95 +38,9 @@
30
38
 
31
39
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
32
40
 
33
- at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
34
-
35
- at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
36
-
37
- at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
38
-
39
- at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
40
-
41
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849)
42
-
43
- at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
44
-
45
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
46
-
47
- at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:701)
48
-
49
- at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:577)
50
-
51
- at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:529)
52
-
53
- at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:169)
54
-
55
- at javax.servlet.GenericServlet.init(GenericServlet.java:158)
56
-
57
- at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1123)
58
-
59
- at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:974)
60
-
61
- at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4868)
62
-
63
- at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184)
64
-
65
- at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
66
-
67
- at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1377)
68
-
69
- at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1367)
70
-
71
- at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
72
-
73
- at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
74
-
75
- at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140)
76
-
77
- at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:902)
78
-
79
- at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:831)
80
-
81
- at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
82
-
83
- at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1377)
84
-
85
- at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1367)
86
-
87
- at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
88
-
89
- at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
90
-
91
- at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140)
92
-
93
- at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:902)
94
-
95
- at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
96
-
97
- at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
98
-
99
- at org.apache.catalina.core.StandardService.startInternal(StandardService.java:423)
100
-
101
- at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
102
-
103
- at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:928)
104
-
105
- at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
106
-
107
- at org.apache.catalina.startup.Catalina.start(Catalina.java:634)
108
-
109
- at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
110
-
111
- at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
112
-
113
- at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
114
-
115
- at java.base/java.lang.reflect.Method.invoke(Method.java:566)
116
-
117
- at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:350)
118
-
119
- at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:492)
120
-
121
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'spring.test.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
41
+ ... 40 more
42
+
43
+ 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)}
122
44
 
123
45
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654)
124
46
 
@@ -128,7 +50,7 @@
128
50
 
129
51
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
130
52
 
131
- ... 49 more
53
+ ... 45 more
132
54
 
133
55
  ```
134
56
 
@@ -138,17 +60,13 @@
138
60
 
139
61
  下記のような構成です。
140
62
 
141
- なお、文字数の関係上import文を省略しております。
63
+
142
-
143
-
144
-
64
+
145
- * 設定クラス:test.spring.config.DataSourceConfig
65
+ * DataSourceの設定クラス:test.spring.config.DataSourceConfig
146
-
66
+
147
- * コントローラ:test.spring.controller.DbTestController
67
+ * Spring Securityの設定ク:test.spring.config.WebSecurityConfig
148
-
68
+
149
- * DTO: test.spring.model.User
69
+ * 社員情報テーブル
150
-
151
- * Mapper: test.spring.mapper.UserMapper
152
70
 
153
71
  ####DataSourceConfig
154
72
 
@@ -158,11 +76,21 @@
158
76
 
159
77
 
160
78
 
79
+ import org.apache.commons.dbcp2.BasicDataSource;
80
+
81
+ import org.springframework.context.annotation.Bean;
82
+
83
+ import org.springframework.context.annotation.Configuration;
84
+
85
+ import org.springframework.transaction.annotation.EnableTransactionManagement;
86
+
87
+ import org.mybatis.spring.annotation.MapperScan;
88
+
89
+
90
+
161
91
  @Configuration
162
92
 
163
- @ComponentScan("spring.test")
164
-
165
- @MapperScan("spring.test.mapper")
93
+ @MapperScan("org.step.kintai.mapper")
166
94
 
167
95
  @EnableTransactionManagement
168
96
 
@@ -176,25 +104,73 @@
176
104
 
177
105
  BasicDataSource basicDataSource = new BasicDataSource();
178
106
 
179
- basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
107
+ basicDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
180
-
181
- basicDataSource.setUrl("jdbc:mysql://localhost/**");
108
+
182
-
183
- basicDataSource.setUsername(**);
109
+ basicDataSource.setUrl(
110
+
184
-
111
+ "jdbc:mysql://localhost:3306/holiday?characterEncoding=UTF-8&serverTimezone=JST&useSSL=false");
112
+
113
+ basicDataSource.setUsername("admin01");
114
+
185
- basicDataSource.setPassword(**);
115
+ basicDataSource.setPassword("Step2911");
186
116
 
187
117
  return basicDataSource;
188
118
 
189
119
  }
190
120
 
121
+ }
122
+
123
+
124
+
125
+ ```
126
+
127
+ ####WebSecurityConfig
128
+
129
+ ```
130
+
131
+ package spring.test.config;
132
+
133
+
134
+
135
+ import org.apache.commons.dbcp2.BasicDataSource;
136
+
137
+ import org.springframework.beans.factory.annotation.Autowired;
138
+
139
+ import org.springframework.context.annotation.Bean;
140
+
141
+ import org.springframework.context.annotation.Configuration;
142
+
143
+ import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
144
+
145
+ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
146
+
147
+ import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
148
+
149
+ import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
150
+
151
+ import org.springframework.security.crypto.password.PasswordEncoder;
152
+
153
+ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
154
+
155
+ import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
156
+
157
+ import org.springframework.security.web.util.matcher.RequestMatcher;
158
+
159
+
160
+
161
+ @Configuration
162
+
163
+ @EnableWebSecurity
164
+
165
+ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
166
+
191
167
 
192
168
 
193
169
  @Bean
194
170
 
195
- public DataSourceTransactionManager transactionManager() {
171
+ public PasswordEncoder passwordEncoder() {
196
-
172
+
197
- return new DataSourceTransactionManager(dataSource());
173
+ return new BCryptPasswordEncoder(10);
198
174
 
199
175
  }
200
176
 
@@ -202,15 +178,57 @@
202
178
 
203
179
  @Bean
204
180
 
205
- public SqlSessionFactoryBean sqlSessionFactory() {
181
+ public RequestMatcher logoutRequestMatcher() {
206
-
182
+
207
- SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
183
+ return new AntPathRequestMatcher("/logout");
184
+
208
-
185
+ }
186
+
187
+
188
+
189
+ @Autowired
190
+
209
- sessionFactoryBean.setDataSource(dataSource());
191
+ private BasicDataSource dataSource;
192
+
193
+
194
+
210
-
195
+ @Autowired
196
+
197
+ public void configure(AuthenticationManagerBuilder auth) throws Exception {
198
+
199
+ auth.jdbcAuthentication().dataSource(dataSource)
200
+
201
+ .authoritiesByUsernameQuery(
202
+
203
+ "select mail_address as username, 'USER' as authority from personnel "
204
+
205
+ + " where personnel_id = ?")
206
+
207
+ .usersByUsernameQuery(
208
+
209
+ "select mail_address as username, password, true as enabled from personnel "
210
+
211
+ + " where personnel_id = ?")
212
+
213
+ .passwordEncoder(passwordEncoder());
214
+
215
+ }
216
+
217
+
218
+
219
+ @Override
220
+
221
+ protected void configure(HttpSecurity http) throws Exception {
222
+
211
- sessionFactoryBean.setConfigLocation(new ClassPathResource("/mybatis-config.xml"));
223
+ http.authorizeRequests().antMatchers("/login", "/logout").permitAll().anyRequest()
212
-
224
+
213
- return sessionFactoryBean;
225
+ .authenticated();
226
+
227
+ http.formLogin().loginPage("/login");
228
+
229
+ http.logout().logoutRequestMatcher(logoutRequestMatcher()).invalidateHttpSession(true).and()
230
+
231
+ .csrf();
214
232
 
215
233
  }
216
234
 
@@ -218,159 +236,41 @@
218
236
 
219
237
  ```
220
238
 
221
- ####DbTestController
222
-
223
- ```
224
-
225
- package spring.test.controller;
226
-
227
-
228
-
229
- @RestController
230
-
231
- public class DbTestController {
232
-
233
- @Autowired
234
-
235
- private UserMapper userMapper;
236
-
237
-
238
-
239
- private static final Logger logger = LoggerFactory.getLogger(UserController.class);
240
-
241
-
242
-
243
- @GetMapping("/dbTest")
244
-
245
- public String userSelect(User user, Locale locale) {
246
-
247
- logger.info("[start]UserController:userSelect", locale);
248
-
249
- // user = new User((short) 1, "test@domain.jp", "sdfsdfsdf");
250
-
251
- user = userMapper.selectByPrimaryKey((short) 1);
252
-
253
- return user.getMailAddress();
254
-
255
- }
256
-
257
- }
258
-
259
-
260
-
261
- ```
262
-
263
-
264
-
265
- ####User(DTO)
266
-
267
- ```
268
-
269
- package spring.test.model;
270
-
271
-
272
-
273
- @Data
274
-
275
- @NoArgsConstructor
276
-
277
- @AllArgsConstructor
278
-
279
- public class User {
280
-
281
- private Short userId;
282
-
283
- private String mailAddress;
284
-
285
- private String password;
286
-
287
- }
288
-
289
- ```
290
-
291
- ####UserMapper
292
-
293
- ```
294
-
295
- package spring.test.mapper;
296
-
297
-
298
-
299
- @Mapper
300
-
301
- public interface UserMapper {
302
-
303
- @Select({"select", "user_id, mail_address, password", "from user",
304
-
305
- "where user_id = #{userId,jdbcType=SMALLINT}"})
306
-
307
- @Results({
308
-
309
- @Result(column = "user_id", property = "userId", jdbcType = JdbcType.SMALLINT, id = true),
310
-
311
- @Result(column = "mail_address", property = "mailAddress", jdbcType = JdbcType.VARCHAR),
312
-
313
- @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR)})
314
-
315
- User selectByPrimaryKey(Short userId);
316
-
317
- }
318
-
319
-
320
-
321
- ```
322
-
323
-
324
-
325
- ####mybatis-config
326
-
327
- ```
328
-
329
- <?xml version="1.0" encoding="UTF-8" ?>
330
-
331
- <!DOCTYPE configuration
332
-
333
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
334
-
335
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
336
-
337
- <configuration>
338
-
339
- <settings>
340
-
341
- <setting name="mapUnderscoreToCamelCase" value="true" />
342
-
343
- <setting name="defaultFetchSize" value="100" />
344
-
345
- </settings>
346
-
347
- <mappers>
348
-
349
- <package name="test.spring.mapper" />
350
-
351
- </mappers>
352
-
353
- </configuration>
354
-
355
- ```
356
-
357
-
239
+
240
+
241
+ #### personnelテーブル(必要なカラムのみ)
242
+
243
+ mysql> SHOW COLUMNS FROM personnel;
244
+
245
+ |Field|Type|Null|Key|Default|Extra|
246
+
247
+ |:--|:--|:--|:--|:--|:--|
248
+
249
+ | personnel_id | smallint(4) unsigned zerofill | NO | PRI | NULL | auto_increment |
250
+
251
+ | mail_address | varchar(254) | NO | | NULL | |
252
+
253
+ | password | varchar(100) | NO | | NULL |
254
+
255
+
256
+
257
+ 17 rows in set (0.08 sec)
358
258
 
359
259
  ### 試したこと
360
260
 
261
+ エラーメッセージから、
262
+
263
+
264
+
265
+ * WebSecurityConfigクラスインスタンスが生成できてのBean定義が出来ていない
266
+
361
- エラーメッセージから、UserMapperクラスのBean定義が出来ていないと判断しました。
267
+ * BasicDataSourceクラスのBean定義が出来ていない
362
-
363
- そこで、
268
+
364
-
365
- * UserMapperに@Repositoryや@Componentを付ける
269
+
366
-
367
- * コントローラに@Import(UserMapper.class)を追加する
270
+
368
-
369
-
370
-
371
- の二点試しましたが、上記と同じエラーメッセージ出力されてしいました。
271
+ こと分かっのですが、その原因や対処法全く分かりせんでした。
372
-
272
+
373
- 上記以外に必要なソースコードやプロパィファイルの内容などございましたらお申しつけください。
273
+ こちらでは上記ソースコードーブ定義情報で十分であると考えましたが、上記以外に必要ものなありましたらご指摘ください。
374
274
 
375
275
 
376
276
 
@@ -382,10 +282,6 @@
382
282
 
383
283
  Spring Framework 5.1.5.RELEASE
384
284
 
385
- MyBatis: 3.5.1
386
-
387
- MyBatis-Spring: 2.0.1
388
-
389
285
  アプリケーションサーバ:Tomcat 9.0.17
390
286
 
391
287
  RDBMS: MySQL 8.0.16

1

試したことについて、具体的な記載に改めました。

2019/06/29 05:55

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- 【MyBatis Spring】No qualifying bean of type '〇〇' available
1
+ 【MyBatis Spring】@MapperScanの使用時に"No qualifying bean of type '〇〇' available"が発生
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- MyBatis-Springを利用し、データベースの内容を出力するREST Controllerを作成したい。
3
+ MyBatis-Springを利用し、データベースの内容を出力するREST APIを作成したい。
4
4
 
5
5
 
6
6
 
@@ -358,9 +358,17 @@
358
358
 
359
359
  ### 試したこと
360
360
 
361
-
361
+ エラーメッセージから、UserMapperクラスのBean定義が出来ていないと判断しました。
362
+
362
-
363
+ そこで、
364
+
365
+ * UserMapperに@Repositoryや@Componentを付ける
366
+
363
- マッパーに@Repositoryや@Componentを付けたり、コントローラに@Import(UserMapper.class)を追加することは試しましたが、いずれも上手くいきませんでした。
367
+ * コントローラに@Import(UserMapper.class)を追加する
368
+
369
+
370
+
371
+ の二点は試しましたが、上記と同じエラーメッセージが出力されてしまいました。
364
372
 
365
373
  上記以外に必要なソースコードやプロパティファイルの内容などございましたらお申しつけください。
366
374