主题 : JEECMS中如何得到Spring 的ApplicationContext?
级别: 秀才
UID: 22917
积分:79 加为好友
威望: 0 精华: 0
主题:5 回复:34
注册时间:2011-08-19
在线时长:0
1#   发表于:2012-06-27 16:39:14  IP:59.126.*.*
如何在JEECMs 代码中取得ApplicationContext,从而得到任何bean引用?

application-context.xml中这册了这个bean:


    <bean id="applicationContextProvider" class="com.jeecms.common.util.ApplicationContextProvider"/>



对应的ApplicationContextProvider 类,实现ApplicationContextAware接口,希望在Spring 启动时自动注入ApplicationContext


    public class ApplicationContextProvider implements ApplicationContextAware
    {
    public void setApplicationContext(ApplicationContext contex)
    throws BeansException
    {
    AppContext.setApplicationContext(contex);   
    }

    }




    public class AppContext {

        private static ApplicationContext ctx;

        /**
         * Injected from the class "ApplicationContextProvider" which is automatically
         * loaded during Spring-Initialization.
         */
        public static void setApplicationContext(ApplicationContext applicationContext) {
            ctx = applicationContext;
        }

        /**
         * Get access to the Spring ApplicationContext from everywhere in your Application.
         *
         * @return
         */
        public static ApplicationContext getApplicationContext() {
            return ctx;
        }
    }



但是调用AppContext.getApplicationContext()时得到的是Null。
使用的是tomcat6.0 
级别: 状元
UID: 33380
积分:50625 加为好友
威望: 21 精华: 3
主题:88 回复:30687
注册时间:2012-05-07
在线时长:0
2#   发表于:2012-06-27 16:41:45  IP:115.214.*.*
回复第1楼 你这使用的版本是什么?
功成由勤,业精于勤。
级别: 秀才
UID: 22917
积分:79 加为好友
威望: 0 精华: 0
主题:5 回复:34
注册时间:2011-08-19
在线时长:0
3#   发表于:2012-06-27 16:53:09  IP:59.126.*.*
我使用的是3.X
级别: 状元
UID: 33380
积分:50625 加为好友
威望: 21 精华: 3
主题:88 回复:30687
注册时间:2012-05-07
在线时长:0
4#   发表于:2012-06-27 16:56:29  IP:115.214.*.*
回复第3楼 application-context.xml中这册了这个bean: 
你把这个bean删除了,肯定获取不到,你自动配置改了哪里?
功成由勤,业精于勤。
级别: 秀才
UID: 22917
积分:79 加为好友
威望: 0 精华: 0
主题:5 回复:34
注册时间:2011-08-19
在线时长:0
5#   发表于:2012-06-27 17:23:53  IP:59.126.*.*
没有进行太多的改动,我就是在application-context.xml中,添加了
 <bean id="applicationContextProvider"class="com.jeecms.common.util.ApplicationContextProvider"/>
  
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
        System.out.println("setApplicationContext");
        context = applicationContext;
    }  
  
    public static ApplicationContext getApplicationContext() {  
        System.out.println("getApplicationContext"+context.getDisplayName());
        return context;  
    } 

在新建的Bean中,调用
ApplicationContextProvider.getApplicationContext();

但是,得到的是Null。 
级别: 状元
UID: 33380
积分:50625 加为好友
威望: 21 精华: 3
主题:88 回复:30687
注册时间:2012-05-07
在线时长:0
6#   发表于:2012-06-27 17:31:27  IP:115.214.*.*
Spring 启动时自动注入ApplicationContext 
是需要在web.xml里面配置的,你直接调用它怎么找的到?
功成由勤,业精于勤。
级别: 秀才
UID: 22917
积分:79 加为好友
威望: 0 精华: 0
主题:5 回复:34
注册时间:2011-08-19
在线时长:0
7#   发表于:2012-06-28 10:36:16  IP:59.126.*.*
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>JeeCms3</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/application-context.xml
/WEB-INF/config/cache-context.xml
/WEB-INF/config/captcha-context.xml
/WEB-INF/config/jeecore-context.xml
/WEB-INF/config/jeecms-context.xml
/WEB-INF/config/szhtpcore-context.xml
</param-value>
</context-param>
<filter>
<filter-name>processTime</filter-name>
<filter-class>com.jeecms.common.web.ProcessTimeFilter</filter-class>
</filter>
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter>
<filter-name>osivFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>processTime</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>osivFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>processTime</filter-name>
<url-pattern>*.jspx</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.jspx</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>osivFilter</filter-name>
<url-pattern>*.jspx</url-pattern>
<!-- <dispatcher>FORWARD</dispatcher>   -->
</filter-mapping>
<filter-mapping>
<filter-name>processTime</filter-name>
<url-pattern>*.jhtml</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.jhtml</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>osivFilter</filter-name>
<url-pattern>*.jhtml</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>processTime</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>osivFilter</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
<!--  <filter-mapping>
<filter-name>processTime</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>osivFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>-->
 
<servlet>
<servlet-name>JeeCmsAdmin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/jeecms-servlet-admin.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>JeeCmsFront</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/jeecms-servlet-front.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Jcaptcha</servlet-name>
<servlet-class>com.jeecms.common.captcha.JcaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jcaptcha</servlet-name>
<url-pattern>/captcha.svl</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DbFile</servlet-name>
<servlet-class>com.jeecms.core.action.front.DbFileServlet</servlet-class>
</servlet>
<servlet>
        <servlet-name>showComImg</servlet-name>
        <servlet-class>com.szhtp.core.servlet.ShowComImageServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>showComImg</servlet-name>
    <url-pattern>/showComImg.do</url-pattern>
    </servlet-mapping>
<servlet-mapping>
<servlet-name>JeeCmsAdmin</servlet-name>
<url-pattern>/jeeadmin/jeecms/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>JeeCmsFront</servlet-name>
<url-pattern>*.jhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>JeeCmsFront</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
<!-- <servlet-mapping>
<servlet-name>JeeCmsFront</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping> -->
<servlet-mapping>
<servlet-name>JeeCmsFront</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DbFile</servlet-name>
<url-pattern>/dbfile.svl</url-pattern>
</servlet-mapping>
<servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.shtml</welcome-file>
<welcome-file>index.jhtml</welcome-file>
</welcome-file-list>
<error-page>
<error-code>403</error-code>
<location>/WEB-INF/error/403.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<mime-mapping>
<extension>rar</extension>
<mime-type>application/zip</mime-type>
</mime-mapping>
<mime-mapping>
<extension>doc</extension>
<mime-type>application/zip</mime-type>
</mime-mapping>

<!-- <filter>
            <filter-name>UrlRewriteFilter</filter-name>
            <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
            <init-param>
                <param-name>logLevel</param-name>
                <param-value>WARN</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>UrlRewriteFilter</filter-name>
            <url-pattern>/*</url-pattern>
         </filter-mapping>  -->
</web-app>
级别: 秀才
UID: 22917
积分:79 加为好友
威望: 0 精华: 0
主题:5 回复:34
注册时间:2011-08-19
在线时长:0
8#   发表于:2012-06-28 10:36:58  IP:59.126.*.*
这是我的web.xml配置
级别: 状元
UID: 33380
积分:50625 加为好友
威望: 21 精华: 3
主题:88 回复:30687
注册时间:2012-05-07
在线时长:0
9#   发表于:2012-06-28 10:42:19  IP:218.60.*.*
回复第8楼 你好!这属于二次开发, 修改源码不在技术支持内,你要需要可以联系我们售前人员,我们提供功能定制服务。
功成由勤,业精于勤。
1 共1页