Results 1 to 2 of 2

Thread: Problem to inject ServletContext into bean

  1. #1
    Join Date
    Aug 2010
    Posts
    25

    Default 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. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •