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

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

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

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

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

Q&A

解決済

1回答

4215閲覧

Spring bootのビルドエラー

退会済みユーザー

退会済みユーザー

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

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

0グッド

0クリップ

投稿2020/04/24 03:28

ログイン関連機能を実装しましたが、ビルド時にエラーが発生し
対象方法が不明のために質問させていただきます。

エラーメッセージ

ログは文字数制限上、各項目の上位の一部を除いて省略しています。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.example.todolist.entity.UserEntity com.example.todolist.repository.UserRepository.findByUsername(java.lang.String)! Unable to locate Attribute with the the given name [username] on this ManagedType [com.example.todolist.entity.UserEntity] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.example.todolist.entity.UserEntity com.example.todolist.repository.UserRepository.findByUsername(java.lang.String)! Unable to locate Attribute with the the given name [username] on this ManagedType [com.example.todolist.entity.UserEntity] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.example.todolist.entity.UserEntity com.example.todolist.repository.UserRepository.findByUsername(java.lang.String)! Unable to locate Attribute with the the given name [username] on this ManagedType [com.example.todolist.entity.UserEntity] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract com.example.todolist.entity.UserEntity com.example.todolist.repository.UserRepository.findByUsername(java.lang.String)! Unable to locate Attribute with the the given name [username] on this ManagedType [com.example.todolist.entity.UserEntity] at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:209) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE] Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [username] on this ManagedType [com.example.todolist.entity.UserEntity] at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.checkNotNull(AbstractManagedType.java:147) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final] at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:118) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]

bundle.gradle

plugins { id 'org.springframework.boot' version '2.2.6.RELEASE' id 'io.spring.dependency-management' version '1.0.9.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '11' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } compileOnly 'org.projectlombok:lombok:1.18.10' annotationProcessor 'org.projectlombok:lombok:1.18.10' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' compile files('lib/ojdbc8.jar') implementation 'org.springframework.boot:spring-boot-starter-security' compile('org.springframework.security:spring-security-web') compile('org.springframework.security:spring-security-config') compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity5') }

loginController

package com.example.todolist.controller; import com.example.todolist.form.SignupForm; import com.example.todolist.service.UserService; (省略) import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; @Controller public class loginController { @Autowired UserService userService; @GetMapping("/signup") public String signup(Model model) { model.addAttribute("signupForm", new SignupForm()); return "signup"; } @PostMapping("/signup") public String signupPost(Model model, @Valid SignupForm signupForm, BindingResult bindingResult, HttpServletRequest request) { if (bindingResult.hasErrors()) { return "signup"; } try { userService.registerUser(signupForm.getUsername(), signupForm.getPassword(), signupForm.getMailAddress()); } catch (DataIntegrityViolationException e) { model.addAttribute("signupError", true); return "signup"; } try { request.login(signupForm.getUsername(), signupForm.getPassword()); } catch (ServletException e) { e.printStackTrace(); } return "redirect:/messages"; } @RequestMapping("/") public String index() { return "redirect:/messages"; } @RequestMapping("/messages") public String messages() { return "messages"; } @GetMapping("/login") public String login() { return "login"; } @PostMapping("/login") public String loginPost() { return "redirect:/login-error"; } @GetMapping("/login-error") public String loginError(Model model) { model.addAttribute("loginError", true); return "login"; } }

UserService

package com.example.todolist.service; import com.example.todolist.entity.UserEntity; import com.example.todolist.repository.UserRepository; (省略) import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class UserService implements UserDetailsService { @Autowired private UserRepository repository; @Autowired private PasswordEncoder passwordEncoder; @Override public UserEntity loadUserByUsername(String username) throws UsernameNotFoundException { if (username == null || "".equals(username)) { throw new UsernameNotFoundException("Username is empty"); } UserEntity user = repository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found: " + username); } return user; } @Transactional public void registerAdmin(String username, String password, String mailAddress) { UserEntity user = new UserEntity(username, passwordEncoder.encode(password), mailAddress); user.setAdmin(true); repository.save(user); } @Transactional public void registerUser(String username, String password, String mailAddress) { UserEntity user = new UserEntity(username, passwordEncoder.encode(password), mailAddress); repository.save(user); } }

調べてみるとパッケージ構成が悪くて、@SpringBootApplicationアノテーションが付いている
ソースから@serviceアノテーションを見つけられない場合
エラーになる現象もあるそうでしたので、パッケージ図も添付します。

私の場合も、上記のケースに該当するのでしょうか。
ご意見をお聞かせください。

パッケージ図

イメージ説明

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

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

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

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

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

rubytomato

2020/04/24 06:17

UserRepository、UserEntityに原因がありそうなのでこれらのソースコードの追記をお願いします。
退会済みユーザー

退会済みユーザー

2020/04/24 06:42

ご確認ありがとうございます。 WebSecurityConfigをtodolist直下に移動させたら上記のエラーは消えました。 ただ、別の問題が発生しているので、新しい質問を上げようと思います。 ありがとうございました。
guest

回答1

0

ベストアンサー

WebSecurityConfigをtodolist直下に移動させたら上記のエラーは消えました。

投稿2020/04/24 06:44

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問