无法解析配置类Spring

时间:2018-02-27 14:57:52

标签: java spring spring-boot gradle

这是我第一次提出问题所以请指导我解决我可能犯的任何错误。

我正在使用Intellij,第一次使用Spring和Gradle -also来构建Web应用程序的后端部分。
它编译好了,但是一旦我尝试运行它,我得到以下错误:

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

build.gradle是:

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')
}

ApplicationConfig.java是:

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
}
}

Application.Java是:

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);
    }
}

到目前为止,我还没有设法确定负责该错误消息的特定代码部分,并且源代码已扩展。
所以,如果你想看到它的一部分或全部,我会感谢你提出一个简单的方法来呈现它!

我已尝试重命名“registerController”,但它只会导致错误消息的轻微更改。

请告诉我,如果我没有足够的解释或给出太少的细节 任何形式的帮助或建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

从配置文件中删除组件扫描。它不需要,因为它被卷入SpringBootApplication注释。

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

你也可以删除@PropertySource(value = {"classpath:application.properties"}) spring boot会自动加载它。

有关详细信息,请参阅https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config