2012-11-01 30 views
6

Tôi đang cố gắng đọc các thuộc tính từ một tệp thuộc tính, tên tệp của nó sẽ khác nhau cho mỗi môi trường của chúng ta, như local.properties, dev. các thuộc tính, vv Các tệp thuộc tính này sẽ chỉ chứa thông tin kết nối cho các cá thể mongodb tương ứng của chúng như host, port và dbname. Thông thường, loại điều này sẽ được thực hiện với định nghĩa JNDI trong máy chủ ứng dụng của chúng tôi, nhưng hiện tại không có triển khai nào cho Mongo.Spring 3.1 contextInitializerClasses không hoạt động trên WebLogic 10.3.6 sử dụng web.xml Ngữ cảnh-Param

Vì tôi đang sử dụng WebLogic 10.3.6, tôi không thể sử dụng đặc tả Servlet 3.0 và do đó không thể sử dụng cấu hình Java cho Spring, chỉ có XML tại thời điểm này. Vì vậy, cách tiếp cận mà tôi đang cố gắng sử dụng là có context context -itextizerClass được định nghĩa trong tệp web.xml của tôi và sau đó đặt nó vào một lớp thực hiện ApplicationContextInitializer và đặt hồ sơ hoạt động theo cách thủ công theo cách thủ công. Tuy nhiên, khi khởi động WebLogic hoặc tại redeploy, không phải đang gọi lớp khởi tạo tùy chỉnh của tôi và hồ sơ của tôi không được thiết lập.

Câu hỏi của tôi là, ngữ cảnh của SpringInitializerClass có phụ thuộc vào Servlet 3.0 hoặc có thứ gì khác mà tôi bị thiếu không?

Mã mà tôi đã xác định:

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> 

Khi tôi cố gắng triển khai ứng dụng, tôi nhận ngoại lệ: NoSuchBeanDefinitionException:No bean named 'deployProperties' is defined sẽ được mong đợi nếu cấu hình không được đặt. Nhật ký của tôi không hiển thị bất kỳ câu lệnh gỡ lỗi nào của tôi được in. Tôi cũng đã cố gắng di chuyển tham số contextInitializerClass thành init-param của DispatcherServlet của tôi, nhưng nó cũng cho kết quả tương tự.

trở ngại của tôi là rằng

  1. tôi không thể thiết lập hồ sơ cá nhân từ bên trong kịch bản Maven của tôi vì công ty chúng tôi sử dụng các vật tương tự để đẩy cho tất cả các môi trường.

  2. Tôi cũng không thể thay đổi phiên bản WebLogic hoặc sử dụng servlet mới nhất thông số kỹ thuật do phụ thuộc vào vùng chứa.

phiên bản hiện tại của tôi là:

  • Xuân 3.1.2.RELEASE
  • WebLogic 10.3.6
  • javax.servlet-api 2,5

Có ai khác nhìn thấy vấn đề này và biết làm thế nào tôi có thể nhận được lớp khởi tạo của tôi nạp? Hay là có cách nào tốt hơn để làm những gì tôi đang cố gắng làm?

này trông có liên quan đến câu hỏi của thành viên khác mà chưa được trả lời: Spring MVC 3.1 Using Profiles for environment specific Hibernate settings

Trả lời

6

Tên của ngữ cảnh param là tôi nghĩ sai, nó phải là contextInitializerClasses không contextInitializerClass, đó có thể là lý do tại sao ApplicationContextInitializer của bạn là không nhận được nhặt

Ngoài ra bạn dường như thiếu sự xâm nhập cho ContextLoaderListener trong file web.xml của bạn, hãy thử thêm rằng quá:

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

Đây là tệp tải lên tệp cấu hình bean xml được chỉ định trong thẻ contextConfigLocation

+0

Thật xấu hổ, bạn đúng về tên, nó cần phải là * Lớp học. Cảm ơn đã giúp đỡ. –

Các vấn đề liên quan