简体   繁体   中英

Spring creates two of @Configuration beans starting up

I have following class:

@Configuration
public class EndpointStatus {

private static final Logger serverLogger = LogManager.getLogger(EndpointStatus.class);

private Long id;
private volatile Status status;

@OneToOne
private volatile CurrentJob currentJob;

public enum Status {
    AVAILABLE,
    BUSY
}

@Bean
@Primary
public EndpointStatus getEndpointStatus() {
    serverLogger.info("STATUS CREATED");
    return new EndpointStatus();
}

public EndpointStatus() {
}

public CurrentJob getCurrentJob() {
    return currentJob;
}

public void setCurrentJob(CurrentJob currentJob) {
    this.currentJob = currentJob;
}

public Status getStatus() {
    return status;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public void setStatus(Status status) {
    this.status = status;
}

public boolean isBusy() {
    return getStatus() == Status.BUSY;
}

Bean is used in endpoint which is annotated with @Component

and then i try to get the bean in endpoint like

ApplicationContext ctx = new AnnotationConfigApplicationContext(EndpointStatus.class);
EndpointStatus sc = ctx.getBean(EndpointStatus.class);

EndpointStatus is not used anywhere else.

To my knowledge, there should be no reason to create a second bean...

However at startup I always get

INFO 6169 [main] c.e.k.d.r.m.i.EndpointStatus             : STATUS CREATED
INFO 6169 [main] c.e.k.d.r.m.i.EndpointStatus             : STATUS CREATED

What am I doing wrong here?

EDIT:

Have tried every answer given to no avail whatsoever..

my class now looks like this

@Configuration
public class EndpointStatusConfig {

private static final Logger serverLogger = LogManager.getLogger(JavaXRest.class);

private Long id;
private volatile Status status = EndpointStatusConfig.Status.AVAILABLE;

@OneToOne
private volatile CurrentJob currentJob;

public enum Status {
    AVAILABLE,
    BUSY
}

@Bean
@Primary
public EndpointStatusConfig getEndpointStatus() {
    serverLogger.info("STATUS CREATED");
    return new EndpointStatusConfig();
}

public CurrentJob getCurrentJob() {
    return currentJob;
}

public void setCurrentJob(CurrentJob currentJob) {
    this.currentJob = currentJob;
}

public Status getStatus() {
    return status;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public void setStatus(Status status) {
    this.status = status;
}

public boolean isBusy() {
    return getStatus() == Status.BUSY;
}

  } 

no matter @Component or @Configuration, making call to sc in endpoint will result in hundreds of beans created crashing the app...

EDIT2: this is just getting worse and worse ... now even call to

if ( sc.isBusy() ) { return Response.ok( sc.getCurrentJob() ).type(MediaType.APPLICATION_JSON).build(); }

will jump to @Bean and create as many EndpointStatus objects as it can before the application crashes.... @Component creates one at startup, then thousands. @Configuration will create 2 at startup and then thousands also...

Just a guesswork but defining the configuration class both as a configuration and a factory bean return type is probably the issue.
EndpointStatus is a configuration class as the class is declared with @Configuration and a configuration class produces a bean in Spring and it is also an explicit bean as you annotated the bean factory method getEndpointStatus() with @Bean .
It is a little like if you had defined twice the bean.

Simply change the name of your Configuration class from EndpointStatus to EndpointStatusConfig and this will then only create a single bean with EndpointStatus class.

As you are annotating EndpointStatus both as @Configuration & @Bean it creates 2 Beans.

I think your problem is using @Configuration instead of @Component

The @Configuration will try add to spring context any @Autowired or @Bean inside the class with @Configuration but won't add it self to the spring context.

If you want to add that class as a bean to the spring context you should use @Component

Edit:

Did you try to inject EndpointStatus class with the @Configuration on it self?

In case you don't and don't know what is injection in spring try this:

@Autowired
EndpointStatus status;

void yourMethod(){
   //Change this 
   //ApplicationContext ctx = new AnnotationConfigApplicationContext(EndpointStatus.class);
   //EndpointStatus sc = ctx.getBean(EndpointStatus.class);
   //Use instead the status variable declared before
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM