org.spring 注解framework jar 4.3.2.release 最新注解版怎么添加数据

阅读(8447)
最原始的进行缓存的方式:
最原始的使用缓存的方式是通过一个全局map保存获取过的数据,下次获取数据时先从map中提取,如果有就直接返回,如果没有就从数据库中去读取,然后放入map中,当然,在做更新操作时需要同步更新这个map中的数据。这种方式虽然原始,但是在一些简单的场景下已经够用了,比如Java的类加载器就是使用的这种方式缓存加载过的class。
通过ehcache以编程方式使用缓存:
跟上面的方式相同,但是缓存通过ehcache去管理,当然比使用map有N多种好处,比如缓存太大了快达到上限之后,将哪一部分缓存清除出去。这种方式完全是通过代码的方式使用ehcache缓存,虽然自由,却也很麻烦;有些比如单纯的场景下,不需要如此麻烦,直接通过注解就行了。
以前在Spring项目中要通过注解的方式使用缓存,比如借助一个jar包:ehcache-spring-annotations.jar,可以在googlecode上面下载,不过已经很久没有更新过了。
使用ehcache-spring-annotations.jar
Spring配置文件:
&?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:ehcache= "http://ehcache-spring-/svn/schema/ehcache-spring"
xsi:schemaLocation= "
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://ehcache-spring-/svn/schema/ehcache-spring http://ehcache-spring-/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"&
&ehcache:annotation-driven cache-manager ="ehcacheManager" /&
&ehcache:config cache-manager = "ehcacheManager"&
&ehcache:evict-expired-elements
interval= "60" /&
&/ehcache:config &
&bean id = "ehcacheManager"
class= "org.springframework.cache.ehcache.EhCacheManagerFactoryBean" &
&property name = "configLocation" value= "classpath:ehcache.xml" /&
共有两个注解:
@Cacheabele
:指定方法使用缓存
@TriggersRemove
:从缓存中移除对象
在查询的方法上使用缓存:
@Cacheable(cacheName="platform.config")
在删除或更新的方法上清空缓存:
@TriggersRemove(cacheName="platform.config", when=When.AFTER_METHOD_INVOCATION , removeAll=true)
public void updateConfig(SystemConfig config) {
this.configDao.update(config);
对于这种方式,这里不做详细探讨。
在Spring 3.1以前,必须借助以上jar包才能支持以注解的方式使用缓存,但是从Spring 3.1开始,已经提供了原生的注解支持,当然也更加强大。
Spring 3.1及以上版本的原生注解支持
Spring配置文件:
&?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:cache= "http://www.springframework.org/schema/cache"
xsi:schemaLocation= "
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"&
&cache:annotation-driven cache-manager ="ehcacheCacheManager" /&
&bean id = "ehcacheCacheManager" class= "org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref= "ehcacheManager" /&
&bean id = "ehcacheManager"
class= "org.springframework.cache.ehcache.EhCacheManagerFactoryBean" &
&property name = "configLocation" value= "classpath:ehcache.xml" /&
Spring原生的缓存的注解共有四个:
@Cacheable
:应用到读取数据的方法上,即可缓存的方法,如查找方法:先从缓存中读取,如果没有再调用方法获取数据,然后把数据添加到缓存中
@CacheEvict :即应用到移除数据的方法上,如删除方法,调用方法时会从缓存中移除相应的数据
:应用到写数据的方法上,如新增/修改方法,调用方法时会自动把相应的数据放入缓存
:上面三种注解配置方法时,一个方法只能使用三者之一。如果要使用多个,则需要使用@Caching
前奏:缓存的key的生成:
如果在Cache注解上没有指定key的话,会使用KeyGenerator生成一个key,默认提供了DefaultKeyGenerator生成器(Spring4之后使用SimpleKeyGenerator)。
如果只有一个参数,就使用该参数作为key,否则使用SimpleKey作为key。
可以在Cache注解上指定key的生成规则,通过SpEL表达式指定规则。详细可以参考:
如果没有入参,又没有指定key,那么key将是“SimpleKey []”,这将很容易造成缓存的混乱,而且也不利于在更新的时候进行缓存的更新或移除。
在查询的方法上使用缓存:
@Cacheable(value="platform.config" )
public String querySingleConfigValue(String configKey) {
指定key的生成规则:
@Cacheable(value="platform.config", value="#configKey")
public String querySingleConfigValue(String configKey) {
在更新的方法上更新缓存:
@CachePut(value=PlatformPrivateConstants.CACHE_NAME_CONFIG, key="#config.configKey")
public SystemConfig updateConfig(SystemConfig config) {
通过"#config.configKey"指定以入参config对象的configKey属性值作为缓存的key。
更新的方法必须将要缓存的对象作为返回值,只有这个返回的对象才会放入缓存中,如果在返回值被返回之前篡改了其内部数据,也会被反映到缓存中。
指定正确的key以防止缓存混乱:
下面两个方法,前者通过configKey查询对象,后者通过configKey查询对象中的configValue属性值:
public SystemConfig querySingleConfig(String configKey) {
public String querySingleConfigValue(String configKey) {
如果不指定key,或者使用相同的key,比如:
@Cacheable(value="platform.config")
public SystemConfig querySingleConfig(String configKey) {
@Cacheable(value="platform.config")
public String querySingleConfigValue(String configKey) {
如果以"DEVELOPER_NAME"作为configKey分别调用这两个方法,那么,先调用哪个方法,则那个方法的返回值会以"DEVELOPER_NAME"作为key存入缓存中,此时调用这两个方法时都会从缓存中取得这个值。比如先调用了querySingleConfig方法,那么查询到的SystemConfig对象就会以"DEVELOPER_NAME"为key存入缓存中,然后调用querySingleConfigValue方法,因为仍会以"DEVELOPER_NAME"作为key从缓存中提取数据,这时就会从缓存中取得了刚才那个SystemConfig对象,却当作是字符串型的configValue属性值返回了。
所以比如为这两个方法指定不同的缓存key:
@Cacheable(value="platform.config", key = "#configKey.concat('_object'))
public SystemConfig querySingleConfig(String configKey) {
@Cacheable(value="platform.config")
public String querySingleConfigValue(String configKey) {
这样前者使用入参configKey值后接"_object"作为key,而后者使用入参configKey值为key,就不会相互覆盖了。
在更新的方法上同时更新多个缓存:
在查询时,可能通过id查询也可能通过name查询,这样在缓存中就存有两份数据,在更新的时候就必须同时更新这两份缓存。
@Cacheable(value="platform.config", key = "#configId)
public SystemConfig querySingleConfig(long configId) {
@Cacheable(value="platform.config", key = "#configKey)
public SystemConfig querySingleConfig(String configKey) {
通过@Caching注解组合多个@CachePut注解,从而在调用更新方法时同时更新多份缓存(以不同的key存在于缓存中)
@Caching(put={@CachePut(value="platform.config" , key="#config.configId"),
@CachePut(value="platform.config" , key="#config.configKey" )})
public SystemConfig updateConfig(SystemConfig config) {
在更新方法上同时更新和清除缓存:
上面的方法虽然可以同时更新多个缓存,但是必须保证这些缓存的值是完全一样的,比如上面两个方法缓存的都是SystemConfig对象,而对于下面的方法就无能为力了,因为下面的方法缓存的是SystemConfig对象的configValue属性值,而上面的方式只能把作为返回值的SystemConfig对象更新到缓存中。
@Cacheable(value="platform.config")
public String querySingleConfigValue(String configKey) {
对于这种情况,通过@CacheEvict清除那些无法更新的缓存,这样下次获取不到这些缓存就会越过缓存从数据库查询最新的数据。
@Caching(evict={@CacheEvict(value="platform.config", key="#config.configKey" )},
put={@CachePut(value="platform.config", key="#config.configId.concat('_object')"),
@CachePut(value="platform.config", key="#config.configKey.concat('_object')" )})
public SystemConfig updateConfig(SystemConfig config) {
使用缓存的业务层的设计宗旨:
使用缓存的业务类应尽量精简、方法应尽可能少,只保留最紧要的方法。
因为进行更新操作时需要考虑每一个的获取数据的方法,必须将可能被更新操作波及的所有缓存进行更新或清除,如果获取数据的方法过多,这个工作量将变得很大,并且很容易出错,比如忘记了更新或清除某一份缓存,而大型项目中这种错误可能难以被发现。原文链接:
阅读排行榜spring3.1&包,根据自己需要加入需要的包
Spring 3.1包详解
Spring 3.1与之前的 2.X 的不一样,新版本把原来的包分解了,功能分工明确:
org.springframework.aop-3.1.0.M1.jar ---- ----spring
的面向切面编程,提供AOP(面向切面编程)实现,
org.springframework.asm-3.1.0.M1.jar ---- ----spring 独立的asm
程序,相比2.5版本,需要额外的asm.jar包。
org.springframework.aspects-3.1.0.M1.jar ---- ----spring 提供对
AspectJ 框架的整合。
org.springframework.beans-3.1.0.M1.jar ----
----springIoC(依赖注入)的基础实现。
org.springframework.context.support-3.1.0.M1.jar ----
----spring-context 的扩展支持,用于 MVC 方面。
org.springframework.context-3.1.0.M1.jar ---- ----spring 提供在基础 IoC
功能上的扩展服务,此外还提供许多企业级服务的支持,如 邮件服务、任务调度、JNDI定位、EJB 集成、远程访问、
缓存以及各种视图层框架的封装等。
org.springframework.core-3.1.0.M1.jar ----
----spring3.1的核心工具包。
org.springframework.expression-3.1.0.M1.jar ---- ----spring
表达式语言。
org.springframework.instrument.tomcat-3.1.0.M1.jar ----
----spring3.1 对 Tomcat 的连接池的基成。
org.springframework.instrument-3.1.0.M1.jar ---- ----spring3.1
对服务器的代理接口。
org.springframework.jdbc-3.1.0.M1.jar ---- ----spring对
JDBC的简单封装。
org.springframework.jms-3.1.0.M1.jar ---- ----spring为简化 JMS API
使用而作的简单封装。
org.springframework.orm-3.1.0.M1.jar ---- ----spring 整合第三方的 ORM
映射支持,如 Hibernate 、Ibatis、Jdo 以及spring的JPA的支持。
org.springframework.oxm-3.1.0.M1.jar ---- ----spring 对Object/XMI
的映射的支持,可以让JAVA与XML之间来回切换。
org.springframework.test-3.1.0.M1.jar ---- ----spring 对Junit
等测试框架的简单封装。
org.springframework.transaction-3.1.0.M1.jar ----
----为JDBC、Hibernate、JDO、JPA 等提供的一致的声明式和编程式事务管理。
org.springframework.web.portlet-3.1.0.M1.jar ---- ----springMVC
org.springframework.web.servlet-3.1.0.M1.jar ---- ----对 J2EE6.0
Servlet3.0 的支持。
org.springframework.web.struts-3.1.0.M1.jar ---- ---- 整合 Struts
org.springframework.web-3.1.0.M1.jar ---- ----springWeb
下的工具包。
现在Spring已经更新到Spring3.X了,最新的是Spring3.1吧。
Spring3.X的JAR文件的组织方式与Spring2.X有了很大变化。
没有那个spring.jar了,而是把所有的JAR文件按模块区分开了。
好吧,现在介绍一下自己平时学习SSH整合时用到的JAR吧。
第一:Spring3(以Spring3.1为例)
----------------------------------------基本JAR----------------------------------------
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
com.springsource.mons.logging-1.1.1.jar(日志)
----------------------------------------加入AOP支持----------------------------------------
org.springframework.aop-3.1.0.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
com.springsource.net.sf.cglib-2.2.0.jar
(上次忘记加了)
----------------------------------------整合Hibernate3.6----------------------------------------
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.orm-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
.mchange.v2.c3p0-0.9.1.2.jar(C3P0数据源,根据具体情况而定)
下面的要么都加,要么都不加。
要加的话,还要引入log4j.properties文件
com.springsource.org.apache.log4j-1.2.15.jar(日志)
slf4j-log4j12-1.6.1.jar(日志)
----------------------------------------整合Struts2.2----------------------------------------
org.springframework.web-3.1.0.RELEASE.jar
好了,上面的就是SSH整合时,Spring3.x常用的JAR。
第二:Hibernate3(以Hibernate3.6为例)
hibernate-distribution-3.6.0.Final\ hibernate3.jar
hibernate-distribution-3.6.0.Final\lib\required\*
hibernate-distribution-3.6.0.Final\lib\jpa\*
第三:Struts2(以Struts2.2为例)
struts-2.2.1.1\lib\ commons-fileupload-1.2.1.jar
struts-2.2.1.1\lib\ commons-io-1.3.2.jar
struts-2.2.1.1\lib\ freemarker-2.3.16.jar
struts-2.2.1.1\lib\ javassist-3.7.ga.jar
struts-2.2.1.1\lib\ ognl-3.0.jar
struts-2.2.1.1\lib\ struts2-core-2.2.1.1.jar
struts-2.2.1.1\lib\ xwork-core-2.2.1.1.jar
struts-2.2.1.1\lib\
struts2-spring-plugin-2.2.1.1.jar&
(Struts2与Spring整合JAR)
注意:在有“额外”字样的JAR文件,需要额外下载。
在spring-framework-3.1.0.RELEASE或spring-framework-3.1.0.RELEASE-with-docs
是没有的,而在spring-framework-3.0.2.RELEASE-dependencies这里有那些额外的JAR。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。一.spring包分析
org.springframework.aop- 3.0.0.RELEASE: 面向切面编程,提供AOP实现。
org.springframework.asm- 3.0.0.RELEASE:独立的asm程序,Spring2.5.6的时候需要asmJar 包.3.0开始提供他自己独立的asmJar
org.springframework.aspects- 3.0.0.RELEASE:提供对AspectJ框架的整合
org.springframework.beans- 3.0.0.RELEASE:SpringIoc(依赖注入)的实现
org.springframework.context.support- 3.0.0.RELEASE:spring-context的扩展支持,用于MVC方面
org.springframework.context- 3.0.0.RELEASE:Spring提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等
org.springframework.core- 3.0.0.RELEASE:Spring的核心工具包
org.springframework.expression- 3.0.0.RELEASE:Spring表达式语言支持包
org.springframework.instrument.tomcat- 3.0.0.RELEASE:Spring对Tomcat连接池的集成
org.springframework.instrument- 3.0.0.RELEASE:Spring对服务器的代理接口
org.springframework.jdbc- 3.0.0.RELEASE:Spring对JDBC的简单封装
org.springframework.jms- 3.0.0.RELEASE:封装JMS API
org.springframework.orm- 3.0.0.RELEASE:整合第三方的ORM框架,如Hibernate,mybatis等
org.springframework.oxm-3.0.0.RELEASE:Spring对Object/XML的映射支持,可以让Java与xml之前来回切换。
org.springframework.test- 3.0.0.RELEASE:Spring对Junit測試框架的簡單封裝
org.springframework.transaction- 3.0.0.RELEASE:對JDBC,Hibernate,JDO,JPA等提供的一致的聲明式和編程式事務管理
org.springframework.web.portlet- 3.0.0.RELEASE:SpringMVC的增強支持包
org.springframework.web.servlet- 3.0.0.RELEASE:對Javaee Servlet的支持
org.springframework.web.struts- 3.0.0.RELEASE:提供對整合struts框架的支持
org.springframework.web- 3.0.0.RELEASE:SpringWeb下的工具包
mybatis-spring-1.2.1.jar:Spring整合mybatis時的支持包
二.Springmvc常用註解說明
@Controller
在類上面定義,表明該類為控制器,返回字符串與redirect:xxx
@RequestMapping(value="" method=XX)
在類或方法上面使用,表示設置URL訪問地址。它有兩個屬性,value指定訪問路徑,method指定請求方式,请求方式在RequestMethod这个类中,全部以常量的形式定义,它默认使用GET请求。
@RequestParam
指定Request请求参数,在方法参数中定义,相当于传统的request.getParameter().
@PathVariable
获取URL访问路径变量,这是Springmvc3.0框架才加入的特性,基于Restful风格的URL访问路径
@ModelAttribute
全局式的方法,在一组URL访问路径中,每次都会执行,方法返回结果保存在module会话中
在类上面定义,指定被注解的类是业务逻辑组件,如果不指定具体的Bean ID,则采用默认命名方式,即类名的首字母小写.
@Repository
在DAO层的类上使用
@Autowired
IOC自动注入功能,替换以前的set方法
@Qualifier
对同一接口类有不同实现指定具体的实现类
@ResponseBody
定义在方法上,Ajax调用声明,指定方法返回结果为Ajax回调函数结果。这是spring3.0新增的特性,用来标识ajax调用。
@InitBinder
初始化数据绑定与类型转换,将传入的参数转换为自定义类型,或者对参数进行自定义处理。
& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区君,已阅读到文档的结尾了呢~~
spring framework 中文
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
spring framework 中文
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口13918人阅读
Java EE(4)
Spring官网改版后找了好久都没有找到直接下载Jar包的链接,下面汇总些网上提供的方法,亲测可用.
1.直接输入地址,改相应版本即可:http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.4.RELEASE/spring-framework-3.2.4.RELEASE-dist.zip
2.在1的方法上输入前面部分,有个树形结构可供选择:http://repo.springsource.org/libs-release-local/org/springframework/spring/
3.同样的,,有树形结构选择需要的包下载:http://repo.spring.io/milestone/org/springframework/
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:44574次
排名:千里之外
原创:25篇
评论:23条
(1)(1)(2)(2)(5)(2)(1)(1)(18)

我要回帖

更多关于 spring 4.3.2 release 的文章

 

随机推荐