账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    spring中这种使用方式classpath如何表示根目录
    60
    0

    1.问题描述
    项目使用maven构建,配置文件都放在src/main/resources下。
    由于项目开发中有人频繁改动web.xml配置,所以项目经理使用了变量(sso.path)来指定外部配置文件,每人都各自使用本地配置文件。
    但问题来了,当我想再切换到src/main/resources目录下的配置时,不知道该怎么表示(当然可以全部注释起来,但改的地方较多)。
    如果存在子目录,倒是很好表示,如classpath:/spring,但我的配置文件都在根目录下,使用classpath: classpath:/ ,classpath://,乱七八糟的都试过,貌似都不起作用。
    求指教!多谢!

    2.代码如下
    web.xml中的配置

        <context-param>
            <!-- 变量 -->
            <param-name>sso.path</param-name>
            <!-- 外部配置文件目录。【问题】如果要表示src/main/resouces下根目录该怎么表示???? -->
            <param-value>file:f:/config</param-value>
        </context-param>
        
        <listener>
            <listener-class>cn.xx.xx.api.sso.listener.ConfigContextListener</listener-class>
        </listener>

    ConfigContextListener实现

    public class ConfigContextListener implements ServletContextListener{
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            String path = sce.getServletContext().getInitParameter("sso.path");
            //设置系统全局变量,以便全局可用
            System.setProperty("sso.path", path);    
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            
        }
    }

    使用:例如,spring-redis.xml中使用(还有很多配置文件都用到了该变量)

        <context:property-placeholder location="${sso.path}/spring/redis.properties"
            ignore-unresolvable="true" />
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 爱していゐ 普通会员 1楼

      在Spring中,我们可以使用classpath来表示根目录。这通常是通过在Spring的配置文件中设置classpath属性来实现的。

      在Spring的主配置文件(如applicationContext.xml或application.yml)中,我们可以设置classpath属性来指定项目的根目录。例如:

      xml <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertySourceFactoryBean"> <property name="file" value="classpath:/path/to/your/directory"/> </bean>

      在这个例子中,我们设置了classpath属性为"classpath:/path/to/your/directory",这意味着Spring将查找这个目录中的资源文件来初始化配置。

      如果你在命令行中运行Spring,你可以使用以下命令来设置classpath:

      bash spring boot run --classpath=classpath:/path/to/your/directory

      这将启动Spring应用,并查找指定目录中的资源文件来初始化配置。

    更多回答
    扫一扫访问手机版