maven 在Sping Boot JPA for MySQL中为我的所有类创建Bean时出错?

flvtvl50  于 2024-01-06  发布在  Maven
关注(0)|答案(2)|浏览(254)

我试图设置一个简单的端点来访问MySQL数据库,使用一个MySQL Account()方法,但似乎Sping Boot 无法通过我的所有类连接/创建bean。
下面是代码的详细信息:

MykitchenresourcesApplication

  1. package com.it326.mykitchenresources;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.autoconfigure.domain.EntityScan;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  7. @SpringBootApplication
  8. @ComponentScan(basePackages = "com.it326.mykitchenresources.*")
  9. @EntityScan("com.it326.mykitchenresources.*")
  10. @EnableJpaRepositories("com.it326.mykitchenresources.*")
  11. public class MykitchenresourcesApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(MykitchenresourcesApplication.class, args);
  14. }
  15. }

字符串

账户管理员

  1. package com.it326.mykitchenresources.controllers;
  2. // Imports
  3. @RestController
  4. @RequestMapping("/account")
  5. public class AccountController {
  6. private final AccountService accountService;
  7. @Autowired
  8. public AccountController(AccountService accountService) {
  9. this.accountService = accountService;
  10. }
  11. @RequestMapping("/hello")
  12. public String hello() {
  13. return "Hello, world!";
  14. }
  15. @PostMapping("/create")
  16. public ResponseEntity<String> createAccount(@RequestBody AccountDTO accountDTO) {
  17. // Retrieve values from the DTO (Data Transfer Object)
  18. String name = accountDTO.getName();
  19. String username = accountDTO.getUsername();
  20. String password = accountDTO.getPassword();
  21. // Create the account
  22. Account createdAccount = accountService.createAccount(name, username, password);
  23. if (createdAccount != null) {
  24. return ResponseEntity.status(HttpStatus.CREATED).body("Account created successfully");
  25. } else {
  26. return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to create account");
  27. }
  28. }
  29. }

账户服务

  1. package com.it326.mykitchenresources.services;
  2. // Imports
  3. @Service
  4. public class AccountService {
  5. @Autowired
  6. private final AccountDb accountDb;
  7. public AccountService(AccountDb accountDb) {
  8. this.accountDb = accountDb;
  9. }
  10. public Account createAccount(String name, String username, String password) {
  11. Account account = new Account();
  12. account.setName(name);
  13. account.setUserName(username);
  14. account.setHashedPassword(hashPassword(password));
  15. return accountDb.save(account);
  16. }
  17. private String hashPassword(String password) {
  18. // Generate a salt and hash the password using BCrypt
  19. String salt = BCrypt.gensalt();
  20. return BCrypt.hashpw(password, salt);
  21. }
  22. }

AccountDb(我的仓库类)

  1. package com.it326.mykitchenresources.dbs;
  2. // Imports
  3. @Repository
  4. public interface AccountDb extends JpaRepository<Account, Integer> {
  5. Account findByUsername(String username);
  6. }

账户(我的实体类)

  1. package com.it326.mykitchenresources.entities;
  2. // Imports
  3. @Entity
  4. @Table(name = "accounts")
  5. public class Account {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. @Column(name = "account_id")
  9. private int accountId;
  10. @Column(name = "name", length = 45)
  11. private String name;
  12. @Column(name = "username", length = 45)
  13. private String username;
  14. @Column(name = "hashed_password", length = 45)
  15. private String hashedPassword;
  16. // Constructors, getters, setters, etc.
  17. public Account(){}
  18. public Account(int accountId, String name, String username, String hashedPassword){
  19. this.accountId = accountId;
  20. this.name = name;
  21. this.username = username;
  22. this.hashedPassword = hashedPassword;
  23. }
  24. public int getAccountId() {
  25. return accountId;
  26. }
  27. public void setAccountId(int accountId) {
  28. this.accountId = accountId;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36. public String getUserName() {
  37. return username;
  38. }
  39. public void setUserName(String username) {
  40. this.username = username;
  41. }
  42. public String getHashedPassword() {
  43. return hashedPassword;
  44. }
  45. public void setHashedPassword(String hashedPassword) {
  46. this.hashedPassword = hashedPassword;
  47. }
  48. }

应用.属性

  1. spring.datasource.url= // My URL is here.
  2. spring.datasource.username= // Same with my username
  3. spring.datasource.password= // And my password
  4. server.port=8081
  5. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  6. spring.jpa.hibernate.ddl-auto=update

pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.1.5</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.it326</groupId>
  12. <artifactId>mykitchenresources</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>mykitchenresources</name>
  15. <description>Backend for myKitchen</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-jpa</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-mail</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. <!-- Password Hashing Dependency -->
  33. <dependency>
  34. <groupId>org.mindrot</groupId>
  35. <artifactId>jbcrypt</artifactId>
  36. <version>0.4</version> <!-- or the latest version available -->
  37. </dependency>
  38. <!-- Database Connection Dependencies -->
  39. <dependency>
  40. <groupId>com.mysql</groupId>
  41. <artifactId>mysql-connector-j</artifactId>
  42. <scope>runtime</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.hibernate</groupId>
  46. <artifactId>hibernate-core</artifactId>
  47. <version>5.5.7.Final</version> <!-- Use the appropriate version -->
  48. </dependency>
  49. <!-- Testing Dependencies -->
  50. <dependency>
  51. <groupId>junit</groupId>
  52. <artifactId>junit</artifactId>
  53. <version>4.12</version>
  54. <scope>test</scope>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-starter-test</artifactId>
  59. <scope>test</scope>
  60. </dependency>
  61. </dependencies>
  62. <build>
  63. <plugins>
  64. <plugin>
  65. <groupId>org.springframework.boot</groupId>
  66. <artifactId>spring-boot-maven-plugin</artifactId>
  67. <configuration>
  68. <image>
  69. <builder>paketobuildpacks/builder-jammy-base:latest</builder>
  70. </image>
  71. </configuration>
  72. </plugin>
  73. </plugins>
  74. </build>
  75. </project>


我试过把我所有的类都放到主应用程序的同一个包中,但似乎这并不能解决我的问题。
我还尝试了不带任何类的mvn clean install,以检查我的主应用程序是否正常工作,它工作得很好。

我得到的日志和错误:

  1. o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization -
  2. cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountController' defined in file [C:\Users\...\target\classes\com\it326\mykitchenresources\controllers\AccountController.class]:
  3. Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'accountService' defined in file [C:\Users\...\target\classes\com\it326\mykitchenresources\services\AccountService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'accountDb' defined in com.it326.mykitchenresources.dbs.AccountDb defined in @EnableJpaRepositories declared on MykitchenresourcesApplication: Not a managed type: class com.it326.mykitchenresources.entities.Account


这是我第一次自己建立Sping Boot 项目,所以我可能会错过一些非常明显的东西,并盯着我的脸;任何帮助和指点都将非常感谢!谢谢!

o8x7eapl

o8x7eapl1#

正如在注解中已经指出的,你应该删除hibernate-core的显式依赖。这一个:

  1. <dependency>
  2. <groupId>org.hibernate</groupId>
  3. <artifactId>hibernate-core</artifactId>
  4. <version>5.5.7.Final</version> <!-- Use the appropriate version -->
  5. </dependency>

字符串
完成后,你应该检查你的类中使用的所有导入。特别是实体类。不应该有来自javax.persistence包的导入。只有来自jakarta.persistence的导入。否则你会遇到对象不匹配和错误,比如:not a managed type

polkgigr

polkgigr2#

您忘记在AccountService的构造函数中自动连接AccountDB

  1. @Service
  2. public class AccountService {
  3. private final AccountDb accountDb;
  4. @Autowired // <-- Added a @Autowired annotation.
  5. public AccountService(AccountDb accountDb) {
  6. this.accountDb = accountDb;
  7. }
  8. public Account createAccount(String name, String username, String password) {
  9. Account account = new Account();
  10. account.setName(name);
  11. account.setUserName(username);
  12. account.setHashedPassword(hashPassword(password));
  13. return accountDb.save(account);
  14. }
  15. private String hashPassword(String password) {
  16. // Generate a salt and hash the password using BCrypt
  17. String salt = BCrypt.gensalt();
  18. return BCrypt.hashpw(password, salt);
  19. }
  20. }

字符串

展开查看全部

相关问题