0

我想将 Web 界面添加到我的 Java 应用程序,以便我可以使用 HTTP 操作它的状态。

我已经为一些启动嵌入式 Tomcat 的类添加了一个 Spring bean 到应用程序上下文中。这个类当然可以访问创建它的上下文。但我想以某种方式将此上下文存储在 Tomcat 类(org.apache.catalina.startup.Tomcat)中,以便稍后可以在 Servlet 中检索,以便我可以执行以下操作:

    public SomeClass extends extends HttpServlet {    
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                ApplicationContext appContext = getContextStoredEarlierInTomcatClass();
                SomeBeanFromContext sbfc = appContext.getBean("sbfc", ApplicationContext.class);
                sbfc.setSomeProperty(newValue);
            }    
    }

知道如何实现吗?

谢谢!

4

1 回答 1

0

Classes including Servlets do not require an ApplicationContext to obtain references to String beans. This is done using dependency injection

@Controller
@RequestMapping("/mypage")
public class SomeClass {

   @Autowired
   private SomeBeanFromContext sbfc;

   @RequestMapping(value = "/individualRequest", method = RequestMethod.GET)
   public String doIndividualRequest(HttpServletRequest request) {
      sbfc.setSomeProperty(newValue);
      ...
   }
}

Spring MVC offers a complete method of injecting beans into target web controller classes using @Controller annotated classes.

于 2013-06-21T18:45:11.013 回答