【Spring】(一)Spring入门及原理

来源:互联网 发布:金山数据恢复收费吗 编辑:程序博客网 时间:2024/05/29 12:36

(一)简介

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文件。

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.     http://www.springframework.org/schema/context  
  10.     http://www.springframework.org/schema/context/spring-context.xsd  
  11.     http://www.springframework.org/schema/aop  
  12.     http://www.springframework.org/schema/aop/spring-aop.xsd  
  13.     http://www.springframework.org/schema/tx   
  14.     http://www.springframework.org/schema/tx/spring-tx.xsd">  
  15.       
  16.       
  17. </beans>  


三、在web.xml中添加Spring的核心监听器。

[html] view plain copy
 print?
  1. <!-- 配置Spring的核心监听器 -->  
  2. lt;listener>  
  3. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4. lt;/listener>  
  5. <context-param>  
  6. <param-name>contextConfigLocation</param-name>  
  7. <param-value>classpath:applicationContext.xml</param-value>  
  8. lt;/context-param>  

目的是为了服务器启动时,为每个项目创建一个ServletContext对象,使用监听器可以监听ServletContext对象在什么时候创建的,并且在ServletContext对象创建时加载Spring的核心配置文件。

如果不配置监听器,我们需要在每个action启动的时候收到加载applicationContext.xml。

代码如下:

[java] view plain copy
 print?
  1. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  

这样每个action启动都要创建一个对象,这就大大降低了我们的性能。

四、配置log4j。创建log4j.properties文件,到src文件下,项目启动时会自动加载该目录下的log4j文件。

[html] view plain copy
 print?
  1. ### direct log messages to stdout ###  
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  3. log4j.appender.stdout.Target=System.out  
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  5. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  
  6.   
  7. ### direct messages to file mylog.log ###  
  8. log4j.appender.file=org.apache.log4j.FileAppender  
  9. log4j.appender.file.File=c:/mylog.log  
  10. log4j.appender.file.layout=org.apache.log4j.PatternLayout  
  11. log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  
  12.   
  13. ### set log levels - for more verbose logging change 'info' to 'debug' ###  
  14.   
  15. log4j.rootLogger=info, stdout  

目的是为了我们可以查看更加信息的日志信息。

 

五、配置c3p0连接池。(这一步并不属于Spring的范畴,在这里也跟大家介绍一下,方便以后SSH三大框架整合)

封装数据库信息。一般新建一个jdbc.properties文件,写数据库连接信息。

在src下新建一个jdbc.properties文件

[html] view plain copy
 print?
  1. jdbc.driver = com.mysql.jdbc.Driver  
  2. jdbc.url = jdbc:mysql:///***  
  3. jdbc.user = ***  
  4. jdbc.password =***  
在applicationContext.xml里面加载c3p0连接池

[html] view plain copy
 print?
  1. <!-- 配置连接池: -->  
  2.     <!-- 引入外部属性文件 -->  
  3.     <context:property-placeholder location="classpath:jdbc.properties"/>  
  4.     <!-- 配置C3P0连接池: -->  
  5.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  6.         <property name="driverClass" value="${jdbc.driver}"/>  
  7.         <property name="jdbcUrl" value="${jdbc.url}"/>  
  8.         <property name="user" value="${jdbc.user}"/>  
  9.         <property name="password" value="${jdbc.password}"/>  
  10.     </bean>  

至此,Spring环境配置结束。

(三)测试

下面我们创建两个类测试一下我们的环境,也熟悉一下Spring是如何创建对象的。

首先我们新建一个User类,在里面写一个方法add吧

[java] view plain copy
 print?
  1. public class User {  
  2.    public void add(){  
  3.     System.out.println("add方法执行");  
  4.   }  
  5. }  

然后我们在applicationContext.xml之后配置,得到User的对象

[html] view plain copy
 print?
  1. <bean id="user" class="User类的全路径"></bean>  

然后写个单元测试方法,直接调用User里面的add方法即可


0 0
原创粉丝点击