스프링 부트 충돌 Bean 정의예외:@Controller 클래스의 주석 지정 빈 이름
자꾸만 그런 생각이 들어요.ConflictingBeanDefinitionException
Spring Boot 어플리케이션에서 오류가 발생했습니다.어떻게 대처해야 할지 잘 모르겠습니다만, 몇 가지 있습니다.@Configuration
Tymeleaf, Spring Security 및 Web 설정에 도움이 되는 주석이 달린 클래스입니다.어플리케이션은 왜 셋업을 시도합니까?homeController
두 번이나요? (어디서 이걸 하려고 하는 거죠?)
에러는 다음과 같습니다.
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();
}
}
내 Tymeleaf 구성 파일:
@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;
}
}
내 웹 구성 클래스:
@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
컨트롤러 클래스에 오류가 있습니까?
나는 같은 문제에 부딪혔지만 다른 이유로 부딪혔다.
이 문제는 프로젝트에서 클래스를 이동했다가 '청소'를 수행하지 못한 경우에도 발생할 수 있습니다.
스프링 부트 플러그인으로 그라들을 사용하고 있습니다.지금은 주로 달리기를 합니다.
$> ./gradlew clean bootRun
Spring Integration Test에서도 인텔iJ에서 실행했을 때 같은 문제가 발생하였습니다.
리팩터 후 컨트롤러 클래스 중 하나가 버전 2017.2 이후 인텔리j의 기본 출력 디렉토리인 /out/production/classes 디렉토리에 실제로 중복되었습니다.gradle 출력 디렉토리가 다르기 때문에(빌드/클래스), gradle clean goal에는 영향이 없었습니다.
이 솔루션은 수동으로 /out/production/classes를 삭제하고 통합 테스트를 다시 실행하는 것이었습니다.
2개의 출력 디렉토리가 없는 내구성이 뛰어난 솔루션에 대해서는, 여기를 참조해 주세요.
해결 방법은 컴포넌트 스캔에 필터를 포함시킴으로써 이중 초기화를 비활성화하는 것입니다.내 경우:
@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);
}
}
여러 폴더 이름과 관련 패키지 이름을 변경한 후 mvn에서 이를 발견했습니다.maven clean and run spring boot를 다시 적용했더니 모두 해결되었습니다.
mvn clean
mvn spring-boot:run
entityManagerFactory가 2개 있는 것 같습니다.하나는 자동 배선하고 다른 하나는 Bean으로 프로그래밍 방식으로 해결합니다.
@Autowired
private LocalContainerEntityManagerFactoryBean entityManagerFactory;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
...
}
entityManagerFactory() 메서드에 설정된 팩토리만 있으면 된다고 생각합니다.
spring-boot에서 생성된 .war 파일에서도 같은 문제가 발생했습니다.승인된 솔루션(Timothy Tuti 자신의 솔루션)은 현재 상태로는 효과가 없었지만, 조금 수정해 보니 효과가 있었습니다.저는 방금 제 어플리케이션에 다음 행을 추가했습니다.자바:
@ComponentScan(basePackages = { "com.mypackage" })
참고로, 저의 신청서 전문을 보내드리겠습니다.자바
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);
}
}
나는 반 위에 콩 이름을 붙여서 문제를 해결했다.
@Component("myBeanName1")
public class MyBean {
}
다음 방법으로 @Autowire로 초기화합니다.
@Autowire
@Qualifier("myBeanName1")
MyBean myBean;
이름을com.example.company
은 '반명'입니다.RestExceptionHandler
그런 다음 패키지와 동일한 이름을 추가해야 합니다.
을 달다@Component("com.example.company.RestExceptionHandler")
충돌 없이 클래스를 식별할 수 있습니다.
현재 모듈(X라고 함)의 종속성(모듈 Y라고 함) 중 하나에서도 같은 클래스의 정의가 있을 때 같은 문제가 발생하였습니다.그래서 저는 공통 클래스를 저장하기 위해 별도의 모듈(Z라고 함)을 만든 후 X와 Y를 모두 사용하기 위해 Z에 대한 의존성을 추가해야 했습니다.
언급URL : https://stackoverflow.com/questions/28498295/spring-boot-conflictingbeandefinitionexception-annotation-specified-bean-name-f
'programing' 카테고리의 다른 글
특정 요청에 대해 ajaxStart() 및 ajaxStop()을 사용하지 않도록 설정합니다. (0) | 2023.03.20 |
---|---|
Type Script:부울을 문자열 값으로 변환 (0) | 2023.03.20 |
Gson의 POJO 어레이 리스트 시리얼화 문제 (0) | 2023.03.20 |
컨트롤러에서 $inject를 사용할 수 없습니다. (0) | 2023.03.20 |
Jasmine 2.0 async done() 및 angular-mocks inject()가 같은 테스트 it()에서 사용됩니다. (0) | 2023.03.20 |