使用spring获取项目的绝对路径,就算打JAR包一样获得当前项目的绝对路径

简介:

搞死了,哈哈,不过弄出来了



核心代码

				ClassPathResource cpr = new ClassPathResource(Constant.CONFIG_PATH_KEY);
				System.out.println("------********----"+cpr.getFile().getPath());
				configPath = cpr.getFile().getPath();



下面开始贴工具包和我自己配置的全局拦截器的JAR


package com.hansci.client.util;

/**
 * <project>hansci_search</project>
 * <package>com.hansci.search.util</package>
 * <class>PropertiesLoader.java</class>
 * @time:2013-2-26 涓嬪崍12:26:59
 *
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.struts2.ServletActionContext;
import org.springframework.core.io.ClassPathResource;



public final class PropertiesLoader {

	private static final long serialVersionUID = 3594717377354174859L;

	private PropertiesLoader() {
	}

	public static Properties getProperties(String propertyFileName)
			throws IOException {
		InputStream is = null;
		String configPath = "";
		try {
			if (propertyFileName == null || "".equals(propertyFileName)) {
//				 configPath =new Thread().getContextClassLoader().getClass().getResource("/").getPath()+Constant.CONFIG_PATH_KEY;
//				 System.out.println("--------configPath="+configPath);
				ClassPathResource cpr = new ClassPathResource(Constant.CONFIG_PATH_KEY);
				System.out.println("------********----"+cpr.getFile().getPath());
				configPath = cpr.getFile().getPath();
//				configPath = ServletActionContext.getServletContext()
//						.getRealPath("/") + Constant.CONFIG_PATH_KEY;
			} else {
				//configPath = ServletActionContext.getServletContext().getRealPath("/") + Constant.CONFIG_PATH_KEY;
				configPath="";
			}
			File file = null;
			if (configPath != null && configPath.trim().length() != 0) {
				file = new File(configPath);
				is = new FileInputStream(file);
			} else {
				is = PropertiesLoader.class.getResourceAsStream(propertyFileName);
			}
			//
			if (is == null) {
				throw new FileNotFoundException(configPath + File.separator
						+ propertyFileName + " not found");
			}
			// load properties
			Properties props = new Properties();
			props.load(is);
			return props;
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static int getIntegerProperty(Properties p, String name,
			int defaultValue) {
		String l = p.getProperty(name);
		return l == null ? defaultValue : Integer.valueOf(l).intValue();
	}

	public static String getStringProperty(Properties p, String name,
			String defaultValue) {
		String propertyValue = p.getProperty(name);
		return propertyValue == null ? defaultValue : propertyValue;
	}

	public static boolean getBooleanProperty(Properties p, String name,
			boolean defaultValue) {
		String propertyValue = p.getProperty(name);
		return propertyValue == null ? defaultValue : Boolean
				.valueOf(propertyValue);
	}
	
/*	public static void main(String[] args){
	PropertiesLoader propertiesLoader = new PropertiesLoader();
	Properties p;
	try {	
		
		p = propertiesLoader.getProperties("");//鍙傛暟涓虹┖鏃? 璁块棶resource.properties鏂囦欢
		String strFlowName=propertiesLoader.getStringProperty(p, "URL","null") ;
		System.out.println(strFlowName);
		strFlowName=propertiesLoader.getStringProperty(p, "AUTHORITY_LOGIN_PATH","null") ;
		System.out.println(strFlowName);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
	
}*/
}



package com.hansci.client.action.authority;



import java.io.IOException;
import java.util.Properties;

import com.hansci.client.util.Constant;
import com.hansci.client.util.PropertiesLoader;
import com.hansci.client.webservice.LoginAuthService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class UserAuthorityInterceptor extends AbstractInterceptor {


	private static final long serialVersionUID = -3929030161999051802L;

	
	public String intercept(ActionInvocation invocation) throws Exception {
        //创建ActionContext实例
		@SuppressWarnings("unused")
		ActionContext ctx = ActionContext.getContext();
		//获取user-client中得到的用户登录的sessionID的属性
    	LoginAuthService service = new LoginAuthService();
    	
    	Properties p;
    	
    	String loginPath="";
    	String operatePath="";
    	try {	
    		
    		p = PropertiesLoader.getProperties("");//鍙傛暟涓虹┖鏃? 璁块棶resource.properties鏂囦欢
    		loginPath=PropertiesLoader.getStringProperty(p, Constant.AUTHORITY_LOGIN_PATH,"null") ;
    		System.out.println(loginPath);
    		operatePath=PropertiesLoader.getStringProperty(p, Constant.AUTHORITY_OPERATE_PATH,"null") ;
    		System.out.println(operatePath);
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	
    	
        System.out.println(service.login(loginPath));
    	if(null!=service.login(loginPath)&&!service.login(loginPath).equalsIgnoreCase("null")) {//如果没有登录,则需要返回重新登录
    		return invocation.invoke();
    	}else {
    		return Action.LOGIN;
    	}
	}

}






目录
相关文章
|
8天前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
40 7
|
2月前
|
Java Maven
java项目中jar启动执行日志报错:no main manifest attribute, in /www/wwwroot/snow-server/z-server.jar-jar打包的大小明显小于正常大小如何解决
在Java项目中,启动jar包时遇到“no main manifest attribute”错误,且打包大小明显偏小。常见原因包括:1) Maven配置中跳过主程序打包;2) 缺少Manifest文件或Main-Class属性。解决方案如下:
679 8
java项目中jar启动执行日志报错:no main manifest attribute, in /www/wwwroot/snow-server/z-server.jar-jar打包的大小明显小于正常大小如何解决
|
2月前
|
JSON 前端开发 Java
|
2月前
|
存储 JSON 前端开发
【Spring项目】表白墙,留言板项目的实现
本文主要介绍了表白墙项目的实现,包含前端和后端代码,以及测试
|
2月前
|
缓存 前端开发 Java
【Spring】——SpringBoot项目创建
SpringBoot项目创建,SpringBootApplication启动类,target文件,web服务器,tomcat,访问服务器
|
3月前
|
Java Kotlin 索引
学习Spring框架特性及jiar包下载
Spring 5作为最新版本,更新了JDK基线至8,修订了核心框架,增强了反射和接口功能,支持响应式编程及Kotlin语言,引入了函数式Web框架,并提升了测试功能。Spring框架可在其官网下载,包括文档、jar包和XML Schema文档,适用于Java SE和Java EE项目。
48 0
|
3月前
|
存储 运维 安全
Spring运维之boot项目多环境(yaml 多文件 proerties)及分组管理与开发控制
通过以上措施,可以保证Spring Boot项目的配置管理在专业水准上,并且易于维护和管理,符合搜索引擎收录标准。
79 2
|
3月前
|
Java 应用服务中间件 Maven
Maven的三种项目打包方式——pom,jar,war的区别
Maven 提供了多种打包方式,分别适用于不同类型的项目。pom 用于父项目或聚合项目,便于项目的结构和依赖管理;jar 用于Java类库或可执行的Java应用程序;war 则专用于Java Web应用程序的部署。理解这些打包方式的用途和特点,可以帮助开发者更好地配置和管理Maven项目,确保构建和部署过程的顺利进行。无论是单模块项目还是多模块项目,选择合适的打包方式对于项目的成功至关重要。
406 3
|
Java Android开发 C++
springMvc2-spring jar包下载
springMvc2-spring jar包下载
145 0