spring是容器框架,它可以管理多个层,web层,业务层,dao层,持久层
概念1:bean即Java的任何一种对象
概念2:ioc(inverse of controll)即控制反转:将创建对象和维护对象的权利交给配置文件而不是程序代码
概念3:di(dependency injection)即依赖注入:将各种通用组件功能(如写日志功能),看作一个bean对象,到时相互引用bean即可
概念4:aop(Aspect Oriented Programming)即面向切面编程
一、下载springframework包,http://repo.spring.io/release/org/springframework/spring/
1.下载....RELEASE-dist.zip,解压后里面包含3个文件夹:docs是api和说明文档、libs是各种功能的jar包、schema是各种功能的xml配置文件
2.jar包的说明:(其中javadoc包是文档,sources包是源码)
Spring AOP:Spring的面向切面编程,提供AOP(面向切面编程)的实现
Spring Aspects:Spring提供的对AspectJ框架的整合
Spring Beans:Spring IOC的基础实现,包含访问配置文件、创建和管理bean等。
Spring Context:在基础IOC功能上提供扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持。
Spring Context Support:Spring context的扩展支持,用于MVC方面。
Spring Core:Spring的核心工具包
Spring expression:Spring表达式语言
Spring Instrument:Spring对服务器的代理接口
Spring Instrument Tomcat:Spring对tomcat连接池的集成
Spring JDBC:对JDBC 的简单封装
Spring JMS:为简化jms api的使用而做的简单封装
Spring Messaging:为集成messaging api和消息协议提供支持
Spring orm:整合第三方的orm实现,如hibernate,ibatis,jdo以及spring 的jpa实现
Spring oxm:Spring对于object/xml映射的支持,可以让JAVA与XML之间来回切换
Spring test:对JUNIT等测试框架的简单封装
Spring tx:为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。
Spring web:包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。
Spring webmvc:包含SpringMVC框架相关的所有类。包含国际化、标签、Theme、视图展现的FreeMarker、JasperReports、Tiles、Velocity、XSLT相关类。当然,如果你的应用使用了独立的MVC框架,则无需这个JAR文件里的任何类。
Spring webmvc portlet:Spring MVC的增强
Spring websocket:Html5新增加特性
二、创建一个spring核心配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->
<context:component-scan base-package="com.eduoinfo.finances.bank.web"></context:component-scan>
<!-- 引入jdbc配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- dataSource 配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />
</bean>
<!-- mybatis文件配置,扫描所有mapper文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml" p:mapperLocations="classpath:com/eduoinfo/finances/bank/web/dao/*.xml" />
<!-- spring与mybatis整合配置,扫描所有dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.eduoinfo.finances.bank.web.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" />
<!-- 对dataSource 数据源进行事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
<!-- 配置使Spring采用CGLIB代理 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 启用对事务注解的支持 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Cache配置 -->
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory" />
</beans>
三、获取配置文件中的bean
(1)Applicationcontext对象获取,它会预加载所有单例的bean:
①Applicationcontext ac = new ClassPathXmlApplicationContext("beans.xml");//类路径
②Applicationcontext ac = new FileSystemXmlApplicationContext("src/beans.xml");//文件路径
③Applicationcontext ac = new XmlWebApplicationContext("beans.xml");//web中加载
ac.getBean("lei");
(2)bean工厂获取:
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beans.xml"));
bf.getBean("lei");
(3)在配置bean的时候可以加入参数,让bean以不同的方式实例化
<bean scope="singleton/prototype/request/session/global-session">
singleton:单例,只实例化一次(默认模式)
prototype:多例,每次都要实例化
request:web开发请求一次有效,每次都要实例化
session:web开发的session有效
global-session:web开发spring容器中永久存在只实例化一次
<bean/>
四、配置bean
1.基本注入(set注入,构造函数注入)
2.集合注入(list,set,map)
3.自动注入(autowire = no,byName,byType,constructor,autodetect)
4.内部bean(bean中定义另一个bean,或者<ref bean="bean名称">)
5.继承bean(parent)
6.注解(<context:annotation-config />启用注解,@Resource)
7.spring自带的bean(如:property读取外部文件信息)
五、AOP面向切面编程
(1)AOP核心概念:
1、横切关注点:对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
2、切面(aspect):类是对物体特征的抽象,切面就是对横切关注点的抽象
3、连接点(joinpoint):被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
4、切入点(pointcut):对连接点进行拦截的定义
5、通知(advice):所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、环绕、异常、引入通知五类
6、目标对象:代理的目标对象
7、织入(weave):将切面应用到目标对象并导致代理对象创建的过程
8、引入(introduction):在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
(2)编程步骤:
1.定义一个接口
public interface Hello()
{
public void SayHello();
}
2.定义实现接口类,即目标对象
public class HelloAOP implements Hello
{
public void SayHello()
{
System.out.println("你好!AOP");
}
}
3.定义通知类,即横切关注点
public class TimeHandler
{
public void printTime()
{
System.out.println("时间:" + System.currentTimeMillis());
}
}
4.配置aop.xml文件
<bean id="helloAOP" class="com.aop.HelloAOP" />
<bean id="timeHandler" class="com.aop.TimeHandler" />
<aop:config>
<aop:aspect id="time" ref="timeHandler">
<aop:pointcut id="pointcut" expression="execution(* com.aop.Hello.*(..))" /> <!--切点,*表示Hello开头的类-->
<aop:before method="printTime" pointcut-ref="pointcut" /> <!--前置通知,方法前调用-->
<aop:after method="printTime" pointcut-ref="pointcut" /> <!--后置通知,方法后调用-->
<aop:after-returning method="afterReturning" pointcut-ref="pointcut"/> <!--返回通知,方法后调用-->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/> <!--异常通知,方法发生异常时调用-->
<aop:around method="around" pointcut-ref="pointcut"/> <!--环绕通知,方法前后调用-->
</aop:aspect>
</aop:config>
5.调用之后,会在打印结果的前和后分别打印出时间
public static void main(String[] args)
{
ApplicationContext ac = new ClassPathXmlApplicationContext("aop.xml");
Hello ha = (Hello)ac.getBean("helloAOP");//这里要注意转成接口
ha.SayHello();
}
概念1:bean即Java的任何一种对象
概念2:ioc(inverse of controll)即控制反转:将创建对象和维护对象的权利交给配置文件而不是程序代码
概念3:di(dependency injection)即依赖注入:将各种通用组件功能(如写日志功能),看作一个bean对象,到时相互引用bean即可
概念4:aop(Aspect Oriented Programming)即面向切面编程
一、下载springframework包,http://repo.spring.io/release/org/springframework/spring/
1.下载....RELEASE-dist.zip,解压后里面包含3个文件夹:docs是api和说明文档、libs是各种功能的jar包、schema是各种功能的xml配置文件
2.jar包的说明:(其中javadoc包是文档,sources包是源码)
Spring AOP:Spring的面向切面编程,提供AOP(面向切面编程)的实现
Spring Aspects:Spring提供的对AspectJ框架的整合
Spring Beans:Spring IOC的基础实现,包含访问配置文件、创建和管理bean等。
Spring Context:在基础IOC功能上提供扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持。
Spring Context Support:Spring context的扩展支持,用于MVC方面。
Spring Core:Spring的核心工具包
Spring expression:Spring表达式语言
Spring Instrument:Spring对服务器的代理接口
Spring Instrument Tomcat:Spring对tomcat连接池的集成
Spring JDBC:对JDBC 的简单封装
Spring JMS:为简化jms api的使用而做的简单封装
Spring Messaging:为集成messaging api和消息协议提供支持
Spring orm:整合第三方的orm实现,如hibernate,ibatis,jdo以及spring 的jpa实现
Spring oxm:Spring对于object/xml映射的支持,可以让JAVA与XML之间来回切换
Spring test:对JUNIT等测试框架的简单封装
Spring tx:为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。
Spring web:包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。
Spring webmvc:包含SpringMVC框架相关的所有类。包含国际化、标签、Theme、视图展现的FreeMarker、JasperReports、Tiles、Velocity、XSLT相关类。当然,如果你的应用使用了独立的MVC框架,则无需这个JAR文件里的任何类。
Spring webmvc portlet:Spring MVC的增强
Spring websocket:Html5新增加特性
二、创建一个spring核心配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->
<context:component-scan base-package="com.eduoinfo.finances.bank.web"></context:component-scan>
<!-- 引入jdbc配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- dataSource 配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />
</bean>
<!-- mybatis文件配置,扫描所有mapper文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml" p:mapperLocations="classpath:com/eduoinfo/finances/bank/web/dao/*.xml" />
<!-- spring与mybatis整合配置,扫描所有dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.eduoinfo.finances.bank.web.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" />
<!-- 对dataSource 数据源进行事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
<!-- 配置使Spring采用CGLIB代理 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 启用对事务注解的支持 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Cache配置 -->
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory" />
</beans>
三、获取配置文件中的bean
(1)Applicationcontext对象获取,它会预加载所有单例的bean:
①Applicationcontext ac = new ClassPathXmlApplicationContext("beans.xml");//类路径
②Applicationcontext ac = new FileSystemXmlApplicationContext("src/beans.xml");//文件路径
③Applicationcontext ac = new XmlWebApplicationContext("beans.xml");//web中加载
ac.getBean("lei");
(2)bean工厂获取:
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beans.xml"));
bf.getBean("lei");
(3)在配置bean的时候可以加入参数,让bean以不同的方式实例化
<bean scope="singleton/prototype/request/session/global-session">
singleton:单例,只实例化一次(默认模式)
prototype:多例,每次都要实例化
request:web开发请求一次有效,每次都要实例化
session:web开发的session有效
global-session:web开发spring容器中永久存在只实例化一次
<bean/>
四、配置bean
1.基本注入(set注入,构造函数注入)
2.集合注入(list,set,map)
3.自动注入(autowire = no,byName,byType,constructor,autodetect)
4.内部bean(bean中定义另一个bean,或者<ref bean="bean名称">)
5.继承bean(parent)
6.注解(<context:annotation-config />启用注解,@Resource)
7.spring自带的bean(如:property读取外部文件信息)
五、AOP面向切面编程
(1)AOP核心概念:
1、横切关注点:对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
2、切面(aspect):类是对物体特征的抽象,切面就是对横切关注点的抽象
3、连接点(joinpoint):被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
4、切入点(pointcut):对连接点进行拦截的定义
5、通知(advice):所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、环绕、异常、引入通知五类
6、目标对象:代理的目标对象
7、织入(weave):将切面应用到目标对象并导致代理对象创建的过程
8、引入(introduction):在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
(2)编程步骤:
1.定义一个接口
public interface Hello()
{
public void SayHello();
}
2.定义实现接口类,即目标对象
public class HelloAOP implements Hello
{
public void SayHello()
{
System.out.println("你好!AOP");
}
}
3.定义通知类,即横切关注点
public class TimeHandler
{
public void printTime()
{
System.out.println("时间:" + System.currentTimeMillis());
}
}
4.配置aop.xml文件
<bean id="helloAOP" class="com.aop.HelloAOP" />
<bean id="timeHandler" class="com.aop.TimeHandler" />
<aop:config>
<aop:aspect id="time" ref="timeHandler">
<aop:pointcut id="pointcut" expression="execution(* com.aop.Hello.*(..))" /> <!--切点,*表示Hello开头的类-->
<aop:before method="printTime" pointcut-ref="pointcut" /> <!--前置通知,方法前调用-->
<aop:after method="printTime" pointcut-ref="pointcut" /> <!--后置通知,方法后调用-->
<aop:after-returning method="afterReturning" pointcut-ref="pointcut"/> <!--返回通知,方法后调用-->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/> <!--异常通知,方法发生异常时调用-->
<aop:around method="around" pointcut-ref="pointcut"/> <!--环绕通知,方法前后调用-->
</aop:aspect>
</aop:config>
5.调用之后,会在打印结果的前和后分别打印出时间
public static void main(String[] args)
{
ApplicationContext ac = new ClassPathXmlApplicationContext("aop.xml");
Hello ha = (Hello)ac.getBean("helloAOP");//这里要注意转成接口
ha.SayHello();
}