经验汇总

1. 日志配置

(1) 增删改必须打印日志。修改数据必须有据可查。
(2) 条件分支必须打印分支参数。
(3) 打印数据量,对比性能。
(4) 打印最后结果。
(5) 打印当前用户。
(6) 对于controller层可以使用拦截器来计算时间,对于service层则只能用aop。

2. spring事务管理。

(1) @Transactional必须配置在service层的方法上,dao层上不起作用。
(2) serivce类的扫描必须配置在spring父容器app.xml中,和transactionManager放在一起,放在spring子容器mvc.xml中将不起作用。
(3) 指定rollbackFor时默认包含RuntimeException。
(4) 当某个方法返回false时可以手动回滚,也可以抛出RuntimeExeception,手动回滚如下:

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

(5) 参考文章https://www.bbsmax.com/A/A7zgYqXk54/

3. spring ${name}起作用

(1) properties文件和被扫描的package位于mvc.xml中。

<!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
    <context:component-scan base-package="com.jessin.practice">
    </context:component-scan>
<!-- properties文件放在mvc里面,@Value("${name}")才能起作用-->
    <context:property-placeholder location="classpath*:/test.properties" />
    @Value("${name}")
    private String name;

(2) 使用${name}注入的类必须是spring管理的bean。

4. spring #{config[‘name’]}起作用

(1) util配置位置没有限制

<util:properties id="config" location="classpath:test.properties" />

@Value("#{config['name']}")
    private String name;

(2) 必须加入util的命名空间:

xmlns:util="http://www.springframework.org/schema/util
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd

(3) 必须注册为spring管理的bean。

5. spring bean的生命周期

调用MyBeanFactoryPostProcessor.postProcessBeanFactory()!
MyInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
调用Car()构造函数。
实例化后,先曝光到缓存中,以允许循环依赖。
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation
InstantiationAwareBeanPostProcessor.postProcessPropertyValues
按照类型或者按照名字,寻找所有依赖的setter属性,并创建。如调用setBrand()设置属性。brand : 奇瑞QQ
调用BeanNameAware.setBeanName()。
调用BeanFactoryAware.setBeanFactory()。
调用MyBeanPostProcessor.postProcessBeforeInitialization(),color为空,设置为默认黑色。
调用InitializingBean.afterPropertiesSet()。
调用myInit(),将maxSpeed设置为240。
调用MyBeanPostProcessor.postProcessAfterInitialization(),将maxSpeed调整为200。
introduce:brand:奇瑞QQ/color:黑色/maxSpeed:200
introduce:brand:奇瑞QQ/color:红色/maxSpeed:200
调用DisposableBean.destory()。
调用myDestroy()。

6. 官方文档

    
spring 4.1.1.官方文档
dubbo使用文档

7. 数据库悲观锁

    打开一个终端:

// 开启一个事务
begin;
select * from employee where id = 17 for update;
commit;

    打开第二个终端:

update employee set gender = 1 where id = 17;

    如果第一个终端不提交,则第二个终端不能提交。for update 会锁住查到的记录,如果第二个终端中更新的id不是17则不受影响。乐观锁的一种实现机制是使用version字段来维护,由应用程序自己维护。

8. annotation-driven的作用

使用注解<mvc:annotation-driven></mvc:annotation-driven>,会注入:
处理器映射器:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping, 
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
处理器适配器:
[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter, 
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,
 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter]
处理异常解析器,返回异常视图:
[org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver, // 支持controllerAdavice和自身的@ExcptionHandler。
 org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver, 
 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver]

9. 自定义标签解析器

{
    "http://www.springframework.org/schema/aop":"org.springframework.aop.config.AopNamespaceHandler",
    "http://www.springframework.org/schema/oxm":"org.springframework.oxm.config.OxmNamespaceHandler",
    "http://www.springframework.org/schema/task":"org.springframework.scheduling.config.TaskNamespaceHandler",
    "http://www.springframework.org/schema/lang":"org.springframework.scripting.config.LangNamespaceHandler",
    "http://www.springframework.org/schema/c":"org.springframework.beans.factory.xml.SimpleConstructorNamespaceHandler",
    "http://www.springframework.org/schema/jee":"org.springframework.ejb.config.JeeNamespaceHandler",
    "http://www.springframework.org/schema/cache":"org.springframework.cache.config.CacheNamespaceHandler",
    "http://www.springframework.org/schema/p":"org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler",
    "http://www.springframework.org/schema/util":"org.springframework.beans.factory.xml.UtilNamespaceHandler",
    "http://www.springframework.org/schema/context":"org.springframework.context.config.ContextNamespaceHandler",
    "http://www.springframework.org/schema/mvc":"org.springframework.web.servlet.config.MvcNamespaceHandler"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值