草庐IT

java - Spring 3.1 contextInitializerClasses 不能使用 web.xml Context-Param 在 WebLogic 10.3.6 上工作

coder 2023-05-05 原文

我正在尝试从属性文件中读取属性,其文件名对于我们的每个环境都不同,例如 local.properties、dev.properties 等。这些属性文件将仅包含其对应 mongodb 的连接信息主机、端口和数据库名称等实例。通常这种事情会在我们的应用服务器中使用 JNDI 定义来完成,但目前还没有针对 Mongo 的实现。

由于我使用的是 WebLogic 10.3.6,我无法使用 Servlet 3.0 规范,因此无法使用 Spring 的 Java 配置,目前只能使用 XML。因此,我尝试使用的方法是在我的 web.xml 中定义一个 contextInitializerClass 上下文参数,然后将其设置为实现 ApplicationContextInitializer 并手动设置 Spring Activity 配置文件的类。但是,在 WebLogic 启动或重新部署时,都没有调用我的自定义初始化程序类,我的配置文件也没有设置。

我的问题是,Spring 的 contextInitializerClass 是否依赖于 Servlet 3.0,或者我还缺少什么?

我定义的代码:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
    <param-name>contextInitializerClass</param-name>
    <param-value>com.myapp.spring.SpringContextProfileInit</param-value>
</context-param>

<!-- Location of spring context config -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</context-param>
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>      
...

SpringContextProfileInit.java

public class SpringContextProfileInit implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {

    private static final Logger log = LoggerFactory.getLogger(SpringContextProfileInit.class);

    public SpringContextProfileInit() {
        log.debug("Got the constructor");
    }

    @Override
    public void initialize(ConfigurableWebApplicationContext ctx) {
        ConfigurableWebEnvironment environ = ctx.getEnvironment();
        log.debug("Got the environment, no profiles should be set: "+ environ.getActiveProfiles());

        /*
        * Here I am setting the profile with a hardcoded name.  In the real app,
        * I would read from a separate properties file, always named app.properties
        * which would live on the app server's classpath.  That app.properties file
        * would contain a property directing the Spring Profile to use.
        */
        environ.setActiveProfiles("local");
        log.debug("Now should be set to local: "+ environ.getActiveProfiles());
        ctx.refresh();
    }

}

servlet-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
    xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<context:property-placeholder properties-ref="deployProperties" />
...
<beans profile="local">
    <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"
            p:location="WEB-INF/local.properties" />
</beans>
<beans profile="beast, dev">
    <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"
            p:location="WEB-INF/dev.properties" />
</beans>
</beans>

当我尝试部署应用程序时,出现异常: NoSuchBeanDefinitionException: 没有定义名为“deployProperties”的 bean 如果未设置配置文件,这将是预期的。我的日志没有显示我的任何调试语句都已打印。我还尝试将 contextInitializerClass 参数移动为我的 DispatcherServlet 的 init-param,但这给出了相同的结果。

我的限制是

  1. 我无法在我的 Maven 脚本中设置配置文件,因为我们的 公司使用相同的工件推送到所有环境。

  2. 我也无法更改 WebLogic 的版本或使用最新的 servlet 规范,因为它依赖于容器。

我目前的版本是:

  • Spring 3.1.2.RELEASE
  • WebLogic 10.3.6
  • javax.servlet-api 2.5

有其他人看到这个问题并且知道如何加载我的初始化程序类吗?还是有更好的方法来做我想做的事情?

这看起来与另一个尚未回答的发帖者的问题有关:Spring MVC 3.1 Using Profiles for environment specific Hibernate settings

最佳答案

我认为上下文参数的名称是错误的,它应该是 contextInitializerClasses 而不是 contextInitializerClass ,这可能是您的 ApplicationContextInitializer 没有被拾取的原因

另外,您的 web.xml 文件中似乎缺少 ContextLoaderListener 条目,请尝试添加:

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

这是加载 contextConfigLocation 标签中指定的 bean 配置 xml 文件的文件

关于java - Spring 3.1 contextInitializerClasses 不能使用 web.xml Context-Param 在 WebLogic 10.3.6 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13179160/

有关java - Spring 3.1 contextInitializerClasses 不能使用 web.xml Context-Param 在 WebLogic 10.3.6 上工作的更多相关文章

  1. ruby - []如何在Ruby中的类上工作 - 2

    我看到我可以使用获取目录中的文件列表Dir["*"]我应该如何准确阅读该语法?据我所知,您可以使用[]从数组或散列中获取值。[]如何处理通话? 最佳答案 []只是一个方法,如#to_s、#object_id。等你可以在任何对象上定义它:classCoolClassdef[](v)puts"hello#{v}"endendCoolClass.new["John"]#=>"helloJohn"在你的情况下,它被定义为单例方法,以这种方式:classDirdefself.[](v)...endend

  2. ruby-on-rails - "Rake spec"大多数测试失败,但 "rails s"在 Diaspora 源上工作正常 - 2

    我已经克隆了Diaspora的源代码,并且我有一个可以正常运行的本地pod,它似乎可以正常运行。但是当我运行$rakespec时,一些初始测试通过了,然后所有测试都开始失败。此外,我发现有趣的是,每次运行rakespec时,它们都会在不同的点失败。它们都因错误而失败:AnerroroccurredinanafterhookActiveRecord::StatementInvalid:PG::ConnectionBad:PQsocket()can'tgetsocketdescriptor:ROLLBACKoccurredat/home/darshan/.rvm/gems/ruby-2.0

  3. ruby-on-rails - Phusion Passenger 不在 Apache 上工作 - 2

    更新:当输入“passenger-memory-stats”时,我显示:---Passengerprocesses---Processes:0我该如何解决这个问题?为什么即使我在httpd.conf中添加它并重新启动apache,passenger也不会启动?我无法让PhusionPassenger在服务器上运行RubyonRails。我已经按照Phusion网站上的所有说明安装了passenger并修改并创建了ApacheVirtualHost以指向新目录并验证所有.conf文件都已成功加载。还加载了httpd-Mpassenger_module。我还在本地主机上成功运行了Passe

  4. ruby - load 在本地路径上工作,require 不 - 2

    加载器.rbputs'>Thisisthesecondfile.'加载演示.rbputs'Thisisthefirst(master)programfile.'load'loadee.rb'puts'Andbackagaintothefirstfile.'当我运行"rubyloaddemo.rb"时,效果很好。这两个文件都在同一个目录中,这就是我运行的目录。但是,如果我将负载更改为要求,无论有无扩展名,我都会得到::29:in`require':nosuchfiletoload--loadee.rb(LoadError)from:29:in`require'fromloaddemo.r

  5. Facebook登录不在签名的APK上工作 - 2

    我生成了密钥哈希keytool.exe-exportcert-Aliaskey0-keystored:\androidCode\keystor\example.jks|C:\openssl\bin\opensslsha1-binary|C:\openssl\bin\opensslbase64它会生成一个钥匙,但不起作用。尝试生成也无法正常工作的代码。我的应用程序尚未发布。它在调试模式下正常工作。看答案转到“应用程序评论”部分,并确保您的应用程序目前已播放并向公众使用。在FacebookKeyHashes中放置您的调试和发布密钥。如果它不起作用,当您尝试在Android中登录时,Facebook

  6. javascript - 如何让 DeviceOrientationEvent 和 DeviceMotionEvent 在 Safari 上工作? - 2

    我正在尝试在我的网站上实现DeviceOrientationEvent和DeviceMotionEvent以获得3D效果。但是,控制台不会记录任何信息,显然iOS13需要用户设置权限才能开始执行此操作。我似乎无法弄清楚如何正确设置它。我做了一些研究,这是我发现的:https://github.com/w3c/deviceorientation/issues/57#issuecomment-498417027遗憾的是,在线提供的所有其他方法都无法再使用。window.addEventListener('deviceorientation',function(event){console.

  7. javascript - 试图让 highchart 示例在本地机器上工作 - 2

    我是highcharts/jquery的新手,我试图将html和js复制到我本地驱动器上的两个文件中,看看我是否可以让它在我的浏览器中呈现。我的基本问题是如何使用jfiddle中highcharts提供的示例代码之一并让它在我的本地机器上运行?来自highcharts.com的示例代码在此处的jfiddle中运行:http://jsfiddle.net/m3MVk/$(function(){varchart;$(document).ready(function(){chart=newHighcharts.Chart({chart:{renderTo:'container',type:'

  8. javascript - .each 不在数组上工作。但是 .isArray 返回 true? - 2

    我创建了一个包含一些项目的对象,其中一个包含多个对象,每个对象都包含一个数组。这是它的结构。$.myVar={cp:"",ps:{m1:["001","002","003"],m2:["002","004"]}};我的脚本不断崩溃,提示$.myVar.ps["m1"]每个都没有方法。当我进入Chrome的控制台进行调查时,我运行以下命令并获得显示的输出。$.myVar.ps["m1"]["001","002","003"]$.myVar.ps["m1"].each(function(i,p){alert(i)})TypeError:Object001,002,003hasnometho

  9. javascript - 如何使 angular js 可拖动元素在平板电脑上工作(触摸) - 2

    我正在研究AngularJS可拖动指令。我的代码类似于此Plunker当使用鼠标操作时,我的代码和这个plunker在Windows上都能完美运行。但是在涉及触摸的任何选项卡上,它根本不起作用。这背后的原因可能是什么?知道如何让它发挥作用吗? 最佳答案 实现了一个指令来控制触摸事件,它是ngTouch。您可以在此处找到更多信息:http://docs.angularjs.org/api/ngTouch另外,我认为这篇文章会有所帮助:JavascriptDraganddropfortouchdevices

  10. javascript - CORS plugin/--disable-web-security 如何在浏览器上工作? - 2

    我确定我不是唯一使用过/使用过CORSplugins的人对于浏览器或--disable-web-security标志,同时对外部(甚至内部)API端点进行API调用。我使用这个插件来进行谷歌地图相关的API调用。但在同一个应用程序中,ParseSDKAPI调用不需要CORS或--disable-web-security标志。我的问题是:为什么这些端点的行为不同以及CORS插件如何解决问题(即使我们无法控制这些API)?提前致谢。 最佳答案 好吧,那个插件的所作所为是非常不负责任的;它实际上禁用了同源策略,该策略强制特定源上的网站只能

随机推荐