SSH系列:(4)Spring

(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 APIs

Often, 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.jar

To 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

时间: 2024-08-06 08:45:02

SSH系列:(4)Spring的相关文章

【SSH系列】静态代理&amp;&amp;动态代理

从设计模式说起 代理模式是二十三中设计模式中的一种,代理模式就是指由一个代理主题来操作真实的主题,真实的主题执行具体的业务操作,而代理主题负责其她相关业务,简而言之,代理模式可以由以下三个部分组成: a.抽象角色:通过接口或抽象类声明真实角色实现的业务方法. b.代理角色:实现抽象角色,是真实角色的代理,通过真实角色的业务逻辑方法来实现抽象方法,并可以附加自己的操作. c.真实角色:实现抽象角色,定义真实角色所要实现的业务逻辑,供代理角色调用.第一次接触代理模式的是在学习大话设计模式的时候,首先

Spring Data 系列(三) Spring+JPA(spring-data-commons)

本章是Spring Data系列的第三篇.系列文章,重点不是讲解JPA语法,所以跑开了JPA的很多语法等,重点放在环境搭建,通过对比方式,快速体会Spring 对JPA的强大功能. 准备代码过程中,保持了每个例子的独立性,和简单性,准备的源码包,下载即可使用.如果,对JPA语法想深入研究的话,直接下载在此基础上进行测试. 前言 Spring Data 系列(一) 入门:简单介绍了原生态的SQL使用,以及JdbcTemplate的使用,在这里写SQL的活还需要自己准备. Spring Data 系

工作笔记3.手把手教你搭建SSH(struts2+hibernate+spring)环境

上文中我们介绍<工作笔记2.软件开发常用工具> 从今天开始本文将教大家如何进行开发?本文以搭建SSH(struts2+hibernate+spring)框架为例,共分为3步: 1)3个独立配置:struts2. Hibernate. Spring 2)2个整合:整合Sring和struts2. 整合Spring和Hibernate 3)资源分类 开发包.软件.框架源码,已经共享到百度网盘:http://pan.baidu.com/s/1o6FkbA6 一.3个独立配置 1.Struts2: 1

SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式

在java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过spring容器帮我们new指定实例并且将实例注入到需要该对象的类中.依赖注入的另一种说法是"控制反转",通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员,而控制反转是指new实例工作不由我们程序员来做而是交给spring容器来做. Spring依赖注入(

Spring基础系列12 -- Spring AOP AspectJ

Spring基础系列12 -- Spring AOP AspectJ 转载:http://www.cnblogs.com/leiOOlei/p/3613352.html 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某方法. Advisor:Advice和Pointcut的结合单元,以便将Advice和Pointcut分开实现灵活配置. Aspe

Spring基础系列8 -- Spring自动装配bean

Spring基础系列8 -- Spring自动装配bean 转载:http://www.cnblogs.com/leiOOlei/p/3548290.html 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wiring ‘constructor’ 5.      Auto-Wiring ‘autodetect’ Spring Auto-Wiring Be

Spring基础系列6 -- Spring表达式语言(Spring EL)

Spring基础系列6 -- Spring表达式语言(Spring EL) 转载:http://www.cnblogs.com/leiOOlei/p/3543222.html 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂表达式,存取对象属性.对象方法调用等.所有的SpEL都支持XML和Annotation两种方式,格式:#{ SpEL exp

Spring基础系列9 -- Spring AOP

Spring基础系列9 -- Spring AOP 转载:http://www.cnblogs.com/leiOOlei/p/3556054.html Spring AOP即Aspect-oriented programming,面向切面编程,是作为面向对象编程的一种补充,专门用于处理系统中分布于各个模块(不同方法)中的交叉关注点的问题.简单地说,就是一个拦截器(interceptor)拦截一些处理过程.例如,当一个method被执行,Spring AOP能够劫持正在运行的method,在met

[Spring系列02]Spring AOP模拟

在博文[Spring系列01]Spring IOC/DI模拟中简略模拟了Spring IOC/DI的实现原理,本文接着模拟了Spring AOP的实现原理. 代码结构图如下: 全部代码如下: UserDAO.java package com.ctsh.dao; import com.ctsh.model.User; public interface UserDAO { public void save(User user); public void delete(); } UserDAO Use

[Spring系列01]Spring IOC/DI模拟

本文以一个简单的实例大致模拟Spring IOC/DI的运行原理,代码简单分dao,model,service三层.即:dao 与数据库的操作,增删改查等方法model 一般都是javabean对象,例如与数据库的某个表相关联.service 供外部调用,等于对dao,model等进行了包装. 程序结构图如下: 先粘贴部分代码,再进行解释: UserDAO.java package com.ctsh.dao; import com.ctsh.model.User; public interfac