WEB-INF / myproject-servlet.xml与WEB-INF / web.xml [英] WEB-INF/myproject-servlet.xml versus WEB-INF/web.xml

查看:196
本文介绍了WEB-INF / myproject-servlet.xml与WEB-INF / web.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几天修复Spring项目中的bug。很长一段时间,错误日志中的主要问题是:

  Bean已存在

我有两个文件:

  WEB -INF / myproject-servlet.xml 
WEB-INF / web.xml

首先一,我可以提供以下输入(让我们假设我有动物园管理动物的申请):

 < context: component-scan base-package =com.my.package.animals/> 

有了这个(以及我理解)我们正在启用Spring bean自动发现。所以现在,当我们运行我们的应用程序时,Spring将从该包中获取所有类,稍后它将遍历 resources 目录中的所有配置文件,并将初始化所有bean(放置在配置文件,与给定的包相关联。)



第二个, web.xml 包括类似的行这个

 < context-param> 
< param-name> contextConfigLocation< / param-name>
< param-value> /WEB-INF/myproject-servlet.xml< / param-value>
< / context-param>

我也可以将路径放到我的配置文件中,例如:

 < context-param> 
< param-name> contextConfigLocation< / param-name>
< param-value> classpath:animals-config.xml< / param-value>
< / context-param>

现在,我在 myproject-servlet中进行了自动检测。 xml 我在 web.xml 中为相同的对象提供了 context-param



我的问题是,是否有可能错误Bean已经存在来自于此?我几乎可以肯定是的,我检查了所有bean ID并且没有重复。



所以我有另一个问题。这样做的好方法是什么?当我创建新的配置文件时,我应该在哪里通知我的应用程序?在 myproject-servlet.xml web.xml 中。我真的需要清理我的应用程序,我将从那开始。



我检查了一些例子,人们宁可不要多放一个< ; context-param> in web.xml file 简单示例



提前谢谢






好的,我非常接近解决我的问题。



我们假设我有两个套餐:

  com.my.pckg.a 
com.my.pckg.b

with classes

  com.my.pckg.a.ClassA 
com.my.pckg .b.ClassB

我添加了 myproject-servlet.xml 以下行:

 < context:component-scan base-package =com.my.pckg.a /> 

我有一个配置文件 myconfig.xml 和里面我有基于类 ClassA ClassB 的bean。



假设我们有豆类ID如下:

  ClassA:ida1,ida2 
ClassB:idb1,idb2

所以,我正在运行我的码头服务器,问题是:



将初始化哪些bean?我只声明了包 com.my.pckg.a ,所以从 myconfig.xml ,spring应仅加载 ida1 ida2 但此文件还包含另一个类的bean。 / p>

最后......?






最后,我想我发现了一个问题。在 web.xml 文件中,我有一行:

 < context:component-scan base-package =com.dirty.pckg/> 

此套餐我有一个类 DirtyClass 使用 @Controller 注释。这个类的一个字段是:

  private static ApplicationContext context = new ClassPathXmlApplicationContext(dirty-config.xml); 

因此,当我的应用程序启动时,Spring需要 DirtyClass (因为它有 @ )并映射它。因为os static 上下文修饰符,它会触发读取 dirty-config.xml 。这是我无法理解为什么我的代码以奇怪的方式运行的方式。

解决方案

web.xml file是Web应用程序的配置。它与Spring无关。



contextConfigLocation context-param是一个用于搜索Spring的Spring侦听器配置文件。它与spring有关。



您可以在 web.xml 中注册多个弹簧配置文件但这些文件不能定义相同的bean(bean id必须不同)。您也可以只有一个弹簧配置文件,它将包含其他配置文件,如下所述: http://www.mkyong.com/spring/load-multiple-spring-bean-configuration-file/






回答其他问题:



当您添加组件扫描时,您要求spring扫描包 com.my.pckg.a 用于注释,如@ Service,@ Component,...组件扫描不是配置的其余部分的过滤器,它是配置本身。所以你添加组件扫描的事实,不会改变myconfig.xml的行为。 ida1,ida2和idb1,idb2都将被实例化。



我真的没有得到你想要用配置文件完成的任务。也许如果你解释了你的需求,我们可以帮你为你设置正确的配置。


I spent few days fixing bugs in my Spring project. For really long time, my main problems in error log were:

Bean already exists

I have got two files:

WEB-INF/myproject-servlet.xml
WEB-INF/web.xml

in first one, I can put following input (let's assume I have got an application to manage animals in zoo):

<context:component-scan base-package="com.my.package.animals" />

with that (as well as I understand) we are enabling Spring beans auto-discovery. So now, when we run our application, Spring will take all classes from this package, later it will go through all config files in resources directory and will inititliaze all beans (placed in config files, which are associated with a given package).

The second one, web.xml includes lines like this

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/myproject-servlet.xml</param-value>
</context-param>

I can also put path to my config files, for instance:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:animals-config.xml</param-value>
</context-param>

So now, I have 'auto-detecting' in myproject-servlet.xml and I have got context-param in web.xml for the same objects.

My question is, is it possible, that errors "Bean already exists" are coming from this? I am almost sure that yes, I checked all beans ids and there is no duplicates.

So I have another question. What is a good approach of doing that? When I create new config file, where I should inform my application about that? In myproject-servlet.xml or web.xml. I really need to clean up my application and I will start with that.

I checked some examples and people rather do not put more than one <context-param> in web.xml file Simple example

Thank you in advance


Ok, I am really close to solve my problem.

Let's assume I have got two packages:

com.my.pckg.a
com.my.pckg.b

with classes

com.my.pckg.a.ClassA
com.my.pckg.b.ClassB

I added in myproject-servlet.xml following line:

<context:component-scan base-package="com.my.pckg.a" />

I have got a config file myconfig.xml and inside I have got beans based on classes ClassA and ClassB.

Let's say we have got beans with ids as follows:

ClassA: ida1, ida2
ClassB: idb1, idb2

So, I am running my jetty server and the question is:

Which beans will be initialized? I declared only package com.my.pckg.a, so from myconfig.xml, spring should load only ida1 and ida2 but this file includes also beans for another class.

So finally... ?


Finally, I suppose I find a problem. In web.xml file I have got a line:

<context:component-scan base-package="com.dirty.pckg" />

in this package I have got a class DirtyClass with @Controller annotation. One of the fields of this class is:

private static ApplicationContext context = new ClassPathXmlApplicationContext("dirty-config.xml");

So, when my application is getting up, Spring takes DirtyClass (because it has @) and maps it. Because os static modifier of context it triggers reading dirty-config.xml. That's way I could not understand why my code behaves in strange way.

解决方案

The web.xml file is the configuration of your web application. It is not related to Spring.

The contextConfigLocation context-param is the one spring listener use to search for Spring configuration files. It is spring related.

You can have multiple spring configuration files that you register in your web.xml but these files must not define the same bean (bean id must be different). You can also have only one spring configuration file that will it self include other configuration files as described here : http://www.mkyong.com/spring/load-multiple-spring-bean-configuration-file/


Answer to the other question :

When you added the component scan, you ask spring to scan the package com.my.pckg.a for annotation like @Service, @Component, ... The component scan is not a filter for the rest of the configuration it is a configuration itself. So the fact that you added component scan, will not change the behaviour of myconfig.xml. Both ida1, ida2 and idb1, idb2 will be instanciated.

I don't really get what you are trying to accomplished with your configuration files. Maybe if you explain your needs, we could help you set up the right config for you.

这篇关于WEB-INF / myproject-servlet.xml与WEB-INF / web.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆