2015-02-13 8 views
8

के लिए एनोटेशन-निर्दिष्ट बीन नाम मैं अपने स्प्रिंग बूट एप्लिकेशन में ConflictingBeanDefinitionException त्रुटि प्राप्त करता रहता हूं। मैं पूरी तरह से यह सुनिश्चित नहीं कर रहा हूं कि इसे कैसे संबोधित किया जाए, मेरे पास कई @ कॉन्फ़िगरेशन एनोटेटेड कक्षाएं हैं जो थाइमेलीफ, स्प्रिंग सिक्योरिटी और वेब सेट अप करने में मदद करती हैं। एप्लिकेशन कंट्रोलर को दो बार सेटअप करने का प्रयास क्यों कर रहा है? (? और जहां यह यह करने के लिए कोशिश कर रहा है)स्प्रिंग बूट कॉन्फ्लिक्टिंग बीन डीफिनिशन अपवाद: @ कंट्रोलर क्लास

त्रुटि है:

org.springframework.beans.factory.BeanDefinitionStoreException: 
Failed to parse configuration class [org.kemri.wellcome.hie.Application]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: 
Annotation-specified bean name 'homeController' for bean class [org.kemri.wellcome.hie.HomeController] conflicts with existing, non-compatible bean definition of same name and class [org.kemri.wellcome.hie.controller.HomeController] 

मेरे वसंत बूट मुख्य आवेदन प्रारंभकर्ता:

@EnableScheduling 
@EnableAspectJAutoProxy 
@EnableCaching 
@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
public class Application extends SpringBootServletInitializer { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 

    @Override 
    protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 

} 

मेरे डेटाबेस कॉन्फ़िग फ़ाइल:

@Configuration 
@EnableTransactionManagement 
@EnableJpaRepositories(basePackages="org.kemri.wellcome.hie.repositories") 
@PropertySource("classpath:application.properties") 
public class DatabaseConfig { 

    @Autowired 
    private Environment env; 

    @Autowired 
    private DataSource dataSource; 

    @Autowired 
    private LocalContainerEntityManagerFactoryBean entityManagerFactory; 

    @Bean 
    public DataSource dataSource() { 
    DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
    dataSource.setDriverClassName(env.getProperty("spring.datasource.driverClassName")); 
    dataSource.setUrl(env.getProperty("spring.datasource.url")); 
    dataSource.setUsername(env.getProperty("spring.datasource.username")); 
    dataSource.setPassword(env.getProperty("spring.datasource.password")); 
    return dataSource; 
    } 
    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
    LocalContainerEntityManagerFactoryBean entityManagerFactory = 
     new LocalContainerEntityManagerFactoryBean(); 

    entityManagerFactory.setDataSource(dataSource); 

    // Classpath scanning of @Component, @Service, etc annotated class 
    entityManagerFactory.setPackagesToScan(
     env.getProperty("spring.jpa.hibernate.entitymanager.packagesToScan")); 

    // Vendor adapter 
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
    entityManagerFactory.setJpaVendorAdapter(vendorAdapter); 

    // Hibernate properties 
    Properties additionalProperties = new Properties(); 
    additionalProperties.put(
     "hibernate.dialect", 
     env.getProperty("spring.jpa.hibernate.dialect")); 
    additionalProperties.put(
     "hibernate.showsql", 
     env.getProperty("spring.jpa.hibernate.showsql")); 
    additionalProperties.put(
     "hibernate.hbm2ddl.auto", 
     env.getProperty("spring.jpa.hibernate.hbm2ddl.auto")); 
    entityManagerFactory.setJpaProperties(additionalProperties); 

    return entityManagerFactory; 
    } 
    @Bean 
    public JpaTransactionManager transactionManager() { 
    JpaTransactionManager transactionManager = 
     new JpaTransactionManager(); 
    transactionManager.setEntityManagerFactory(
     entityManagerFactory.getObject()); 
    return transactionManager; 
    } 
    @Bean 
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { 
    return new PersistenceExceptionTranslationPostProcessor(); 
    } 

} 

मेरा थाइमेलीफ कॉन्फ़िगरेशन फ़ाइल:

@Configuration 
public class ThymeleafConfig { 

@Bean 
public ServletContextTemplateResolver templateResolver(){ 
    ServletContextTemplateResolver thymeTemplateResolver = new ServletContextTemplateResolver(); 
    thymeTemplateResolver.setPrefix("/WEB-INF/views/"); 
    thymeTemplateResolver.setSuffix(".html"); 
    thymeTemplateResolver.setTemplateMode("HTML5"); 
    return thymeTemplateResolver; 
} 

@Bean 
public SpringSecurityDialect springSecurityDialect(){ 
    SpringSecurityDialect dialect = new SpringSecurityDialect(); 
    return dialect; 
} 

@Bean 
public SpringTemplateEngine templateEngine() { 
    SpringTemplateEngine engine = new SpringTemplateEngine(); 
    engine.addTemplateResolver(templateResolver()); 
    Set<IDialect> dialects = new HashSet<IDialect>(); 
    dialects.add(springSecurityDialect()); 
    engine.setAdditionalDialects(dialects);  
    return engine; 
} 

@Bean 
public ThymeleafViewResolver thymeleafViewResolver() { 
    ThymeleafViewResolver resolver = new ThymeleafViewResolver(); 
    resolver.setTemplateEngine(templateEngine()); 
    resolver.setViewClass(ThymeleafTilesView.class); 
    resolver.setCharacterEncoding("UTF-8"); 
    return resolver; 
} 

}

मेरा वेब config वर्ग:

@Configuration 
@PropertySource("classpath:application.properties") 
public class WebConfig extends WebMvcAutoConfigurationAdapter { 

    @Autowired 
    private Environment env; 

    @Bean 
    public JavaMailSenderImpl javaMailSenderImpl() { 
     JavaMailSenderImpl mailSenderImpl = new JavaMailSenderImpl(); 
     mailSenderImpl.setHost(env.getProperty("smtp.host")); 
     mailSenderImpl.setPort(env.getProperty("smtp.port", Integer.class)); 
     mailSenderImpl.setProtocol(env.getProperty("smtp.protocol")); 
     mailSenderImpl.setUsername(env.getProperty("smtp.username")); 
     mailSenderImpl.setPassword(env.getProperty("smtp.password")); 
     Properties javaMailProps = new Properties(); 
     javaMailProps.put("mail.smtp.auth", true); 
     javaMailProps.put("mail.smtp.starttls.enable", true); 
     mailSenderImpl.setJavaMailProperties(javaMailProps); 
     return mailSenderImpl; 
    } 

    @Bean 
    public CacheManager cacheManager() { 
     return new ConcurrentMapCacheManager(); 
    } 
} 

मेरे नियंत्रक

@Controller 
public class HomeController { 

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

    /** 
    * Simply selects the home view to render by returning its name. 
    */ 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model) { 
     logger.info("Welcome home! The client locale is {}.", locale); 

     Date date = new Date(); 
     DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 

     String formattedDate = dateFormat.format(date); 

     model.addAttribute("serverTime", formattedDate); 

     return "index.html"; 
    } 
} 

क्या के लिए ConflictingBeanDefinitionException त्रुटि कारण हो सकता है (जहां एक नियंत्रक की स्थापना त्रुटि है) मेरी नियंत्रक वर्ग?

+0

कोशिश ** @ EnableWebMvc ** अपने _WebConfig.class_ – lgd

+0

कि काम नहीं किया है पर। मुझे अब भी वही त्रुटि मिलती है। –

उत्तर

5

जैसा समाधान मैंने पाया, समाधान स्कैन में फ़िल्टर को शामिल करके डबल प्रारंभिकता को अक्षम करना है। मेरे मामले में:

@EnableScheduling 
@EnableAspectJAutoProxy 
@EnableCaching 
@Configuration 
@ComponentScan(basePackages = { "org.kemri.wellcome.hie" }, 
    excludeFilters = {@Filter(value = Controller.class, type = FilterType.ANNOTATION)}) 
@EnableAutoConfiguration 
@PropertySource("classpath:application.properties") 
public class Application extends SpringBootServletInitializer { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 
+1

आपने वास्तव में डबल प्रारंभिक स्थान (स्टैक ट्रेस से छोड़कर) कहां रखा? क्या आपको कारण मिला? – lgd

0

ऐसा लगता है कि आप दो entityManagerFactory, एक आप autowire जाएगा और आप बीन के रूप में प्रोग्राम के रूप में हल एक है:

@Autowired 
private LocalContainerEntityManagerFactoryBean entityManagerFactory; 

@Bean 
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
... 
} 

मैं तुम सिर्फ entityManagerFactory में अपने कॉन्फ़िगर फैक्टरी() विधि की जरूरत है।

+0

अभी भी वही त्रुटि मिल रही है। –

+0

आपको अपने आवेदन कक्षा में कॉन्फ़िगरेशन विधि को ओवरराइड करने की आवश्यकता नहीं है, क्योंकि आपकी कक्षा स्वयं ही एक एप्लिकेशन है। –

+0

मैंने मुख्य अनुप्रयोग वर्ग में कॉन्फ़िगरेशन फ़ंक्शन को हटा दिया है और मुझे अभी भी वही त्रुटि मिलती है। –

0

मुझे वसंत-बूट से उत्पन्न जेनर फ़ाइल के साथ एक ही समस्या हो रही थी। अनुमोदित समाधान (तीमुथियुस तुति का अपना समाधान) मेरे लिए काफी काम नहीं करता है, लेकिन मैंने इसे थोड़ा सा tweaked और यह काम किया। मैं बस अपना Application.java में निम्नलिखित पंक्ति कहा:

@ComponentScan(basePackages = { "com.mypackage" })

संदर्भ के लिए, यहाँ जाता है मेरा पूरा Application.java

package com.inmoment.devchallenge; 
import org.neo4j.graphdb.GraphDatabaseService; 
import org.neo4j.graphdb.factory.GraphDatabaseFactory; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.context.web.SpringBootServletInitializer; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.neo4j.config.EnableNeo4jRepositories; 
import org.springframework.data.neo4j.config.Neo4jConfiguration; 

@SpringBootApplication 
@Configuration 
@ComponentScan(basePackages = { "com.inmoment.devchallenge.controller" }) 
@EnableAutoConfiguration 
public class Application extends SpringBootServletInitializer { 

    @Configuration 
    @EnableNeo4jRepositories(basePackages = "com.inmoment.devchallenge.repository") 
    static class ApplicationConfig extends Neo4jConfiguration { 

     public ApplicationConfig() { 
      setBasePackage("com.inmoment.devchallenge.repository"); 
     } 

     @Bean 
     GraphDatabaseService graphDatabaseService() { 
      return new GraphDatabaseFactory().newEmbeddedDatabase("accessingdataneo4j.db"); 
     } 
    } 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(Application.class, args); 
    } 

} 
12

मैं एक ही समस्या में लेकिन एक अलग कारण के लिए भाग गया।

यह तब भी हो सकता है जब आप अपनी कक्षाओं में अपनी कक्षाओं को चारों ओर ले जाएं और 'साफ' करने में असफल हों।

मैं वसंत-बूट प्लगइन के साथ ग्रेडल का उपयोग करता हूं। अब मैं आमतौर पर चलाएँ: जोड़ने के लिए

$> ./gradlew clean bootRun 
+1

मुझे लगता है कि यह सही जवाब होना चाहिए। स्वीकार्य समाधान भी मदद करता है, लेकिन यह संभवतः बालों के आंकड़ों के कारण भी होता है। –

संबंधित मुद्दे