Announcement Announcement Module
Collapse
No announcement yet.
Problem to inject ServletContext into bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem to inject ServletContext into bean

    I have web-app (Web Flow + JSF), and bean that implements ServletContextAware, but ServletContext is not injected to that bean.
    Why ?

    Code:
    	<bean class="services.MessageService" id="messageService" scope="singleton" />

    Code:
    public class MessageService implements Serializable, ServletContextAware {
           private Logger log = Logger.getLogger(getClass());
    
    	@Override
    	public void setServletContext(ServletContext servletContext) {
    		// TODO Auto-generated method stub
    		log.debug("setServletContext("+servletContext+")");
    	}
    
    }

  • #2
    1)Singleton is default scope for beans so no need to specify it

    2)Why should a service be aware of servlet context? If you properly organize your application into layers, the service (business) layer should be separate from the presentation layer and should know nothing of servlets or servlet contexts.

    Anyway, the reason why you are getting null can be:

    - you're not properly using dependency injection pattern. For example, in the class where you use your MessageService you are not injecting the bean but doing MessageService service = new MessageService() instead;

    - since you're using JSF, maybe you're using the MessageService inside a JSF managed bean. Those beans are not Spring beans so dependencies simply are not injected.

    Comment

    Working...
    X