Home > OS >  Every bean/class instance created twice in spring boot application
Every bean/class instance created twice in spring boot application

Time:04-08

I'm using java based configuration, using only @Component and @Scheduler Annotation in my other classes, but I don't know why all classes in class path loaded twice. How to prevent this and how to debug it? problem : All my scheduled methods are running twice in scheduled interval. @Scheduled(cron = "0 0/5 * * * ?")

    @EnableScheduling
    @SpringBootApplication
    @ComponentScan(basePackages = "com.example")
    public class Dashboard implements CommandLineRunner{
    
    
        @Autowired
        private ApplicationContext appContext;
    
        @Autowired
        private EventBus eventBus;
    
        @Auditable(eventProvider = EventProvider.class)
        @PostConstruct
        public void startupApplication() {
            try {
                EventProvider provider = appContext.getBean(EventProvider.class);
                AuditRequestContext requestContext = new AuditRequestContext(null, null, null, null);
                Event aaaEvent = provider.getEvent(requestContext);
                eventBus.post(aaaEvent);
            } catch (Exception e) {
                System.out.println("Error getting bean for the class: " , e);
            }
        }
    
        public static void main(String[] args) {
            SpringApplication.run(Dashboard.class, args);
        }
    
        @Override
        public void run(String... args) throws Exception {
           String [] beans = appContext.getBeanDefinitionNames();
           Arrays.sort(beans);
           for(String bean : beans) {
               logger.info("bean :: "   bean);
           }  
        }
    
    }

Log printed: every bean loaded twice (All my scheduled methods are running twice)

dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: auditConfig 
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: auditConfig 
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: auditRequestBodyAdviceAdapter 
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: auditRequestBodyAdviceAdapter 
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: auditRequestInterceptor
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: auditRequestInterceptor 
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: authEntryPoint 
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: authEntryPoint 
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: authFilter 
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: authFilter 
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: authenticationController 
dashboard [INFO ] 23034 2022-04-08 04:54:05.227 [main] LoggerService - bean :: authenticationController

How to resolve this?

Added Log4j configuration.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
    <Properties>
        <Property name="basePath">${example.log.path}</Property>
        <Property name="host">localhost</Property>
        <Property name="port">514</Property>
        <Property name="appName">example Dashboard</Property>
        <Property name="facility">local0</Property>
        <Property name="mdcId">example Dashboard</Property>
    </Properties>
    <Appenders>
        <File name="fileLogger" fileName="${basePath}/${example.log.name}">
            <PatternLayout>
                <pattern>${package.name} [%-5level] %5X{pid} %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
        </File>
        <Syslog name="syslog"
                format="RFC5424" host="${host}" port="${port}"
                protocol="UDP" appName="${appName}" includeMDC="true"
                facility="${facility}" enterpriseNumber="18060" newLine="true"
                mdcId="${mdcId}">
        </Syslog>
    </Appenders>
    <Loggers>
        <Logger name="com.example" level="info" additivity="true">
            <AppenderRef ref="fileLogger"/>
            <AppenderRef ref="syslog"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="fileLogger"/>
        </Root>
    </Loggers>
</Configuration>

CodePudding user response:

You want to set additivity to false for your com.example logger. @AndyWilkinson is correct you're duplicating your logs, not your beans.

  • Related