Spring笔记(一)

Spring应用

一、        Spring基础

  在本部分别,将介绍Spring框架的两个核心特性:反向控制(IOC)和面向切面编程(AOP)。

1、  首先,简单介绍Spring中的IOCAOP

2、  其次,装配Bean,介绍如何利用IOC实现系统对象间的松耦合关系,如何使用XML在Spring容器中定义系统对象,装配其依赖类。

3、  创建切面,介绍Spring的AOP把系统级服务(如安全和监控)从服务对象中解耦出来

1、    Spring简介

1)Spring特点

   Spring是一个轻量级的locAOP容器框架。

a)         轻量级:从大小及系统开支上说。且Spring是非侵入式的(基于Spring开发的系统中对象一般不依赖于Spring的类)

b)        反向控制:使用loc对象是被动接收依赖而不是主动去找(容器在实例化对象是主动将其依赖类注入给它)

c)         面向切面:将业务逻辑从系统服务中分离,实现内聚开发。系统对象只做其该做的业务业务逻辑不负责其他系统问题(如日志和事务支持)

d)        容器:包括且管理系统对象的生命周期和配置,通过配置设定Bean是单一实例还是每次请求产生一个,并设定Bean之间的关联关系

e)         框架:使用简单组件配置组合成一个复杂的系统,系统中的对象是通过XML文件配置组合起来的,且Spring提供了很多基础功能(事务管理,持久层集成等)

2)Spring模块

  

 

AOP

Module

O/R

Mapping

Module

Web Context

and utility

Module

 

 

MVC

framework

JDBC and

DAO

Module

Application

Context

Module

 

Core container and Supporting Utilities

Spring框架由7个模块组成:

a)         核心容器:提供了基础功能。包括BeanFactory类(Spring框架的核心,采用工厂模式实现Loc

b)        应用上下文模块:扩展了Beanfactory,添加了对I18N(国际化)、系统生命周期事件及验证的支持,并提供许多企业级服务,如电子邮件、JNDI访问、EJB集成、远程调用及定时服务,并支持与模板框架(如VelocityFreeMarker)的集成。

c)         AOP模块:对面向切面提供了丰富的支持模式Spring应用系统开发切面的基础;并引入metadata编程

d)        JDBCDAO模块

e)         O/R映射模块:

f)         Web模块:建立在应用上下文模块的基础上,提供了合适web系统的上下文,另外,该模块支持多项面web任务,如透明处理多文件上传请求,自动将请求参数绑定到业务对象中等。

g)        MVC框架:所有模块都是建立在核心容器上的,容器规定如何创建、配置和管理Bean,以及其细节。

3)搭建Spring应用开发环境

a)      Spring的下载和安装

               Spring当前GA版本是3.0.5,登录到http://www.springsource.org/ 点,单击Powloads链接进入download页面。然后单击Download链接,逐步进入真正的下载页面。

                       建议下载spring-framework-3.0.5-with-dependencies.zip包,这个压缩中不仅含Spring自身的所有应用包,还含有Spring编译和运行所依赖的第三方类库以及第三方开源框架的应用包。

               在应用程序中使用Spring时,需要将spring.jar添加到项目的类路径中,另外还需要添加日志处理包commons-logging.jar以及它的实现包log4j-1.2.15jar(还需要添加他的配置文件log4j.properties)。至于其他的第三方类库,可以在具体需要使用时再加入。

b)     添加Spring配置文件

一般来说,使用Spring时会在项目的类路径(CLASSPATH)中添加Spring配置文件来声明Spring要管理的内容。通常把这个文件取名为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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

                        <!--配置由Spring来管理的Bean-->

                 <bean id="gdi" class="cn.csdn.dao.GenericDaoImpl">

                  <property name="say" value="O(_)O哈哈~"></property>

                 </bean>

</beans>

4)简单示例

首先导入必须的包commons-logging.jarspring.jar

GreetDao.java

package cn.csdn.dao;

 

public interface GreetDao {

         void sayGreet();

}

GreetDaoImpl.java

package cn.csdn.dao;

 

public class GreetDaoImpl implements GreetDao{

    private String say;

    @Override

    public void sayGreet() {

       System.out.println("Spring say:"+say);

    }

    public void setSay(String say) {

       this.say = say;

    }

}

GreetService.java

package cn.csdn.service;

 

public interface GreetService {

   void sayGreet();

}

 

GreetServiceImpl.java

package cn.csdn.service;

 

import cn.csdn.dao.GreetDaoImpl;

 

public class GreetServiceImpl implements GreetService{

    private GreetDaoImpl greetDaoImpl;

    @Override

    public void sayGreet() {

       greetDaoImpl.sayGreet();

    }

    public void setGreetDaoImpl(GreetDaoImpl greetDaoImpl) {

       this.greetDaoImpl = greetDaoImpl;

    }

}

GreetTest.java

package cn.csdn.junit;

 

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.csdn.service.GreetServiceImpl;

 

public class GreetTest {

 @Test

 public void test1(){

     ApplicationContext  ac = new ClassPathXmlApplicationContext("applicationContext.xml");

     GreetServiceImpl gsi=(GreetServiceImpl) ac.getBean("greetServiceImpl");

     gsi.sayGreet();

 }

}

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"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

                          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

                        ">

<bean id="greetDaoImpl" class="cn.csdn.dao.GreetDaoImpl">

    <property name="say">

        <value>hello</value>

      </property>

</bean>   

<bean id="greetServiceImpl" class="cn.csdn.service.GreetServiceImpl">

    <property name="greetDaoImpl" ref="greetDaoImpl"></property>

</bean>                       

</beans>

整理自尚硅谷视频教程springboot高级篇,并增加部分springboot2.x的内容 一、Spring Boot与缓存 一、JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。 • CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可 以在运行 期访问多个CachingProvider。 • CacheManager定义了创建、配置、获取、管理和控制多个唯一命名 的Cache,这些Cache 存在于CacheManager的上下文中。一个CacheManager仅被一个 CachingProvider所拥有。 • Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个 Cache仅被一个 CacheManager所拥有。 • Entry是一个存储在Cache中的key-value对。 • Expiry 每一 个存储在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期 的状态。一旦过期,条 目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。 二、Spring缓存抽象 Spring从3.1开始定义了org.springframework.cache.Cache 和 org.springframework.cache.CacheManager接口来统一不同的缓存技术; 并支持使用JCache(JSR- 107)注解简化我们开发; • Cache接口为缓存的组件规范定义,包含缓存的各种操作集合; • Cache接 口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache 等; • 每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否 已经被调用 过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法 并缓存结果后返回给用户。下 次调用直接从缓存中获取。 • 使用Spring缓存抽象时我们需要关注以下两点; 1、确定方法需要被缓存 以及他们的缓存策略 2、从缓存中读取之前缓存存储的数据 Cache 缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、 ConcurrentMapCache等 CacheManager 缓存管理器,管理各种缓存(Cache)组件 @Cacheable 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @CacheEvict 清空缓存 @CachePut 保证方法被调用,又希望结果被缓存。 @EnableCaching 开启基于注解的缓存 keyGenerator 缓存数据时key生成策略 serialize 缓存数据时value序列化策略 @CacheConfig 抽取缓存的公共配置 三、几个重要概念&缓存注解 1、常用注解 2、常用参数 名字 位置 描述 示例 methodName root object 当前被调用的方法名 #root.methodName method root object 当前被调用的方法 #root.method.name target root object 当前被调用的目标对象 #root.target targetClass root object 当前被调用的目标对象类 #root.targetClass args root object 当前被调用的方法的参数列表 #root.args[0] 3、常用参数SPEL说明 名字 位置 描述 示例 caches root object 当前方法调用使用的缓存列表(如 @Cacheable(value= {"cache1","cache2"}) ), 则有两 个cache #root.caches[0].name argument name evaluation context 方法参数的名字. 可以直接 #参数 名 ,也可以使用 #p0或#a0 的形 式,0代表参数的索引; #iban 、 #a0 、 #p0 result evaluation context 方法执行后的返回值(仅当方法执 行之后的判断有效,如‘unless’ , ’cache put’的表达式 ’cache evict’的表达式 beforeInvocation=false ) #result 四、代码中使用缓存 1、搭建基本环境 1、导入数据库文件 创建出department和employee表 2、创建javaBean封装数据 3、整合MyBatis操作数据库 1.配置数据源信息 2.使用注解版的MyBatis; 1)、@MapperScan指定需要扫描的mapper接口所在的包
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值