Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to parse configuration class, Spring

It's my first time asking a question so please guide me through any mistakes I might make.

I'm using Intellij, with Spring and Gradle -also for the first time- to make the backend part of a web application.
It compiles ok but once I try running it I get the following error:

2018-02-27 17:00:33.526  INFO 4308 --- [           main] g.n.e.s.k.KidspirationApplication        : Starting KidspirationApplication on mini-me with PID 4308 (started by Orestis in C:\Users\Orestis\Desktop\Software Technology\Stack-Over-Flowers\kidspiration)
2018-02-27 17:00:33.531  INFO 4308 --- [           main] g.n.e.s.k.KidspirationApplication        : No active profile set, falling back to default profiles: default
2018-02-27 17:00:33.606  INFO 4308 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@551bdc27: startup date [Tue Feb 27 17:00:33 EET 2018]; root of context hierarchy
2018-02-27 17:00:33.969  WARN 4308 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [gr.ntua.ece.softeng.kidspiration.KidspirationApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'registerController' for bean class [gr.ntua.ece.softeng.kidspiration.Register_Login.RegisterController] conflicts with existing, non-compatible bean definition of same name and class [gr.ntua.ece.softeng.kidspiration.Controllers.RegisterController]
2018-02-27 17:00:34.136 ERROR 4308 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [gr.ntua.ece.softeng.kidspiration.KidspirationApplication]; nested exception is 
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'registerController' for bean class [gr.ntua.ece.softeng.kidspiration.Register_Login.RegisterController] conflicts with existing, non-compatible bean definition of same name and class [gr.ntua.ece.softeng.kidspiration.Controllers.RegisterController]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:272) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at gr.ntua.ece.softeng.kidspiration.KidspirationApplication.main(KidspirationApplication.java:14) [classes/:na]  

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'registerController' for bean class [gr.ntua.ece.softeng.kidspiration.Register_Login.RegisterController] conflicts with existing, non-compatible bean definition of same name and class [gr.ntua.ece.softeng.kidspiration.Controllers.RegisterController]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:345) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:283) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:135) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:287) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... 13 common frames omitted

Process finished with exit code 1

The build.gradle is:

buildscript {
    ext {
        springBootVersion = '1.5.10.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin:'idea'
apply plugin: 'org.springframework.boot'

group = 'gr.ntua.ece.softeng'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8

jar {
baseName = 'kidspiration-platform'
version = '0.1.0'
}

repositories {
mavenCentral()
}

dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework:spring-jdbc')
compile('mysql:mysql-connector-java')
compile('com.h2database:h2')
//runtime('mysql:mysql-connector-java')
}

The ApplicationConfig.java is:

package gr.ntua.ece.softeng.kidspiration.Configuration;

import org.springframework.beans.factory.annotation.Autowired;
import javax.sql.DataSource;

//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = "gr.ntua.ece.softeng.kidspiration")
@PropertySource(value = {"classpath:application.properties"})
//@EnableAutoConfiguration

public class ApplicationConfig {

@Autowired
private Environment env;

@Bean
public DataSource datasource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getRequiredProperty("spring.datasource.driver-class-name"));
    dataSource.setUrl(env.getRequiredProperty("spring.datasource.url"));
    dataSource.setUsername(env.getRequiredProperty("spring.datasource.username"));
    dataSource.setPassword(env.getRequiredProperty("spring.datasource.password"));
    return dataSource;
}

@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    return jdbcTemplate;
}

@Bean
public PlatformTransactionManager txManager() {
    return new DataSourceTransactionManager(datasource()); //check later if datasource could be passed as parameter
}
}

The Application.Java is:

package gr.ntua.ece.softeng.kidspiration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling

public class KidspirationApplication {

    public static void main(String[] args) {

        SpringApplication.run(KidspirationApplication.class, args);
    }
}

So far, I haven't managed to pinpoint a specific part of code responsible for the error message and the source code is extended.
So, if you would like to see some part or all of it, I'd be thankful for your suggesting an easy way to present it!

I've tried renaming the "registerController" but it only lead to a minor alteration in the error message.

Please, tell me if I didn't explain something fairly enough or gave too little detail.
Any kind of help or suggestion would be appreciated.

like image 322
okariotis Avatar asked May 17 '26 20:05

okariotis


1 Answers

Remove your component scan from the configuration file. it is not needed as it is rolled into the SpringBootApplication annotation.

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html

@Target(value=TYPE)
 @Retention(value=RUNTIME)
 @Documented
 @Inherited
 @SpringBootConfiguration
 @EnableAutoConfiguration
 @ComponentScan(excludeFilters={@ComponentScan.Filter(type=CUSTOM,classes=TypeExcludeFilter.class),})
public @interface SpringBootApplication

You can also remove @PropertySource(value = {"classpath:application.properties"}) spring boot will automatically load this.

See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config for more information on this.

like image 176
Darren Forsythe Avatar answered May 19 '26 09:05

Darren Forsythe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!