Announcement Announcement Module
Collapse
No announcement yet.
Wiring a bean into servlet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Wiring a bean into servlet

    I am developing a game site and I want to wire game server with the web application, which is not currently in Spring web MVC but plain servlets/jsp. Namely I need game server written in Java to check game server logins with the web session keys, so I need to wire game server login tokens cache with the login servlet.

    For that, I did the following:
    1.
    Added a listener to my web application web.xml
    Code:
    <listener><listener-class>com.foo.ContextLoadingListener</listener-class></listener>
    2.
    Load login tokens cache in the listener
    Code:
    public class ContextLoadingListener implements ServletContextListener  {
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            
          ApplicationContext ctx = new ClassPathXmlApplicationContext("newSpringXMLConfig.xml");
    
          LoginTokensCache testObject = (LoginTokensCache) ctx.getBean("tokensCache");
        } ... }
    3.
    Have the tokens cache autowired into login servlet
    Code:
    public class LoginServlet extends HttpServlet {
        @Autowired
        // @Qualifier("tokensCacheQual")
        private LoginTokensCache tokens;
    4.
    Use annotations configuration in Spring config file and have one login servlet defined as bean there, hoping that it will make Spring notice itself

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:p="http://www.springframework.org/schema/p"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    ">
        <context:annotation-config/>
    <bean id="tokensCache" class="com.foo.LoginTokensCache">
        <qualifier value="tokensCacheQual" />
    </bean>
    <bean id="loginServletBeanID" class="com.foo.LoginServlet" ></bean>
    </beans>
    As the result, I don't get tokens cache to be wired into the login servlet. I tried with & without the qualifier. Can I somehow force the web container to wire something into servlets?

  • #2
    Here is one way which I had done it previously
    Code:
    @Override
    	public void init() throws ServletException {
    		WebApplicationContextUtils.
    				getRequiredWebApplicationContext(getServletContext()).
    				getAutowireCapableBeanFactory().
    				autowireBean(this);
    	}
    This basically converts your servlet into spring bean on which spring container can inject dependencies.

    Comment


    • #3
      Wiring a bean into servlet as well as struggle so be motivational in all perspectives and be determined in all views so get a broad vision in any ways so always consider morale in all stage.

      Comment

      Working...
      X