• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Spring Boot problem z tworzeniem bean-a

Cloud VPS
0 głosów
507 wizyt
pytanie zadane 28 maja 2019 w Java przez JuniorPL Użytkownik (770 p.)

Cześć mam problem którego od dłuższego czasu nie mogę rozwiązać. W programie tworzy się "konflikt" po dodaniu zależności JDBC i JPA. Jeżeli te dwie zależnosci są w projekcie to nie mogę stworzyć bena. Może ktoś coś podpowie. W projekcie są trzy klasy.

Klasa: DemoSecurityConfig
 


@Configuration
@EnableWebSecurity
@ComponentScan(basePackages="CarSupportApplication")
@PropertySource("classpath:persistence-mysql.properties")

public class DemoSecurityConfig extends WebSecurityConfigurerAdapter
{
    
    @Autowired
    private Environment env;
    
    private Logger logger = Logger.getLogger(getClass().getName());
    
    @Autowired
    private DataSource securityDataSource;
    
        
    @Bean
    public DataSource securityDataSource() {
        
        // create connection pool
        
        ComboPooledDataSource securityDataSource = new ComboPooledDataSource();
            
        // set the jdbc driver class
        
        try
        {
            securityDataSource.setDriverClass(env.getProperty("jdbc.driver"));
        }
        catch (PropertyVetoException exc)
        {
            throw new RuntimeException(exc);
        }
                        
        logger.info(">>> jdbc.url=" + env.getProperty("jdbc.url"));
        logger.info(">>> jdbc.user=" + env.getProperty("jdbc.user"));
           
        
        securityDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
        securityDataSource.setUser(env.getProperty("jdbc.user"));
        securityDataSource.setPassword(env.getProperty("jdbc.password"));
        
           
        securityDataSource.setInitialPoolSize(
                getIntProperty("connection.pool.initialPoolSize"));

        securityDataSource.setMinPoolSize(
                getIntProperty("connection.pool.minPoolSize"));

        securityDataSource.setMaxPoolSize(
                getIntProperty("connection.pool.maxPoolSize"));

        securityDataSource.setMaxIdleTime(
                getIntProperty("connection.pool.maxIdleTime"));
        
        return securityDataSource;
    }
    
private int getIntProperty(String propName) {
        
        String propVal = env.getProperty(propName);
            
        int intPropVal = Integer.parseInt(propVal);
        
        return intPropVal;
    }
    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        
        auth.jdbcAuthentication().dataSource(securityDataSource);        
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception
    {

        http.authorizeRequests()
        
        .antMatchers("/hidden-page/**").hasRole("EMPLOYEE")
         .antMatchers("/").permitAll()
        .and()
        .formLogin()
        .loginPage("/login-form")
        .loginProcessingUrl("/authenticateTheUser")
        .defaultSuccessUrl("/hidden-page",true)
        .permitAll()
        .and()
        .logout().permitAll()
        .and()
        .exceptionHandling()
        .accessDeniedPage("/access-denied");
        
    }
}

Klasa:  MySpringMvcDispacherServletInitializer

public class MySpringMvcDispacherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class<?>[] getRootConfigClasses() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		// TODO Auto-generated method stub
		return new Class[] {DemoSecurityConfig.class};
	}

	@Override
	protected String[] getServletMappings() {
		// TODO Auto-generated method stub
		return new String[] {"/"};
	}

}

Klasa:  SecurityWebApplicationInitializer

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer
{
	
}

Stack tracer:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'securityDataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:339)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoSecurityConfig': Unsatisfied dependency expressed through field 'securityDataSource'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'securityDataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]

 

 

1 odpowiedź

0 głosów
odpowiedź 28 maja 2019 przez Czarus0 Obywatel (1,040 p.)
wybrane 28 maja 2019 przez JuniorPL
 
Najlepsza

Spróbuj przenieść konfigurację beana dataSource do innej klasy konfiguracyjnej:

@Configuration
public class DataSourceCnofig {
   @Bean
       public DataSource securityDataSource() { ... }
}

Wygląda jakby Spring przy inicjalizacji się zapętlił w tym przypadku.

komentarz 28 maja 2019 przez JuniorPL Użytkownik (770 p.)
Podzielenie programu na dwie klasy oraz dodanie adnotacji "@EnableTransactionManagement" rozwiązało problem, dzięki za podpowiedź.

Podobne pytania

0 głosów
2 odpowiedzi 399 wizyt
pytanie zadane 1 sierpnia 2017 w Java przez Szymon Ciamaga Obywatel (1,980 p.)
0 głosów
2 odpowiedzi 364 wizyt
pytanie zadane 20 maja 2017 w Java przez jaroslaw.slaby Początkujący (410 p.)
0 głosów
0 odpowiedzi 409 wizyt

93,485 zapytań

142,417 odpowiedzi

322,765 komentarzy

62,898 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

Kursy INF.02 i INF.03
...