找不到 Bean Spring 启动
Bean that could not be found Spring Boot
我在尝试使用标记化启动 Spring 启动应用程序时遇到问题。这是我的服务 class:
@Service
@Slf4j
public class JwtTokenService {
private final JwtConfig jwtConfig;
public JwtTokenService(JwtConfig jwtConfig) {
this.jwtConfig = jwtConfig;
}
public String generateToken(Authentication authentication) {
Long now = System.currentTimeMillis();
return Jwts.builder()
.setSubject(authentication.getName())
.claim("authorities", authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority).collect(Collectors.toList()))
.setIssuedAt(new Date(now))
.setExpiration(new Date(now + jwtConfig.getExpiration() * 1000))
.signWith(SignatureAlgorithm.HS512, jwtConfig.getSecret().getBytes())
.compact();
}
}
这是我的配置 class:
@Data
@NoArgsConstructor
@Component
public class JwtConfig {
@Value("${security.jwt.uri:/auth/**}")
private String Uri;
@Value("${security.jwt.header:Authorization}")
private String header;
@Value("${security.jwt.prefix:Bearer }")
private String prefix;
@Value("${security.jwt.expiration:#{24*60*60}}")
private int expiration;
@Value("${security.jwt.secret:JwtSecretKey}")
private String secret;
}
当我尝试 运行 我的应用程序时出现以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in services.JwtTokenService required a bean of type 'config.JwtConfig' that could not be found.
Action:
Consider defining a bean of type 'config.JwtConfig' in your configuration.
我不明白为什么会出现此错误。
我解决了。问题是我的 spring 启动应用程序没有扫描配置所在的包。在我的@SpringBootApplication 中,我在 JwtConfig 所在的包中添加了 @ComponentScan。
我在尝试使用标记化启动 Spring 启动应用程序时遇到问题。这是我的服务 class:
@Service
@Slf4j
public class JwtTokenService {
private final JwtConfig jwtConfig;
public JwtTokenService(JwtConfig jwtConfig) {
this.jwtConfig = jwtConfig;
}
public String generateToken(Authentication authentication) {
Long now = System.currentTimeMillis();
return Jwts.builder()
.setSubject(authentication.getName())
.claim("authorities", authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority).collect(Collectors.toList()))
.setIssuedAt(new Date(now))
.setExpiration(new Date(now + jwtConfig.getExpiration() * 1000))
.signWith(SignatureAlgorithm.HS512, jwtConfig.getSecret().getBytes())
.compact();
}
}
这是我的配置 class:
@Data
@NoArgsConstructor
@Component
public class JwtConfig {
@Value("${security.jwt.uri:/auth/**}")
private String Uri;
@Value("${security.jwt.header:Authorization}")
private String header;
@Value("${security.jwt.prefix:Bearer }")
private String prefix;
@Value("${security.jwt.expiration:#{24*60*60}}")
private int expiration;
@Value("${security.jwt.secret:JwtSecretKey}")
private String secret;
}
当我尝试 运行 我的应用程序时出现以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in services.JwtTokenService required a bean of type 'config.JwtConfig' that could not be found.
Action:
Consider defining a bean of type 'config.JwtConfig' in your configuration.
我不明白为什么会出现此错误。
我解决了。问题是我的 spring 启动应用程序没有扫描配置所在的包。在我的@SpringBootApplication 中,我在 JwtConfig 所在的包中添加了 @ComponentScan。