(一)简介
spring 是一个轻量级的框架。IOC和AOP是Spring的两大核心。
IOC:Inversion of Control。控制反转。比如一个类A要调用另一个类B的方法,我们需要new出一个类B的对象b,使用类的对象b调用类B里面的方法。之前创建对象的权力在类A手中。如果我们使用IOC,经过一定的配置,Spring会自动创建类B的对象,提供给类A。这样,控制类创建的主权反过来了,所以也叫控制反转。
AOP:Aspect Oriented Programming。面向切面编程。它的思想是:我们拓展功能不通过修改代码来实现。
大致的概念就这样,我们先有一个基本的了解,后续的博客会陆续介绍。
(二)搭建Spring环境
一、导入jar包。 点此下载
二、创建Sping的核心配置文件——applicationContext.xml,并且引入约束。Spring的核心配置文件名称和位置都可以不固定,建议大家建立在src文件下面,因为服务器启动时默认加载src下的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:context = "http://www.springframework.org/schema/context" xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:tx = "http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> </ beans >
三、在web.xml中添加Spring的核心监听器。
lt;listener> < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > lt;/listener> < context-param > < param-name > contextConfigLocation </ param-name > < param-value > classpath:applicationContext.xml </ param-value > lt;/context-param>
目的是为了服务器启动时,为每个项目创建一个ServletContext对象,使用监听器可以监听ServletContext对象在什么时候创建的,并且在ServletContext对象创建时加载Spring的核心配置文件。
如果不配置监听器,我们需要在每个action启动的时候收到加载applicationContext.xml。
代码如下:
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" );
这样每个action启动都要创建一个对象,这就大大降低了我们的性能。
四、配置log4j。创建log4j.properties文件,到src文件下,项目启动时会自动加载该目录下的log4j文件。
### direct log messages to stdout ### log4j.appender.stdout = org .apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System .out log4j.appender.stdout.layout = org .apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file = org .apache.log4j.FileAppender log4j.appender.file.File = c :/mylog.log log4j.appender.file.layout = org .apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern =%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger = info , stdout
目的是为了我们可以查看更加信息的日志信息。
五、配置c3p0连接池。(这一步并不属于Spring的范畴,在这里也跟大家介绍一下,方便以后SSH三大框架整合)
封装数据库 信息。一般新建一个jdbc.properties文件,写数据库连接信息。
在src下新建一个jdbc.properties文件
jdbc.driver = com .mysql.jdbc.Driver jdbc.url = jdbc :mysql:///*** jdbc.user = *** jdbc.password =***
在a pplicationContext.xml里面加载c3p0连接池
< context:property-placeholder location = "classpath:jdbc.properties" /> < bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" > < property name = "driverClass" value = "${jdbc.driver}" /> < property name = "jdbcUrl" value = "${jdbc.url}" /> < property name = "user" value = "${jdbc.user}" /> < property name = "password" value = "${jdbc.password}" /> </ bean >
至此,Spring环境配置结束。
(三)测试
下面我们创建两个类测试一下我们的环境,也熟悉一下Spring是如何创建对象的。
首先我们新建一个User类,在里面写一个方法add吧
public class User { public void add(){ System.out.println("add方法执行" ); } }
然后我们在applicationContext.xml之后配置,得到User的对象
< bean id = "user" class = "User类的全路径" > </ bean >
然后写个单元测试方法,直接调用User里面的add方法即可