标签:ssh
(1)引入jar包
(2)配置
(3)测试
1、引入jar包
添加User Library,名为spring-core
引入spring-core的jar包(spring-framework-3.2.5.RELEASE) spring-beans-3.2.5.RELEASE.jar spring-context-3.2.5.RELEASE.jar spring-core-3.2.5.RELEASE.jar spring-expression-3.2.5.RELEASE.jar 注意:这里没有添加commons logging的jar包,而是要使用slf4j的日志组件,下面我们会遇到错误 |
2、配置
添加applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="person" class="com.rk.test.entity.Person"></bean> </beans>
3、测试
package com.rk.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.rk.test.entity.Person; public class TestSpring { private ApplicationContext ac; @Before public void init() { ac = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void test() { Person p = (Person) ac.getBean("person"); System.out.println(p); } }
运行后,会遇到这个错误:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
解决方法是引入jcl-over-slf4j-1.7.21.jar (在讲述slf4j的时候提到过这个包)
4、对问题的说明
The implementation of JCL over SLF4J, i.e jcl-over-slf4j.jar, will allow your project to migrate to SLF4J piecemeal, without breaking compatibility with existing software using JCL. Similarly, log4j-over-slf4j.jar and jul-to-slf4j modules will allow you to redirect log4j and respectively java.util.logging calls to SLF4J. http://www.slf4j.org/manual.html |
Bridging legacy APIsOften, some of the components you depend on rely on a logging API other than SLF4J. You may also assume that these components will not switch to SLF4J in the immediate future. To deal with such circumstances, SLF4J ships with several bridging modules which redirect calls made to log4j, JCL and java.util.logging APIs to behave as if they were made to the SLF4J API instead. Gradual migration to SLF4J from Jakarta Commons Logging (JCL)jcl-over-slf4j.jarTo ease migration to SLF4J from JCL, SLF4J distributions include the jar file jcl-over-slf4j.jar. This jar file is intended as a drop-in replacement for JCL version 1.1.1. It implements the public API of JCL but using SLF4J underneath, hence the name "JCL over SLF4J." Our JCL over SLF4J implementation will allow you to migrate to SLF4J gradually, especially if some of the libraries your software depends on continue to use JCL for the foreseeable future. You can immediately enjoy the benefits of SLF4J‘s reliability and preserve backward compatibility at the same time. Just replace commons-logging.jar with jcl-over-slf4j.jar. Subsequently, the selection of the underlying logging framework will be done by SLF4J instead of JCL but without the class loader headaches plaguing JCL. The underlying logging framework can be any of the frameworks supported by SLF4J. Often times, replacing commons-logging.jar with jcl-over-slf4j.jar will immediately and permanently solve class loader issues related to commons logging. http://www.slf4j.org/legacy.html |
Spring使用SLF4J代替Commons Logging写日志 项目的日志更换成slf4j和logback后,发现项目无法启动。错误提示Java.lang.ClassNotFoundException: org.apache.commons.logging.Log,如图所示。原因是Spring默认使用commons logging写日志,需要桥接工具把日志输入重定向到slf4j。在项目中添加commons logging到slf4j的桥接器jcl-over-slf4j即可解决该问题。 http://blog.csdn.net/accountwcx/article/details/47314409 |
标签:ssh
原文地址:http://lsieun.blog.51cto.com/9210464/1834684