我正在尝试创建我的第一个JavaSpring安全应用程序,但遇到以下错误:
Consider defining a bean of type 'de.JBR.mongo.models.UserService' in your configuration.
这是我的Sprinboot应用程序类:
@ComponentScan({ "de.JBR.mongo.repositry", "de.JBR.mongo.api.models", "security.config", "security"})
@EnableMongoRepositories("de.JBR.mongo")
@SpringBootApplication
@AllArgsConstructor
public class EssensManagerApplication {
private final UserRepositry repositry;
public static void main(String[] args) {
SpringApplication.run(EssensManagerApplication.class, args);
}}
用户服务类:
@AllArgsConstructor
@Service
public class UserService implements UserDetailsService{
private final static String USER_NOT_FOUND = "Nutzer mit Name %s nicht gefunden";
private final UserRepositry repositry;
@Override
@Bean
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return repositry.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException(String.format(USER_NOT_FOUND, username)));
}
}
1条答案
按热度按时间rqqzpn5f1#
由于您添加了一个
@ComponentScan
,而de.JBR.mongo.models
不在包列表中,因此Spring不会自动创建您的服务。因此,请在配置文件中创建一个@Bean
,或者将该包添加到@ComponentScan
列表中。