Announcement Announcement Module
No announcement yet.
Problem to inject ServletContext into bean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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 ?

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

    public class MessageService implements Serializable, ServletContextAware {
           private Logger log = Logger.getLogger(getClass());
    	public void setServletContext(ServletContext servletContext) {
    		// TODO Auto-generated method stub

  • #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.