当前位置: 首页 > 知识库问答 >
问题:

Spring boot security无法自动连接@Repository

巴帅
2023-03-14

当我在spring boot中添加安全配置时,遇到了这个令人讨厌的错误:

2017-05-18 15:23:29.160  WARN 1806 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'userDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsServiceImpl': Unsatisfied dependency expressed through field 'userRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#236c098' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#236c098': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'chatController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException

这是SpringBootApplication:

public class Ailab4finalApplication {

    public static void main(String[] args) {
        SpringApplication.run(Ailab4finalApplication.class, args);
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("validation");
        return messageSource;
    }
}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private Environment env;

    @Bean(name="passwordEncoder")
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .antMatchers("/webjars/**", "/css/**", "/registration", "/", "/home").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .and()
                .logout()
                    .permitAll();
        http.csrf().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(getPasswordEncoder());
    }
}
@Repository
public interface UserRepository extends  CrudRepository<UserHibernate, Long> {
   @Query("select u from UserHibernate u where u.email = ?1")
   public UserHibernate findByEmail(String email);

   @Query("select u from UserHibernate u where u.nickname = ?1")
   public UserHibernate findByNickname(String nickname);

   @Query("select u from UserHibernate u where u.id = ?1")
   public UserHibernate findById(Long id);
}

最后,这是我的项目树:

src:
   -Ailab4finalApplication.java
   -DatabaseConfig.java
   -SecurityConfig.java
src/services:
   -SecurityService.java
   -SecurityServiceImpl.java
   -UserDetailsServiceImpl.java
   -UserService.java
   -UserServiceImpl.java
src/jpa_repositories:
   -SecurityService.java
   -SecurityServiceImpl.java
   -UserDetailsServiceImpl.java
   -UserService.java
   -UserServiceImpl.java

共有1个答案

秦育
2023-03-14

我通过自动进入SecurityConff我使用的所有服务和存储库来解决这个问题。

 类似资料:
  • 我正在学习将activiti与spring boot结合使用,但我遇到了一些错误,并且搜索了太多关于使用spring boot和java的acitiviti稳定版本的信息 我正在使用java 8和tomcat 8.0.3 我更改了这么多版本的spring和jdk,检查了这么多样本,没有发现任何问题,我只有一个控制器类和spring主类,当我运行项目时,得到了以下错误: 这是我的RestContro

  • 这是我的FileStorageProperties类: 这让我想到:未通过@enableconfigurationproperties注册或标记为spring组件。 这是我的文件存储服务: 这给了我一个错误:无法自动关联未找到类型的bean。 这是我的项目结构: 当我试图运行它时,它给了我: 申请无法启动 描述: com中构造函数的参数0。穆阿。cse616.服务。FileStorageServi

  • 我正在使用java配置而不是XML迁移到spring Security4.0.1。当我自动操作时,它会给出以下错误: HTTP状态500-org.springframework.beans.factory.beanCreationException:创建名为“ScopedTarget.UsersComponent”的bean时出错:注入autowired依赖项失败;嵌套异常为org.springf

  • 这是我的当前设置:ProjectRepo: ProjectService: ProjectRestController:

  • 我的类使用@service,而bean已经自动连线,所以我不明白为什么会出现这个错误。

  • 我从spring-boot开始,我有一些配置问题。我无法自动操作某些服务。我得到一个BeanCreationException。 Hier是我自动执行服务的控制器: 控制台的错误: 当我从compenentscan中删除两个控制器时,我不会出现这个错误,但我需要扫描这些控制器来访问endpoint。