每天记录一些一时想不起来的基础知识点
1.关于数据之间的转换:
Double 转为int :Double d=22.2d; -> d.intValue();
Spring:
2018-09-10:
Spring中初始化是在实例化之后的,InitializingBean,@PostConstruct 是按这种执行顺序的:构造函数->依赖注入->@PostConstruct->InitialingBean的afterPropertiesSet()->xml中指定的init方法
2018-09-11
关于aop的相关知识,当aop设置了返回值的的时候,会代替拦截的返回值,但是呢需要注意返回值是要一样的,如这样是不行的
@RequestMapping(value="/test")
public Integer test() throws SQLException
{
String sql="insert into user values (?, ? )";
// queryRunner.insert(sql, rsh, params)
Object a=queryRunner.insert(sql, new ScalarHandler<Object>(),new Object[] {new Random().nextInt(),"joker"});
System.out.println(a);
return 1;
}
而aop:
@Pointcut("execution(* com.test.controller..*.*(..))")
public void test1() {}
@Around("test1()")
public String test(ProceedingJoinPoint joinpoint)
{
System.out.println("before");
try
{
joinpoint.proceed();
} catch (Throwable e)
{
e.printStackTrace();
}
System.out.println("after");
return "aop";
}
这样会报castException的,string 无法转为Interger,所以aop的返回值要以被拦截的方法为准
关于aop中控制事务的方式,只需要在那个切点添加事务节点即可:
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- <tx:method name="query*" propagation="SUPPORTS"
read-only="true"></tx:method>
<tx:method name="get*" propagation="SUPPORTS"
read-only="true"></tx:method>
<tx:method name="select*" propagation="SUPPORTS"
read-only="true"></tx:method> -->
<tx:method name="*" propagation="REQUIRED"
rollback-for="Exception"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.test.controller..*.test(..))" id="test"/>
<aop:pointcut id="allManagerMethod"
expression="execution (* com.exmaple.fm..service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="test" order="0" />
</aop:config>
这个切点指明,对这个pointcut形成的切面,一旦抛出了异常就回滚(当然是需要在本地环境下)
2018-09-14
关于Comparator的使用,如果想升序排序的话,重写的那块关于返回值就需要小于等于的时候返回-1 :return o1.id<=o2.id?-1:1;