[英]Application run failed in springboot Unsatisfied dependency
我是 spring 開發的初學者。 我嘗試使用 Spring 啟動 2.4.1。 激活我為用戶創建的激活鏈接,但它給了我以下錯誤:
Error starting ApplicationContext.To display the conditions report re-run your application with 'debug' enabled.
2021-01-10 00:07:22.711 ERROR 9560 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject
這是我的代碼:(存儲庫)
@Repository
public interface UsersRepositor extends JpaRepository<Users,Integer> {
public Users findByEmail(String email);
public Users findByToken(String token);
@Query("update Users U set U.active=true where U.token =:token and u.id:id")
@Modifying
@Transactional
public void activeUsersByTokenAndId(@Param("token")String token,@param("id")int id);
}
這是我的代碼:(控制器)
@Controller
public class ActivationController {
@Autowired
UsersRepositor usersRepositor;
@GetMapping(value = "/activation/{token}")
public String activeUsersByToken(
@PathVariable("token")String token
){
Users users=usersRepositor.findByToken(token);
if (users !=null){
usersRepositor.activeUsersByTokenAndId(token,users.getId());
}
return "redirect:/login";
}
}
要使用 Spring 引導,最好的方法是使用/導入 spring 引導啟動器依賴項,請參閱 pom.xml 定義示例。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
在您的示例中,我進行了一些更改,請參見下文:
通常域的名稱 object 是單數,使用 User 而不是 Users
@Entity
public class User {
@Id
...
private Integer id;
private String token;
...
//Getters and Setters
}
更改存儲庫以刪除 @Transactional 並修復 @param(下)和查詢錯誤。
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
public User findByEmail(String email);
public User findByToken(String token);
@Query("update User u set u.active = true where u.token = :token and u.id = :id")
@Modifying
public void activeUserByTokenAndId(@Param("token")String token, @Param("id")int id);
}
添加服務實現
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(final UserRepository userRepository){
this.userRepository = userRepository;
}
@Transactional
public void activeUser(final String token){
User user= userRepository.findByToken(token);
if (users ==null){
// TODO throw exception like
// throw new UserNotFoundException();
}
this.userRepository.activeUserByTokenAndId(token, id);
}
}
更改 controller 以使用服務
@RestController
public class ActivationController {
private final UserService userService;
public ActivationController (final UserService userService){
this.userService = userService;
}
@GetMapping(value = "/activation/{token}")
public String activeUsersByToken(@PathVariable("token")String token){
userService.activeUser(token);
return "redirect:/login";
}
}
運行應用程序
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
公共 class ActivationController {
用戶存儲庫用戶存儲庫
@Autowired ActivationController(UsersRepositor usersRepositor){
this.usersRepositor=usersRepositor; }
相反,您嘗試構造函數注入
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.