最近换电脑,原本项目从windows系统迁移至mac系统,但是比较奇怪,web项目打成war包之后,部署在tomcat 启动就会报格式错误。具体如下
我的配置如下:
对于的xml文件配置如下:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"
>
<description>redis配置</description>
<context:property-placeholder ignore-unresolvable="true" location="classpath:/redis.properties" />
<!-- redis -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.password}" />
<property name="timeout" value="${redis.timeout}" />
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="usePool" value="true" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
<!-- 将session放入redis -->
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="1800" />
</bean>
非常奇怪,同样的war包我在windows 和Linux上都可以运行,唯独在mac 上运行会报格式错误,放在docker里面运行也会有相同的错误,有没有大神遇到过类似的情况啊!!!!!
https://blog.csdn.net/yiluoak_47/article/details/12980781
https://blog.csdn.net/mingtian625/article/details/49671163
网上的一些方法
1.方法一
用类加载器
InputStream fis =TestProperties.class.getClassLoader().getResourceAsStream(“*.properties”)
2.方法二(示例TestProperties和init.properties在同一目录下)
InputStream fis =TestProperties.class.getResourceAsStream(“*.properties”)
如果不在同一目录下,用../格式或将pro文件放在classpath下,用/*.properties也行,/代表classpath。
3.方法三,对于Web工程也可以这样。
先获取ServletContext,然后
InputStream in=context.getResourceAsStream(“/WEB-INF/classes/init.properties”);
这个没试过。
4.方法四
用new FileInputStream("src/main/resources/*.properties");
FileInputStream("")里的结构是从项目的根文件夹开始的
当在web项目里时又发生错误了,于是我写了个
System.out.println(new File(pathname:".").getAbsolutePath());
看看这个路径是从哪里开始,结果为
C:\Tomcat\apache-tomcat-8.5.20\bin.
是在tomcat的bin里,所以要写“../webapps/项目名/WEB-INF/classes/*.properties”
所以说,当作为webservice部署到tomcat里时,若想使用当前目录,最好先使用System.out.println(new File(".").getAbsolutePath());查看一下当前目录是什么,因为他不一定是你的.class文件存放的目录
但是!!!
由于我的项目时用IDEA建了个maven管理的web项目(在D盘),然后用的本地的tomcat(在C盘),而打包发布的时候默认在D盘的target文件夹里边,所以System.out.println(new File(".").getAbsolutePath());结果在C盘tomcat的bin里没错,但是实际文件在D盘,还是报错。。。。
终极解决方案:在controller到service接口的方法里都加上HttpServletRequest request这个参数,在方法里用
request.getSession().getServletContext().getRealPath("/WEB-INF/classes/rewardSetting.properties");
来获得绝对路径从而解决错误!!!
redis.properties 没有打包在class目录下
尝试先把配置文件放在不同目录下,先判断目录区别在哪