Spring笔记
笔记-传智播客springMVC公开课

SpringMVC公开课笔记高浩阳2014-11-29目录1SpringMVC框架 (2)1.1SpringMVC框架 (2)1.2SpringMVC组件总结 (2)2开发SpringMVC的第一个程序 (3)2.1准备环境 (3)2.2开发SpringMVC的第一个程序 (3)2.2.1创建Java Web工程 (3)2.2.2向工程中填充SpringMVC的jar包 (3)2.2.3配置前端控制器 (4)2.2.4创建一个配置文件springmvc.xml(名称不固定) (7)2.2.5配置处理器映射器HandlerMapping (8)2.2.6配置处理器适配器HandlerAdapter (9)2.2.7配置视图解析器ViewResolver (12)2.2.8编写Handler (13)2.2.9在springmvc.xml中配置helloAction.java (16)2.2.10将工程部署到tomcat,启动tomcat (17)2.3小结: (18)3注解开发第一个例子 (19)3.1新建工程SpringMVCTest02 (19)3.2在springmvc.xml中配置 (20)3.3开发action (21)3.4配置action (22)3.5部署工程,运行Tomcat (24)4注解开发学生信息管理功能 (24)5SpringMVC特点 (33)6和Jquery easyui 整合完成数据列表 (33)1SpringMVC框架1.1SpringMVC框架1. 用户发起请求request(比如请求链接叫http://www.xxx/user.action)注册用户信息。
2. SpringMVC通过DispatcherServlet接受请求。
DispatcherServlet是一个前端控制器(想到struts2在web.xml配置一个filter前端控制器)相当于控制器Controller3. DispatcherServlet调用HandlerMapping(处理器映射器)根据user.action找到处理器(Handler)HandlerMapping负责分局user.action这个链接找到Handler,根据xml配置或注解配置找到Handler4. HandlerMapping将找到的Handler给DispatcherServlet前端控制器5. DispatcherServlet前端控制器调用HandlerAdapter(处理器适配器)去执行Handler处理器适配器负责执行Handler6. Handler将处理结果返回给HandlerAdapter(处理器适配器)处理结果就是ModelAndView(封装了模型数据和视图)7. DispatcherServlet调用视图解析器ViewResolver去解析视图8. 将View给用户相应1.2SpringMVC组件总结1. DispatcherServlet前端控制器(不需要程序员写)负责框架调度,相当于中央处理器基本controller控制器功能:接收用户request请求和给用户response响应2. HandlerMapping(处理器映射器)(不需要程序员写)负责根据action的连接找到Handler处理器(理解成写的action)3. HandlerAdapter(处理器适配器)(不需要程序员写)负责去执行Handler4. **Handler处理器需要程序员写理解成struts里边的action,需要程序员写action类,这个action类符合适配器的执行规则。
Spring实战学习笔记之SpEL表达式

Spring实战学习笔记之SpEL表达式在Spring XML配置⽂件中装配Bean的属性和构造参数都是静态的,⽽在运⾏期才知道装配的值,就可以使⽤SpEL实现SpEL表达式的⾸要⽬标是通过计算获得某个值。
在计算这个数值的过程中,会使⽤到其他的值并会对这些值进⾏操作。
SpEL特性:(1)、使⽤Bean的ID来引⽤Bean;(2)、调⽤⽅法和访问对象的属性;(3)、对值进⾏算术、关系和逻辑运算;(4)、正则表达式匹配;(5)、集合操作#{ }标记会提⽰ Spring这个标记⾥的内容是SpEL表达式。
最简单的属性注⼊:<property name="count" value="#{5}" />还可以与⾮SpEL表达式的值混⽤:<property name="message" value="The value is #{5}" />浮点数value="#{89.7}" 科学记数法value="#{1e4}"=====>10000.0String类型的字⾯值:<property name="name" value="#{'Tom'}" />或<property name="name" value='#{"Tom"}' /> 单引号和双引号相互包含的⽅式使⽤布尔类型:<property name="enable" value="#{false}" />引⽤Bean:<property name="userDao" value="#{userDao}" />等价于<property name="userDao" ref="userDao" />引⽤Bean属性:<property name="orderOwner" value="#{}" />类似代码:User user=newUser();order.setOrderOwner(user.getName());引⽤Bean的⽅法:<property name="currUser" value="#{userDao.getCurrUser()}" />(假设userDao内有公共⽅法getCurrUser())现在如果想把userDao.getCurrUser()得到的当前英⽂⽤户名转为⼤写字母:<property name="currUser" value="# {userDao.getCurrUser().toUpperCase()}" />,这时如果得到的⽤户名为空(null),则会抛NullPointerException异常,为了避免异常,可以使⽤?.代替点(.),如:<property name="currUser" value="#{userDao.getCurrUser()?.toUpperCase()}" />这样null后⾯的⽅法不再执⾏SpEL表达式中使⽤T( )运算符访问指定类的静态⽅法和常量。
SpringBootActuator学习笔记

SpringBootActuator学习笔记spring boot ActuatorActuator概述Actuator指的是负责和移动装置的组件。
通过Actuator暴露的端点我们可以获取⼀个正在运⾏中的应⽤内部的状态导⼊依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>Actuator具备⼀些开箱即⽤的端⼝Endpoints id描述HTTP⽅法是否敏感信息auditevents显⽰当前应⽤程序的审计事件信息GET Yesbeans显⽰应⽤上下⽂中创建的所有Bean GET Yescaches显⽰可⽤缓存信息GET Yesconditions显⽰⾃动装配类的状态及及应⽤信息GET Yesconfigprops显⽰所有 @ConfigurationProperties 列表GET Yesenv显⽰ ConfigurableEnvironment 中的属性GET Yesflyway显⽰ Flyway 数据库迁移信息GET Yeshealth显⽰应⽤的健康信息(未认证只显⽰status,认证显⽰全部信息详情)GET Noinfo显⽰任意的应⽤信息(在资源⽂件写info.xxx即可)GET Noliquibase展⽰Liquibase 数据库迁移GET Yesmetrics提供应⽤运⾏状态的完整度量指标报告GET Yesmappings显⽰所有 @RequestMapping 路径集列表GET Yes scheduledtasks显⽰应⽤程序中的计划任务GET Yessessions允许从Spring会话⽀持的会话存储中检索和删除⽤户会话。
SpringBoot学习笔记

10分钟创建一个SB应用:1.创建项目2将springboot的版本改为1.5.6(不修改后面操作数据库会报类找不到)<version>1.5.6.RELEASE</version>3.pom.xml中配置3个数据库相关的内容4.在入口文件增加注解(不注解会导致mapper识别不到):@MapperScan("com.example.demo.mapper")5.创建generator/generatorConfig.xml文件,并修改数据库账号密码、包名、表名6.修改application.yml,增加数据源相关的配置7.创建一个maven的Run配置,设置mybatis-generator:generate -e8.编写Service接口和Service实现类9.编写Controller和方法10.启动应用创建项目https:///lom9357bye/article/details/69677120通过tomcat部署项目https:///PJH-Forever/p/8026330.htmlspring boot configuration annotation proessor not found in classpath引入如下依赖:<dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-configuration-processor </artifactId><optional> true </optional></dependency>pom.xml中的parent只有parent设置了,后面的才不用写version;没有在parent中设置的,后面必须写version,否则只会下载一个unknown的错误包一些tips:spring boot 2.0相比1.5.x,变更比较大,一些类去除了,因此要注意不同版本的api如果在generatorConfig.xml中配置了某个表,但是没有用到,那么程序起来的时候会报错;删除即可Durid包含的主要功能:1)使用StaFilter插件进行监控数据库访问性能2)替换DBCP和C3P0,提供一个高效、可扩展的数据库连接池3)支持数据库密码的加密4)SQL执行日志5)提供了一个监控页面,可以查看sql情况控制器中通过@RequestBody获取POST参数报错:{"timestamp":1527154727083,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message": "Content type 'application/json;charset=UTF-8' not supported","path":"/recomd/add"}经过排查,发现是参数中的类Recommend里面,有2个setIndexList()方法:setIndexList(List xxx);setIndexList(ArrayList xxx);删除一个就正常了,猜测是RequestBody在组装Recommend这个参数类的时候,无法判断该用哪一个set方法,从而报错了这应该算是框架的一个bug了Field userMapper in erServiceImpl required a bean of type 'erMapper' that could not be found.原因是在入口文件XxxApplication中,少加了一个注解:@MapperScan("com.zhouchangju.mapper")将项目启动方式从XxxApplication改为Artifact后,访问页面都是4041.没有将pom.xml里面的启动方式从jar改为war2.XxxApplication类没有继承自SpringBootServletInitializer类extends SpringBootServletInitializer安全插件配置url的时候,不用加artifactid比如项目叫做daily,我设置antMatchers的时候,不用加daily这一截:/**定义安全策略*/@Overrideprotected void configure(HttpSecurity http) throws Exception {//配置安全策略http.authorizeRequests()//定义/请求不需要验证.antMatchers("/","/**/*.css","/**/*.js","/**/*.jpg","/**/*.png","/**/*.jpeg","/user/getdynamicpassword","/user/login","/login.html").permitAll().anyRequest().authenticated()//其余的所有请求都需要验证.and().logout().permitAll()//定义logout不需要验证.and().formLogin()//使用form表单登录.loginPage("/login.html").failureUrl("/login?error=true");}本地开发时,Security验证的接口,总是报403需要在SecurityConfig中关闭csrf:.csrf().disable()Edit Configration时,没有Tomcat Serversettings->Build,Execution,Deploment->右侧ApplicationServer->添加本地tomcat信息误删了项目模块文件xxx.iml怎么办?可以在Project Structure里面的module里面,重新导入模块,注意每一步都是import,不是createKafkaConsumer is not safe for multi-threaded access配置文件中的属性无法加载: require a bean of type ng.String that could not be found 是因为写了一个带有String参数的构造函数导致的,删除构造函数即可解决,详见:https:///questions/40670099/im-not-including-the-java-lang-string-bean maven编译项目,报错:cannot find symbol原因是svn上面代码不全,缺少了一些文件如何修改打包后的war文件的存放位置?由于maven遵循“约定优先于配置”的思想,所以如果不做特殊配置,它默认是把打包之后产生的文件都放在target目录下的。
Spring日常笔记记录10--动态代理实现InvocationHandler

Spring⽇常笔记记录10--动态代理实现InvocationHandler动态代理动态代理是指,程序在整个运⾏构成中根本就不存在⽬标类的代理类,⽬标对象的代理对象只是由代理⽣成⼯具(不是真实定义的类)在程序运⾏时由JVM根据反射等机制动态⽣成的。
代理对象与⽬标对象的代理关系在程序运⾏时才确⽴。
⼀、JDK动态代理动态代理的实现⽅式常⽤的有两种:使⽤JDK的Proxy,与通过CGLIB⽣成代理。
jdk的动态要求⽬标对象必须实现接⼝,这是Java设计上的要求。
从jdk1.3以来,Java语⾔通过ng.reflect包提供三个⽀持代理模式Proxy,Method和InvocationHandler。
⼆、CGLIB动态代理第三⽅的⼯具库,创建代理对象,原理是继承。
通过继承⽬标类,创建⼦类。
⼦类就是代理对象,要求⽬标类不能是final的,⽅法也不能是final的三、JDK动态代理底层实现反射包 ng.reflect , ⾥⾯有三个类: InvocationHandler , Method, Proxy.1)InvocationHandler 接⼝(调⽤处理器):就⼀个⽅法invoke()invoke():表⽰代理对象要执⾏的功能代码。
你的代理类要完成的功能就写在invoke()⽅法中。
代理类完成的功能:1. 调⽤⽬标⽅法,执⾏⽬标⽅法的功能2. 功能增强,在⽬标⽅法调⽤时,增加功能。
2)⽅法原型:参数: Object proxy:jdk创建的代理对象,⽆需赋值。
Method method:⽬标类中的⽅法,jdk提供method对象的Object[] args:⽬标类中⽅法的参数, jdk提供的。
public Object invoke(Object proxy, Method method, Object[] args)1) InvocationHandler 接⼝:表⽰你的代理要⼲什么怎么⽤: 1.创建类实现接⼝InvocationHandler2.重写invoke()⽅法,把原来静态代理中代理类要完成的功能,写在这。
spring_security3.2 详细配置 培训笔记

Spring Security 3.x 出来一段时间了,跟Acegi是大不同了,与2.x的版本也有一些小小的区别,网上有一些文档,也有人翻译Spring Security 3.x的guide,但通过阅读guide,无法马上就能很容易的实现一个完整的实例。
我花了点儿时间,根据以前的实战经验,整理了一份完整的入门教程,供需要的朋友们参考。
1,建一个web project,并导入所有需要的lib,这步就不多讲了。
2,配置web.xml,使用Spring的机制装载:<?xml version="1.0" e ncoding="UTF-8"?><web-app version="2.4" xmlns="/xml/ns/j2ee"xml ns:xsi="/2001/XMLS chema-i nstance"xsi:s chemaLocation="http://java.s /xml/ns/j2eehttp://java.s /xml/ns/j2ee/web-app_2_4.xsd"><conte xt-param><param-name>contextConfi gLocation</param-name><param-val ue>classpath:applicationConte xt*.xml</param-value></conte xt-param><listener><listener-class>org.springframework.we b.conte xt.Context LoaderListener</listener-class></listener><filter><filter-name>springSecurityFilterChain</filter-name><filter-class>org.springframework.we b.filter.DelegatingFilterProxy</filter-class></filter><filter-mappi ng><filter-name>springSecurityFilterChain</filter-name><url-pattern>/*</url-pattern></filter-mapping><w elcome-file-list><wel come-file>l ogin.jsp</wel come-file></welcome-file-list></we b-app>这个文件中的内容我相信大家都很熟悉了,不再多说了。
SpringBoot踩坑笔记二:SpringBoot整合redis报Noqualifyin。。。
SpringBoot踩坑笔记⼆:SpringBoot整合redis报Noqualifyin。
今天在学习Spring Boot整合redis的过程中遇到个问题,在使⽤@Autowiredprivate RedisTemplate<String, Object> redisTemplate;注⼊时,启动项⽬会报异常org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.bh.redis.RedisTest': Unsatisfied dependency expressed through field 'redisTemplate'; nested exception is org.springframework.beans.facto at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:393) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) ~[spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) ~[spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44) ~[spring-boot-test-autoconfigure-2.0.5.RELEASE.jar:2.0. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) ~[spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) [.cp/:na]at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) [.cp/:na]at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) [.cp/:na]at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) [.cp/:na]at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) [.cp/:na]at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209) [.cp/:na]Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate<ng.String, ng.Object>' available: expected at least 1 bean which qualifi at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]... 29 common frames omitted这么⼀⼤串看的是不是有点懵,其实咱们看⼀句可以了No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate<ng.String, ng.Object>' available:⼤致意思是没有与RedisTemplate<String, Object>匹配的bean解决⽅法1、不指定泛型,如@Autowiredprivate RedisTemplate redisTemplate;2、使⽤@Resource注解代替@Autowired,如@Resourceprivate RedisTemplate<String, Object> redisTemplate;。
Springcloud学习笔记09-常用注解01@PostMapping、@GetMapp。。。
Springcloud学习笔记09-常⽤注解01@PostMapping、@GetMapp。
.springframework.web.bind.annotation包下注解1.1 @PostMapping、@GetMapping、@RequestMapping、@RestController、@ResponseBody、@RequestParam、@RequestPart、@PutMapping(1)@RequestMapping@RequestMapping如果没有指定请求⽅式,将接收Get、Post、Head、Options等所有的请求⽅式.(2)@GetMapping@GetMapping是⼀个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
该注解将HTTP Get 映射到特定的处理⽅法上。
get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段⼀⼀对应,在URL中可以看到。
get是从服务器上获取数据。
若符合下列任⼀情况,则⽤GET⽅法:* 请求是为了查找资源,HTML表单数据仅⽤来帮助搜索。
* 请求结果⽆持续性的副作⽤。
* 收集的数据及HTML表单内的输⼊字段名称的总长不超过1024个字符。
(3)@PostMapping@PostMapping是⼀个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。
get⽅式的安全性较Post⽅式要差些,包含机密信息的话,建议⽤Post数据提交⽅式;post是向服务器传送数据。
若符合下列任⼀情况,则⽤POST⽅法:* 请求的结果有持续性的副作⽤,例如,数据库内添加新的数据⾏。
* 若使⽤GET⽅法,则表单上收集的数据可能让URL过长。
* 要传送的数据不是采⽤7位的ASCII编码。
(4)@requestBody(后端⽅法接收请求体)@requestBody注解常⽤来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,⽐如说:application/json或者是application/xml等。
大学英语课堂笔记
第一单元课时一重点单词1.She clutched her bag in her arms.She clutched at the flying kite.At 表方向企图drowning man 溺死的人2.throw sb into throw sb out of3.make + 培养目标+ out of 把...培养成,变成The army made a man (out) of him.4.There is no good to be had in doing something=It is no good doing somethingSpill milk 洒了的牛奶tear sb away from 使某人勉强离开e into view 看见As soon as 一.....就...Square 广场6.out of curiosity 出于好奇historical curiosity 珍奇品7.burst into 突然.... applause 掌声8.sort 分类,安排sort into 分成Pile 摞sort his book into two pile9. plot 情节Intricate plot 复杂情节10.overlook 俯视11.observant adj 遵守规则的be observant ofnothing but只有,只不过;无非At present I can do nothing but wait...12.resort to 诉诸于,求助于13.exertion 尽力the exertions of a busy day14.turn toward 转向单词补充Of 说服的1.convince about 说服that 完全相信到2.vast 广阔的hesitate 犹豫的cling clung 抓住physical punishment 体罚3.rank 队列form 组织paradise 天堂pattern 模式balcony 阳台roof 屋顶yell 大喊unclouded 晴朗的fooling around 游手好闲的rivalries 对抗take advantage of 利用reference to 提及,关于4.line with 沿...排列in contest of 在比赛的注:contestvt. 竞争,为…而奋争;辩驳vi. 争斗;竞争;争夺n. 比赛;竞赛;搏斗课时二短语1.in vain 徒然,无效e to a halt 停止=stop3.invade 涌入,入侵4.find one’s way to = arrivalRivers find their way to sea.5.take over 接管6.show over 炫耀7.sb in a daze 茫然8.let up 停止,减弱,放松When would the rain let up .9.stretch out 伸出stretch a hand 伸出手He stretched himself out in the sofa and fell sleep.10.make的短语make sure 确信,证实make good use of 充分利用make full use ofmake up 弥补,组成,化妆,整理make friends with 与...交友make up for 补偿,弥补make a differences 有影响make out 理解,辨认出,说明,填写,设法应付make a decision 做决定make for 导致,有助于,走向make of 了解,用...制造make in 加入,进入,干涉别人课外补充课文补充1.huge 巨大的庞大的,极大的(体积)2.vast 广阔的,辽阔的(平面)3. 事,事情affair (公共,商业)事物,业务,事态事件,事变,发生的事情4. n 材料,物质matter vi 有重要性的,有重大关系的,要紧化脓,出脓It’s just a matter of time. 这仅仅是个时间问题。
spring3.0学习笔记二---SpEL表达式1
spring3.0学习笔记二---SpEL表达式1相对来讲,java是一门静态语言。
而我们今天要讲的是一门动态“语言”---SpEL。
动态语言和静态语言的最显著差别在于,举个例子," 'Hello'.toUperCase()"这只是一个普通的字符串,差别在于能否把它编译和运行起来得到结果。
就是说动态语言能把一个字符串解释成程序语句。
如果还不是很明白的话没关系,看下面的SpEL例子。
(接下去的我就用例子来做介绍了)public void testSpEL1() {//ExpressionParser是Spring3里的一个包,用来动态解释一个字符串。
ExpressionParser parser = new SpelExpressionParser();Expression exp = parser.parseExpression(" 'Hello,World' ");System.out.println((String)exp.getV alue());}这里'Hello,World' 是一个字符串,解释起来还是一个字符串,所以打印结果是:Hello,World第二个例子:调用方法public void testSpEL2() {ExpressionParser parser = new SpelExpressionParser();Expression exp=parser.parseExpression(" 'Hello'.concat(' World!')"); //这里调用了字符串String的concat 方法//Expression exp=parser.parseExpression("newString('helloworld').toUpperCase()");Stringmessage=(String)exp.getV alue();}第三个例子:调用属性ExpressionParser parser= new SpelExpressionParser();Expression exp=parser.parseExpression("'HelloWorld'.bytes"); //得到字符串的byte//Expression exp=parser.parseExpression("'HelloWorld'.bytes.length"); //得到属性的属性byte [] bytes=(byte[])exp.getV alue();第四个例子:调用类中的属性(下面开始复杂起来了)@Resourcepublic User user; //注入之后,user.getName() 为xiaolingpublic void testSpEL() {//设“值域”,限定在u这个对象里EvaluationContext context = new StandardEvaluationContext(user);ExpressionParser parser = new SpelExpressionParser();Expression exp = parser.parseExpression("username"); //==user.getUsername() System.out.println((String)exp.getV alue(context)); //结果:xiaoling}或者用更简洁的一个方式:System.out.println((String)exp.getV alue(user));这样就不用设值域了!呵呵!第五个例子:给对象中的属性设值StandardEvaluationContext context = new StandardEvaluationContext(user); parser.parseExpression("username").setV alue(context, "ling");第六个例子:做判断Expression exp=parser.parseExpression("name=='xiaoling' ");boolean result=exp.getV alue(context,Boolean.class); //evaluate stotrue打印出来,结果为true看到这,可能你还是一头雾水:这算什么新特性啊,哪有什么用处!用处就在于,SpEL把java变成“动”的了!(纯属我个人观点!!)接下来,我们把SpEL用到bean.xml中去先看个例子(例一)<bean id="numberGuess" class="cn.ling.spel.NumberGuess"><property name="randomNumber" value="#{T(ng.Math).random()*100.0}"/></bean>怎么样,可以在xml文件里面赋值了!呵呵!value里面都用#{}来赋值。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
chapter01 Spring简介一、Spring是什么?在了解Spring之前,我们来了解在Java EE框架下企业级开发采用EJB框架的一些不足:(1) EJB太笨重,而且Entity EJB不能脱离容器(2) 企业级服务使用困难(3) 开发的复杂度太高(4) 侵入式方案,EJB要使用特定的接口因此,Spring应运而生。
Spring是一个开源的用于简化采用Java语言开发企业级程序的一个分层的框架。
关于程序的分层结构:1、Presentation layer(表示层)(1) 表示逻辑(生成界面代码)(2) 接收请求(3) 处理业务层抛出的异常(4) 负责规则验证(数据格式,数据非空等)(5) 流程控制2、Service layer(服务层/业务层)(1) 封装业务逻辑处理,并且对外暴露接口(2) 负责事务,安全等服务3、Persistence layer(持久层)(1) 封装数据访问的逻辑,暴露接口(2) 提供方便的数据访问的方案(查询语言,API,映射机制等)Domain layer(域层)(1) 业务对象以及业务关系的表示(2) 处理简单的业务逻辑(3) 域层的对象可以穿越表示层,业务层,持久层二、Spring的作用为什么要使用Spring?(1) 简化企业级开发①封装了大部分的企业级服务,提供了更好的访问这些服务的方式②提供了IOC,AOP功能的容器,方便编程(2) 遵循Spring框架的应用程序,一定是设计良好的,针对接口编程,这样就简化了企业级程序的设计。
(3) Spring的组成① Spring Core:核心容器,BeanFactory提供了组件生命周期的管理,组件的创建,装配,销毁等功能SpringContext:ApplicationContext,扩展核心容器,提供事件处理、国际化等功能。
它提供了一些企业级服务的功能,提供了JNDI,EJB,RMI的支持。
② Spring AOP:提供切面支持③ Spring DAO:提供事务支持,JDBC,DAO支持④ Spring ORM:对流行的O/R Mapping封装或支持⑤ Spring Web:提供Web应用上下文,对Web开发提供功能上的支持,如请求,表单,异⑥ Spring Web MVC:全功能MVC框架,作用等同于Struts。
chapter02 Spring的IoC一、IoC的概念IoC,Inversion of Control,控制反转。
对象的协作关系由对象自己负责。
依赖注入:对象的协作关系有容器来建立。
二、IoC的类型(1) 基于特定接口(侵入式方案)(2) 基于set方法(3) 基于构造器三、Spring容器Spring容器负责生成、组装、销毁组件,并负责事件处理、国际化等功能。
(1) BeanFactory<interface>①核心容器,负责组件生成和装配②实现主要包括Xml BeanFactory(2) ApplicationContext(3) WebApplicationContext(4) ……四、IoC的使用Resource:interface,用来包装资源xmlBeanFactory:BeanFactory的一个实现,使用Resource对象来查找配置文件BeanFactory.gerBean(“BeanId”):取得以参数命名,或者Id等于参数值的一个Bean 实例。
BeanFactory(容器)在默认情况下,会采用单例方式返回对象。
容器只到调用getBean方法时,才会实例化某个对象。
(1) Spring可以采用XML或者.properties文件作配置(2) 配置文件(XML)根元素<beans>可以有多个<bean>子元素,每个<bean>代表一个需要装配的对象。
1、setter注入(1) 注入简单属性(String和8中基本类型)<beans><bean id=”BeanId” class=”classpath” autowire=”” dependency-check=””><property name=”parameterName”><value>parameterValue</value></property></bean></beans>对于基本类型,Spring容器会自动作类型转换,以便赋值。
(2) 注入对象<bean><ref local=”BeanId”>A、让Spring容器在当前配置文件中找到相应的Bean,并调用set方法,注入该Bean。
B、将一个Bean的定义嵌套在另一个Bean中(可读性差),被嵌套的Bean不能采用getBean()返回C、采用<ref bean=””>搜索多个配置文件来注入(3) 注入集合类型① SetSet中存放字符串,对象,集合,不允许重复② ListList中可以放入字符串,对象,List③ MapMap 有<entry>子元素来存取key,value,key只能为String④ PropertiesProperties有<props>子元素2、consctructor注入<bean><constructor-arg><value></value></constructor-arg><constructor-arg><ref bean= “” /></constructor-arg></bean>如果Bean属性不多,并且属性值必须要注入才能使用,则应该采用constructor注入,其他情况就要set方法注入。
装配关系检查(Dependency checking)simple:检查简单类型ojects:检查对象类型all:检查所有<bean dependency-check=“all”></bean>自动装配(Autowring Properties)装配方式:byName,byType,autodetect,constructorautowire=“byName”:按照名称匹配按照Bean的Id与属性的名称进行匹配自动装配与手动装配可以结合使用,手动装配会覆盖自动装配。
autowire=“byType”:按照类型匹配要注入的属性类型与配置文件中的Bean类型匹配的个数超过一个,会出错。
autowire=“antodetect”:先按照construct,后按照byType。
autowire=“constructor”:先去匹配构造器中参数类型,后与配置文件中的Bean类型匹配。
3、比较两种注入方式关于自动匹配:优点:快速开发缺点:依赖关系不清楚,易出错,不易维护。
自动匹配的应用场合:(1) 构建系统原型(2) 与依赖关系检查(Dependency-check)结合使用(3) 自动装配与手动装配结合4、特殊的IoC①后处理Bean接口:org.springframework.beans.factory.config.BeanPostProcessorSpring已经实现该接口的BeanPostProcessor(不用再注册)ApplicationContextAwareProcessor:把应用上下文传递给所用实现了 ApplicationContextAware接口的BeanApplicationContextAware接口使用举例,可参照事件监听机制DefaultAdvisorAutoProxyCreator自动对Bean应用切面② Bean工厂后处理(只能在应用上下文中使用)接口:org.springframework.beans.factory.config.BeanFactoryPostProcessorSpring内部接口实现:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer属性编辑org.springframework.beans.factory.config.CustomEditorConfigurerCustomerInfo("fname-lname-address")实例工厂配置代码:--------------------------------------------------------------------------------<beans><bean id="car" class="ioc3.Car" factory-bean="carFactory" factory-method="getCar" dependency-check="all"><property name="name"><value>奥迪A8</value></property><property name="price"><value>800000.0</value></property></bean><bean id="carFactory" class="ioc3.CarFactory"/></beans>--------------------------------------------------------------------------------静态工厂配置--------------------------------------------------------------------------------<beans><bean id="car" class="ioc3.CarFactory" factory-method="getCar"><property name="name"><value>奥迪A8</value></property><property name="price"><value>800000.0</value></property></bean></beans>--------------------------------------------------------------------------------5、事件处理事件监听1)自定义事件,通过继承org.springframework.context.ApplicationEvent2)自定义监听器,实现 org.springframework.context.ApplicationListener,并注册3)发布事件,为得到应用上下文,必须实现org.springframework.context.ApplicationContextAware接口chapter03 Spring的AOP一、AOP(Aspect-oriented programming,面向切面编程):什么是AOP?定义:将程序中的交叉业务逻辑提取出来,称之为切面。