首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决org.springframework.beans.factory.BeanCreationNotAllowedException?问题

如何解决org.springframework.beans.factory.BeanCreationNotAllowedException?问题
EN

Stack Overflow用户
提问于 2021-02-08 05:24:49
回答 1查看 4.8K关注 0票数 1

我正在做一个youtube教程中的项目,并且被困在这个解决org.springframework.beans.factory.BeanCreationNotAllowedException?的Exception.How上。

我正在做一个youtube教程中的项目,并且被这个例外困住了。我在这里看到了类似的堆栈溢出问题,但是存在一个不正确的导入问题。我检查了我的进口,没有问题。我试图将该文件移动到根用户,然后再次捕获了相同的错误。我试着在youtube和网络上找到解决方案,但我没有得到任何东西。请帮我找出解决办法。

来源:https://github.com/DnyaneshwarKolhe/Bookstore.git

代码语言:javascript
复制
Exception in thread "task-2" org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'delegatingApplicationListener': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
            at org.springframework.context.event.AbstractApplicationEventMulticaster.retrieveApplicationListeners(AbstractApplicationEventMulticaster.java:245)
            at org.springframework.context.event.AbstractApplicationEventMulticaster.getApplicationListeners(AbstractApplicationEventMulticaster.java:197)
            at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:134)
            at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
            at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361)
            at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:99)
            at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.access$100(DataSourceInitializedPublisher.java:50)
            at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher$DataSourceSchemaCreatedPublisher.lambda$postProcessEntityManagerFactory$0(DataSourceInitializedPublisher.java:200)
            at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
            at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
            at java.base/java.lang.Thread.run(Thread.java:832)
        2021-02-08 09:47:56.074  INFO 10824 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
        2021-02-08 09:47:56.075  INFO 10824 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
        2021-02-08 09:47:56.094  INFO 10824 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
        2021-02-08 09:47:56.098  INFO 10824 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
        2021-02-08 09:47:56.112  INFO 10824 --- [           main] ConditionEvaluationReportLoggingListener : 
        
        Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
        2021-02-08 09:47:56.250 ERROR 10824 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
        
        ***************************
        APPLICATION FAILED TO START
        ***************************

        Description:
        
        Field userSecurityService in com.bookstore.config.SecurityConfig required a bean of type 'com.bookstore.service.impl.UserSecurityService' that could not be found.
        
        The injection point has the following annotations:
            - @org.springframework.beans.factory.annotation.Autowired(required=true)
    
    
        Action:
        
        Consider defining a bean of type 'com.bookstore.service.impl.UserSecurityService' in your configuration.

UserSecurityService.java

代码语言:javascript
复制
package com.bookstore.service.impl;
        
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
        
import com.bookstore.domain.User;
import com.bookstore.repository.UserRepository;
        
        
public class UserSecurityService implements UserDetailsService {
        
    @Autowired
    private UserRepository userRepository;
                
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            User user=userRepository.findByUsername(username);
                
                if(null == user) {
                    throw new UsernameNotFoundException("Username Not Found");
                }
                return user;
       }
        
 }

SecurityConfig.java

代码语言:javascript
复制
package com.bookstore.config;
        
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import com.bookstore.service.impl.UserSecurityService;
import com.bookstore.utility.SecurityUtility;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ComponentScan("com.bookstore.service.impl.UserSecurityService") 
public class SecurityConfig extends WebSecurityConfigurerAdapter{
        
    @Autowired
    private Environment env;
    
    @Autowired
    private UserSecurityService userSecurityService;
    
    private BCryptPasswordEncoder passwordEncoder() {
                return SecurityUtility.passwordEncoder();
    }
            
     private static final String[] PUBLIC_MATCHERS = {
                "/css/**",
                "/js/**",
                "/image/**",
                "/",
                "/myAccount"
    };
            
    @Override
    protected void configure(HttpSecurity http) throws Exception{
                http
                        .authorizeRequests().
                        /*antMatchers("/**").*/
                        antMatchers(PUBLIC_MATCHERS).
                        permitAll().anyRequest().authenticated();
                
                http
                        .csrf().disable().cors().disable()
                        .formLogin().failureUrl("/login?error").defaultSuccessUrl("/")
                        .loginPage("/login").permitAll()
                        .and()
                        .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                        .logoutSuccessUrl("/?logout").deleteCookies("remember-me").permitAll()
                        .and()
                        .rememberMe();
                        
    }
            
    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth)
              throws Exception{
              auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder());
            }
             
}
EN

回答 1

Stack Overflow用户

发布于 2021-02-08 06:29:49

您需要在您的@Service上添加UserSecurityService,如下所示。

代码语言:javascript
复制
@Service
public class UserSecurityService implements UserDetailsService {
    
        @Autowired
        private UserRepository userRepository;
            
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            User user=userRepository.findByUsername(username);
            
            if(null == user) {
                throw new UsernameNotFoundException("Username Not Found");
            }
            return user;
        }
    
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66096290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档