最近公司有个客户要求服务器用weblogic,只好把原本在resin上跑的项目改造一下
一.项目改造
1.首先需要在web.xml同级目录下新建weblogic.xml,内容如下
<?xml version="1.0" encoding="UTF-8"?> <weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"> <context-root>dcmsDialerNew</context-root> //实际访问的项目名 <index-directory-enabled>true</index-directory-enabled> <container-descriptor> <!-- 优先使用Web应用里加载的类 防止应用程序的jar包和weblogic下的jar包冲突--> <optimistic-serialization>true</optimistic-serialization> <show-archived-real-path-enabled>true</show-archived-real-path-enabled> <prefer-web-inf-classes>true</prefer-web-inf-classes> </container-descriptor> </weblogic-web-app>
2.需要把数据源改造一下
在设置数据源的bean里(spring-jdbc.xml配置),头添加配置jee,方式也改成jee
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd xmlns:jee="http://www.springframework.org/schema/jee" http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"> <bean id="transactionManager"class="org.springframework.transaction.jta.JtaTransactionManager" /><bean id="txProxyTemplate" abstract="true" <property name="proxyTargetClass"> class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="transactionManager"><ref bean="transactionManager" /></property><value>true</value></property> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <property name="transactionAttributes"><props><prop key="do*">PROPAGATION_REQUIRED,-Exception</prop><prop key="save*">PROPAGATION_REQUIRED,-Exception</prop><prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop><prop key="*">PROPAGATION_REQUIRED,readOnly</prop></props></property></bean><!-- mysql数据库配置(jndi方式) --> <!-- <bean id="mysqlPortal" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/portalDB</value> </property></bean> <bean id="portal" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"><ref bean="mysqlPortal" /></property></bean> <property name="jndiName"> <bean id="mysqlDatasource" class="org.springframework.jndi.JndiObjectFactoryBean"><value>java:comp/env/jdbc/ucds</value></property></bean> </bean> --> <bean id="mysqlJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource"><ref bean="mysqlDatasource" /></property> <!-- mysql数据库配置(jee方式) --> <jee:jndi-lookup id="mysqlDatasource" jndi-name="ucds" resource-ref="false"/> <bean id="mysqlJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"><ref bean="mysqlDatasource" /></property> <jee:jndi-lookup id="mysqlPortal" jndi-name="portalDB" resource-ref="false"/> </bean><bean id="portal" class="org.springframework.jdbc.core.JdbcTemplate"> </beans> <property name="dataSource"><ref bean="mysqlPortal" /></property> </bean>
二.遇到的问题
1.xml-apis jar包冲突
java.lang.ClassCastException: weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory
原因:因为在weblogic.xml里设置了优先加载WEB-INF中的jar,所以要删掉冲突的jar。
解决方法:删掉war包中的xml-apis相关的jar包就可以了
2.找不到配置文件
在resin服务器没问题,部署到weblogic上读取不到配置文件
原因:
部署到weblogic时,是打成war包的,这时src目录就已经消失了,至于classes目录在部署到weblogic中时经过解压的war包,classes目录下是空的,classes下的东西是被打包成 _wl_cls_gen.jar! , 包括配置文件都被压缩了,所以一般的寻找路径的方法,是看不到jar包里面的东西。
解决方法:
1.weblogic中不识别../ ,如果在java中写 ../imagesTemp,那获取真实路径的时候就会出现异常抛出 unsafe path
2.ClassLoader classloader =Thread.currentThread().getContextClassLoader();
//这句是把配置文件直接读进来
InputStream is =classloader.getResourceAsStream(PROPERTIES_FILE);或
InputStream istream = Config.class.getResourceAsStream(PROPERTIES_FILE);//推荐
或InputStream istream=request.getSession().getServletContext().getResourceAsStream("/WEB-INF/classes/SystemConfig.properties");
properties = new Properties();
properties.load(istream);
String ipString = properties.getProperty(key);
InputStream is =classloader.getResourceAsStream("UserInfoZhyl.ini");//这句是把配置文件直接读进来,括号里写的是配置文件的名字。
URL path =classloader.getResource("UserInfoZhyl.ini");//这句就是获得配置文件的URL ,括号里写的是配置文件的名字。
String pathStr = path.toString();//这句是把上面的URL转成字符串
3.没有配置数据源
项目中用到了portalDB数据源,需要在weblogic上配置,不然报以下错误:
weblogic javax.naming.NameNotFoundException: While trying to look up comp/env/jdbc/portalDB in /app/webapp/dcmsDialer.war/1686902893.; remaining name 'comp/env/jdbc/portalDB'
解决方法:在weblogic门户中的服务--》数据源中添加数据源
注:如果项目有跨库操作要在把数据源的事物处理设置为仿真两阶段提交,然后重启weblogic
4.stax-api-1.0.1.jar冲突(貌似带api的jar包都应该删除)
weblogic.xml.stax.EventFactory cannot be cast to javax.xml.stream.XMLEventFactory
解决方法:log4j打印日志不报错,只得重启weblogic,在控制台看日志,经过一天半努力,最终删除stax-api-1.0.1.jar解决
5.java.lang.LinkageError: javax/xml/namespace/QName
还是jar包冲突,可以使用工具jfind找到相应的jar包
解决方法:删除所有带javax/xml/namespace/QName的jar包
以上是我遇到的错误,更多错误:https://my.oschina.net/fyming/blog/112124
关于Java路径问题,这篇文章写的很详细: Java路径问题最终解决方案
读取applicationContext.xml配置文件,这篇文章介绍的很详细:读取WEB-INF 下applicationContext.xml配置文件
相关文章:Class.getResource和ClassLoader.getResource的区别分析