创建名称为'securityConfig'的bean时出错,自动连接依赖项的注入失败;异常java.lang.IllegalArgumentException

时间:2018-09-10 07:08:40

标签: java spring-boot spring-security

我正在做图书馆管理系统的Spring Boot安全应用程序。当我运行我的应用程序时,出现了上述错误。有人可以帮我吗。预先感谢!

// Securityconfig

@Configuration
@EnableWebSecurity
public class SecurityConfiguration  extends WebSecurityConfigurerAdapter{
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private DataSource dataSource;
    @Value("${spring.queries.users-query}")
    private String usersQuery;
    @Value("${spring.queries.roles-query}")
    private String rolesQuery;
    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.
            jdbcAuthentication()
            .usersByUsernameQuery(usersQuery)
            .authoritiesByUsernameQuery(rolesQuery)
            .dataSource(dataSource)
                .passwordEncoder(bCryptPasswordEncoder);
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.
            authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/login").permitAll()
            .antMatchers("/registration").permitAll()
            .antMatchers("/admin/**").hasAuthority("ADMIN").anyRequest()
            .authenticated().and().csrf().disable().formLogin()
            .loginPage("/login").failureUrl("/login?error=true")
            .defaultSuccessUrl("/admin/home")
            .usernameParameter("email")
            .passwordParameter("password")
            .and().logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/").and().exceptionHandling()
            .accessDeniedPage("/access-denied");
    }
    @Override
    public void configure(WebSecurity web) throws Exception {
        web
           .ignoring()
           .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**");
    }
}

// ServiceImpl

@Service
public class StudentServiceImpl implements StudentService {
    private StudentRepository stuRepository;
    public StudentServiceImpl(StudentRepository stuRepository) {
        this.stuRepository=stuRepository;
    }
    @Autowired   
    private BCryptPasswordEncoder bCryptPasswordEncoder;        
    @Override
    public Student findStudentByEmail(String email) {
        return stuRepository.findByEmail(email);
    }
    @Override
    public void saveStudent(Student stu) {
        stu.setPassword(bCryptPasswordEncoder.encode(stu.getPassword()));
        stuRepository.save(stu);
    }
}

//错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.queries.users-query' in value "${spring.queries.users-query}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:378) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]

//如果要在属性文件中添加查询。我收到此错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'stuserv'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentServiceImpl' defined in file [C:\Users\Root\eclipse-workspace\manlib\target\classes\com\itcinfo\manlib\service\StudentServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentRepository': Post-processing of merged bean definition failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType;
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]

1 个答案:

答案 0 :(得分:4)

如果您阅读错误消息,您将看到

  

无法解析值“ $ {spring.queries.users-query}”中的占位符“ spring.queries.users-query”

您错过了属性文件spring.queries.users-query中的属性 添加它,错误应该消失

更新:

如果您阅读新的错误消息:

  

java.lang.NoSuchMethodError:javax.persistence.PersistenceContext.synchronization()Ljavax / persistence / SynchronizationType;

您会看到一个异常,显示不兼容的jar文件