SPRING
lazy-init =true 懒加载
init-method=“test” 只要一实例化就加载这个方法
单例作用域
<bean id='test' class='com.test' scope=singleton>
原型作用域 每次请求都会创建一个新的bean对象
<bean id='test' class='com.test' scope="prototype">
bean 的生命周期
bean的定义,初始化,使用,周期
阶段一: 在配置文件中定义bean的标签,设置对应的id与class属性值
阶段二: 在容器中,默认在初始化时实例化对象
在spring查看实例化的两种方式
1.在bean标签上,设置init-method属性,属性值对应bean对象的方法,当bean对象实例化时,方法会被调用
2. 实现 initializingBean接口,接口中的方法会在Bean对象实例化时调用
阶段三:
1 ApplicationContext ac=new ClassPathXmlApplication(”配置文件“);
ac.getBean(“id属性值”)
2 BeanFactory ac=new ClassPathXmlApplicationContext(“配置文件”);
ac.getBean(“id属性值”)
阶段四:
1 通过AbstractApplicationContext 对象,调用其close方法实现bean的销毁过程
2 通过bean标签设置destory-method 方法,ac.close
SpringTask
处理定时任务 1 jdk自带的Timer(比较简单,几分几秒不太行) 2 第三方组件Quartz(太大了,不需要太强的) 3使用springTask
第一步: 引入依赖
spring -context
第二步: 配置自动扫描 .xml
第三步:<task:scheduled ref=“taskJob” method=“job1” cron=“0/2 * * * * ?”/>
或者加个 @Scheduled(cron=“0/2 * * * * ?”)
动态代理
JDK动态代理
Proxy.newProexyInstance(ClassLoader ,Class<?>{} interfaces, InvocationHadnle)
CGLIB动态代理
jdk只能代理实现接口的类
AOP
joinpoint(连接点) pointcut(切入点) advice(通知) aspect(切面) target(切面)
底层使用JDK动态代理或者CGLIB代理
1 在spring.xml中把aop开启
2 写一个切面类(注释的方法)
@Component
@Aspect //声明当前类是一个切面
@Pointcut(“execution(*com.xxxx.service…*.*(…))”)
第一个* 表示public privite等所有方法 第二个表示任意类,第三个表示任意方法
两个点表示所有子包
@Before(value=“切入点的那个方法就是加pointcut的那个”)
@AfterReturning(value=“切入点的那个方法就是加pointcut的那个”)
@After(value=“切入点的那个方法就是加pointcut的那个”) 类似于finally
@AfterThrow(value="",throwing=“e”)
public void afterThrow(Exception e)
@Around(value="")
3 XML的方法
事务
(事物的四大特性)acid
可在spring配置事务
或者在spring.xml中配置个事务管理器方法上加上
@Transactional(propagation=Propagation.REQUIRED)
SpringMVC
web.xml中配置中央控制器和servlet-context的路径
servlet-context配置映射关系。
@RequestMapping(defaultValue=“18”,name=‘id’)
请求转发和重定向
设置请求域
@RequstBody 定义在参数前面,说明前端传进来的是json对象。
拦截器
实现接口HandlerInterceptor
继承类HandlerInterceptorAdaptor
全局异常处理
1 使用spring mvc 提供的简单异常处理器 SimpleMappingExceptionResolver
2 实现spring 的异常处理接口HadnleExceptionResolver自定义自己的异常处理器
3 使用@ExceptionHandler注解实现异常处理
自定义组合注解
@Retaention
@Target
public @interface MycompScan{}
分页
数据校验 valication
要加@Valid
捕获异常 BindException
数据缓存
EHcache
@Cacheable
@CachePut 与缓存数据同步
@CacheEvict删除缓存数据
QUARTZ
springboot自带的shedule 不支持分布式