用tomcat8部署一web工程啓動時報錯以下:java
Caused by: java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:65) at com.foo.util.rds.Subscriber.afterPropertiesSet(Subscriber.java:93) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509) ... 20 more
該web工程依賴的日誌jar包有以下幾個web
其中slf4j-log4j13-1.0.1中的LoggerFactory 65行代碼以下 spring
loggerFactory = StaticLoggerBinder.SINGLETON.getLoggerFactory();
而slf4j-api-1.7.6中也有LoggerFactory,可是它倆內容不一樣, slf4j-api-1.7.6中LoggerFactory的65行內容爲api
static final String UNSUCCESSFUL_INIT_MSG = "org.slf4j.LoggerFactory could not be successfully initialized. See also " + UNSUCCESSFUL_INIT_URL;
另外slf4j-log4j13-1.0.1中存在StaticLoggerBinder,其中SINGLETON爲公有靜態屬性tomcat
public static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
而slf4j-jdk14-1.7.7和logback-classic-1.1.2中也有StaticLoggerBinder, 且他們中的SINGLETON爲私有的jvm
private static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
若是用的是slf4j-log4j13-1.0.1中的LoggerFactory,但StaticLoggerBinder用的又是其餘jar包的,就會報上述錯誤ide
在tomcat的啓動腳本(catalina.sh)中設置了jvm參數打印類加載信息(JAVA_OPTS="$JAVA_OPTS -XX:+TraceClassLoading"),果真如上所述.net
[Loaded org.slf4j.LoggerFactory from file:/home/zhuguowei/shicaigj/scgj-admin/target/scgj-admin/WEB-INF/lib/slf4j-log4j13-1.0.1.jar] [Loaded org.slf4j.impl.StaticLoggerBinder from file:/home/zhuguowei/shicaigj/scgj-admin/target/scgj-admin/WEB-INF/lib/slf4j-jdk14-1.7.7.jar]
而在tomcat7中能成功部署, 看一下tomcat7中的加載信息
日誌
[Loaded org.slf4j.LoggerFactory from file:/home/zhuguowei/shicaigj/scgj-admin/target/scgj-admin/WEB-INF/lib/slf4j-api-1.7.6.jar] ... [Loaded org.slf4j.impl.StaticLoggerBinder from file:/home/zhuguowei/shicaigj/scgj-admin/target/scgj-admin/WEB-INF/lib/logback-classic-1.1.2.jar]
解決方法code
參考一個開源項目(springside)修改root-pom 將依賴的slf4jjar包修改成
<slf4j.version>1.7.8</slf4j.version> <logback.version>1.1.2</logback.version> <!-- LOGGING begin --> <!-- slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <!-- logback --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <!-- 代碼直接調用log4j會被橋接到slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> <!-- 代碼直接調用commons-logging會被橋接到slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> <!-- 代碼直接調用java.util.logging會被橋接到slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> <version>${slf4j.version}</version> </dependency <!-- LOGGING end -->
補充
該web工程用tomcat8不能成功加載, 緣由是slf4jjar包衝突, 目前的解決方法是改用了tomcat7
但用了tomcat7 出現了中文亂碼的問題, 須要修改server.xml
在Connector元素中添加一個屬性: URIEncoding="UTF-8"
以下所示:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
參考了該博客:
http://blog.csdn.net/renfufei/article/details/11294917
而tomcat8不須要顯式作此設置