Every bean/class instance created twice in spring boot application

Ved Prakash

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>
Hopey One

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Are new instances of Service, Repository and Component created for every new request of spring mvc rest api or spring boot api to application server?

spring boot application deploy to dedicated jetty instance

Sharing an instance of a class across a spring boot application

Spring boot application main method is executing twice -> port already in use

How to make sure the application is installed on a spot ec2 instance every time a new instance is created

Spring - application Initialized twice?

Spring Application loads twice

Cannot use AWS SDK in Spring Boot Application (Socket not created by this factory)

how to limit the database connection created by spring boot application running microservices

Where and how to store txt files created periodically in Spring boot application

Table not created in MySql DB after starting Spring Boot application

Does every spring boot application create a tomcat container to be able to run?

How to connect to specific local MongoDB instance in Spring Boot Dockerised application?

How to create a second RedisTemplate instance in a Spring Boot application

Why Spring Boot Application logs that it started twice after adding spring-cloud-bus dependency

This application has no explicit mapping for /error, so you are seeing this as a fallback. In my newly created Spring boot application

Will a new instance of Singleton scoped object in a WebApplicationContext be created if the application is closed and started again in Spring MVC

Spring beans are initializing twice in Spring Web application

Error while using npm install in spring boot application created using JHipster

Spring boot application, integration of log4j, no file is created whenusing rolling file appender

Why is my h2 database empty every time my Spring Boot application starts?

Spring boot application and MessageSource

Spring boot application into Kubernetes

Spring Boot Patient Application

Spring boot for desktop application

Spring boot application as dependency

Session in Spring Boot application

NonUniqueResultException in Spring Boot application

Spring Boot application is not executable