Tuesday, November 1, 2011

如何在servlet取得spring beans (autowired)


servlet ,會碰到無法讀到 spring 定義的annotation ,解法是:在 servlet的 init method中,,寫下面的程式,
讓 spring 去 ApplicationContext 做 lookup,看 ApplicationContext 有那些已經注入(inject) 的 Spring beans。
   1: public void init(ServletConfig config) throws ServletException {
   2:     super.init(config);
   3:     SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
   4: }

這樣,就可以用 spring @Autowired 等的 annotation。

參考來源:

http://stackoverflow.com/questions/467235/access-spring-beans-from-a-servlet-in-jboss

10 comments:

  1. I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective. Techno-based information has been fetched in each of your topics. Sure it will enhance and fill the queries of the public needs. Feeling so glad about your article. Thanks…!
    magento training course in chennai
    magento training institute in chennai
    magento 2 training in chennai
    magento development training
    magento 2 course
    magento developer training

    ReplyDelete

  2. You write this post very carefully I think, which is easily understandable to me. Not only this, but another post is also good. As a newbie, this info is really helpful for me. Thanks to you.
    Tally ERP 9 Training
    tally classes
    Tally Training institute in Chennai
    Tally course in Chennai

    ReplyDelete
  3. I feel satisfied to read your blog, you have been delivering a useful & unique information to our vision.keep blogging.
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata

    ReplyDelete