Home > Software design >  The dependencies of some of the beans in the application context form a cycle: session factory creat
The dependencies of some of the beans in the application context form a cycle: session factory creat

Time:05-12

i'm new in Spring framework. I have a spring boot application(mvc, hibernate), im trying to get session factory but i got an error

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  securityConfig defined in file [mySrc\springBootApp\config\SecurityConfig.class]
↑     ↓
|  userDetailsServiceImpl defined in file [mySrc/springBootApp\service\impl\UserDetailsServiceImpl.class]   
↑     ↓
|  userDAO defined in me.kqlqk.springBootApp.DAO.UserDAO defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration
↑     ↓
|  (inner bean)#2b6a0ea9
└─────┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

My config class:

@Configuration
@EnableWebSecurity
@EnableTransactionManagement
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    private final UserDetailsService userDetailsService;
    private Environment env;

    @Autowired
    public SecurityConfig(@Qualifier("userDetailsServiceImpl") UserDetailsService userDetailsService, Environment env) {
        this.userDetailsService = userDetailsService;
        this.env = env;
    }

    //configuration here

    @Bean("entityManagerFactory")
    @Autowired
    public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
        properties.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
        properties.put("current_session_context_class", env.getProperty("spring.jpa.properties.hibernate.current_session_context_class"));

        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setPackagesToScan("me.kqlqk.springBootApp.models");
        sessionFactory.setHibernateProperties(properties);

        return sessionFactory;
    }
}

i use @Bean("entityManagerFactory") cuz without it i got

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in me.kqlqk.springBootApp.service.impl.UserDetailsServiceImpl required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Qualifier(value="userDetailsServiceImpl")


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

and my userDetailsServiceImpl class:

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    private UserDAO userDAO;

    @Autowired
    public UserDetailsServiceImpl(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        User user = userDAO.getByEmail(email);
        Set<GrantedAuthority> authorities = new HashSet<>();

        user.getRoles().forEach(role -> authorities.add(new SimpleGrantedAuthority(role.getName())));

        return new org.springframework.security.core.userdetails.User(
                user.getEmail(),
                user.getPassword(),
                authorities);
    }
}

So, how can i configure session factory or where's problem in my code?

CodePudding user response:

So, i just decied to use @Transactional and no sessionFactory needed

  • Related