Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 直接注入servlet,而不是新的ClassPathXmlApplicationContext_Java_Applicationcontext_Spring Bean - Fatal编程技术网

Java 直接注入servlet,而不是新的ClassPathXmlApplicationContext

Java 直接注入servlet,而不是新的ClassPathXmlApplicationContext,java,applicationcontext,spring-bean,Java,Applicationcontext,Spring Bean,我有一个包含许多servlet的大型java项目。每个servlet都需要 要使用以下命令从同一bean文件获取对象: ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 然后我用 context.getBean("<BEAN_NAME">); context.getBean(“您可以将a与Servlet#init()方法一起使用。当Servlet容器创建Servle

我有一个包含许多servlet的大型java项目。每个servlet都需要 要使用以下命令从同一bean文件获取对象:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
然后我用

     context.getBean("<BEAN_NAME">);
context.getBean(“您可以将a与
Servlet#init()
方法一起使用。当Servlet容器创建Servlet上下文时,它将在任何
ServletContextListener
上调用
contextInitialized()

public class YourServletContextListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // clear context
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // initialize spring context    
        event.getServletContext().setAttribute("context", springContext);
    }
}
此上下文(servlet上下文)中的所有servlet都可以访问这些属性

在Servlet
init()
方法中,您只需获取属性

public class YourServlet implements Servlet {

    @Override
    public void init(ServletConfig config) {
        config.getServletContext().getAttribute("context");
        // cast it (the method returns Object) and use it
    }

    // more
}
您可以将a与
Servlet#init()方法一起使用。当Servlet容器创建Servlet上下文时,它将在任何
ServletContextListener
上调用
contextInitialized()
,您可以在其中初始化应用程序单例/bean/等

public class YourServletContextListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // clear context
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // initialize spring context    
        event.getServletContext().setAttribute("context", springContext);
    }
}
此上下文(servlet上下文)中的所有servlet都可以访问这些属性

在Servlet
init()
方法中,您只需获取属性

public class YourServlet implements Servlet {

    @Override
    public void init(ServletConfig config) {
        config.getServletContext().getAttribute("context");
        // cast it (the method returns Object) and use it
    }

    // more
}

由于您有一个web应用程序,我将使用
spring web
中包含的
WebApplicationContext
,它在web应用程序部署时加载,并在取消部署时正确关闭。您只需在
web.xml
中声明
ContextLoaderListener

<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
优点是,您的上下文在所有servlet之间共享

参考文献:


既然您有一个web应用程序,我将使用
spring web
中包含的
WebApplicationContext
,它在web应用程序部署时加载,在取消部署时正确关闭。您所要做的就是在
web.xml
中声明
ContextLoaderListener

<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
优点是,您的上下文在所有servlet之间共享

参考文献:


    • 您是否考虑过让servlet实现

      然后,您必须将Servlet声明为名为springbean,并在web.xml上使用相同的名称,然后您可以简单地使用@Autowired注释将springbean注入Servlet中

      更多信息请访问

      示例代码:

      
        @Component("myServlet") 
         public class MyServlet implements HttpRequestHandler {
      
              @Autowired
              private MyService myService;
      ...
      
      示例web.xml

      <servlet>     
                  <display-name>MyServlet</display-name>
                  <servlet-name>myServlet</servlet-name>
                  <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet
                 </servlet-class>
          </servlet>
          <servlet-mapping>
                  <servlet-name>myServlet</servlet-name>
                  <url-pattern>/myurl</url-pattern>
          </servlet-mapping>
      
      
      MyServlet
      myServlet
      org.springframework.web.context.support.HttpRequestHandlerServlet
      myServlet
      /myurl
      
      您考虑过让servlet实现吗

      然后,您必须将Servlet声明为名为springbean,并在web.xml上使用相同的名称,然后您可以简单地使用@Autowired注释将springbean注入Servlet中

      更多信息请访问

      示例代码:

      
        @Component("myServlet") 
         public class MyServlet implements HttpRequestHandler {
      
              @Autowired
              private MyService myService;
      ...
      
      示例web.xml

      <servlet>     
                  <display-name>MyServlet</display-name>
                  <servlet-name>myServlet</servlet-name>
                  <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet
                 </servlet-class>
          </servlet>
          <servlet-mapping>
                  <servlet-name>myServlet</servlet-name>
                  <url-pattern>/myurl</url-pattern>
          </servlet-mapping>
      
      
      MyServlet
      myServlet
      org.springframework.web.context.support.HttpRequestHandlerServlet
      myServlet
      /myurl
      
      那么,你会使用
      DispatcherServlet
      和完整的Spring MVC套件吗?是的,我在发布问题之前先用谷歌搜索了一下。在理解自动连线功能时遇到了问题。我习惯于注入xml配置,而不是代码注释。你会使用
      DispatcherServlet
      和完整的MVC套件吗然后是Spring MVC套件?是的,我在发布问题之前先用谷歌搜索了一下。在理解自动连线功能方面有问题。我习惯于注入xml配置,而不是代码注释。通过@nif和你的答案,我可以想出某种解决方案。现在检查一下。感谢@nif和你的答案,我可以想出一些解决方案这是一种解决方案。现在检查一下。谢谢。我不明白什么是MyBeans类。在我使用ApplicationContext获取bean之前。现在我需要为它创建一个类?或者可能我遗漏了什么..ctx.getBean获取bean本身,而不是bean.xml文件?啊,好的..那么我如何确切地告诉它要转到哪个bean文件?@ufk:i e编辑了Spring文档中包含该部分的答案。通过
      contextConfigLocation
      context param
      指定XML/XMLs的位置。ohhhh没有我想象的那么复杂。我不明白MyBeans类是什么。在我使用ApplicationContext获取bean之前。现在我需要为r它?或者可能我遗漏了什么..ctx.getBean获取的是bean本身而不是bean.xml文件?啊哈好吧..那么我该如何准确地告诉它要转到哪个bean文件?@ufk:我编辑了答案以包含Spring文档中的那部分。通过
      contextConfigLocation
      上下文参数
      指定xml/XMLs的位置。ohhhh没有我想象的那么复杂。