Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to express my problem #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/com/example/demo/AnotherConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ServletContextAware;

Expand All @@ -25,4 +26,13 @@ public void setServletContext( ServletContext servletContext ) {
this.servletContext = servletContext;
}

}
/**
* I have a bean that depends on servletContext
*/
@Bean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be a bean?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would need to use it in other parts of the application.

In CometD context this bean will be a BayeuxServer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that won't work for sure. Can you give this a try?

public class DemoBean implements ApplicationListener<ApplicationStartedEvent> {

   ....

   @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
      ApplicationContext applicationContext = event.getApplicationContext();
      this.servletContext = ((WebApplicationContext) applicationContext).getServletcontext();
    }

}

public Hello hello() {
LOG.warn("hello() {}", servletContext);
return new Hello(servletContext);
}

}
36 changes: 33 additions & 3 deletions src/main/java/com/example/demo/DemoPostProcessor.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.demo;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand All @@ -21,10 +23,38 @@ public class DemoPostProcessor implements DestructionAwareBeanPostProcessor, App

private WebApplicationContext applicationContext;

Optional<Hello> getHello() {
if (applicationContext.getServletContext() != null) {
// Once we have servletContext I want to instantiate my Hello bean. This could fail with:
//
// > Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException:
// > Error creating bean with name 'hello':
// > Requested bean is currently in creation: Is there an unresolvable circular reference?

try {
return Optional.of(applicationContext.getBean(Hello.class));
} catch (BeanCurrentlyInCreationException e) {
// Ignore
}
}

return Optional.empty();
}

@Override
public Object postProcessBeforeInitialization( Object bean, String name ) throws BeansException {
Optional<Hello> found = getHello();
if (found.isPresent()) {
found.get().process(bean, name);
} else {
LOG.warn("Hello not found yet {}", name);
}
return bean;
}

@Override
public void postProcessBeforeDestruction(Object o, String s) throws BeansException {
LOG.warn("Destruction of {} with servletContext {}", s, this.applicationContext.getServletContext());
public void postProcessBeforeDestruction(Object bean, String name) throws BeansException {
// LOG.warn("Destruction of {} with servletContext {}", name, getHello().get().servletContext);
}

@Override
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/example/demo/Hello.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.demo;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class should process beans.
*/
public class Hello {
private static final Logger LOG = LoggerFactory.getLogger(Hello.class);

public final ServletContext servletContext;

public Hello(ServletContext servletContext) {
if (servletContext == null) {
throw new IllegalArgumentException("servletContext is null");
}
this.servletContext = servletContext;
}

public void process(Object bean, String name) {
LOG.warn("I am processing a bean {} name. servletContext={}", name, servletContext);

}
}