发表于: 2017-09-12 23:27:58
1 874
今天完成的事
对今日所学内容做一个总结
aop操作准备
1.导入jar包
2,创建spring核心配置文件,导入aop的约束
使用表达式配置切入点
*是访问修饰符
..是什么意思呢,如果里面有参数也包含
对类里面某个方法增强
execution(* 包名+类名+方法名(..))
对类里面所有方法增强
execution(* 包名+类名+.*(..))
对所有类所有方法增强
execution(* *.*(..))
对所有save开头的方法增强
execution(* save*(..))
举个例子,创建两个类Book和MyBook
package com.jnshu.aspectj;
public class Book {
public void add(){
System.out.println("add.....");
}
}
package com.jnshu.aspectj;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyBook {
public void before1(){
System.out.println("前置增强");
}
public void after1(){
System.out.println("后置增强");
}
// 环绕通知
public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("方法之前");
proceedingJoinPoint.proceed();
System.out.println("方法之后");
}
}
3.创建配置文件起名Bean.xml,配置对象
配置对象
<bean id="book" class="com.jnshu.aspectj.Book"></bean>
<bean id="myBook" class="com.jnshu.aspectj.MyBook"></bean>
配置aop操作
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pointcut1" expression="execution(* com.jnshu.aspectj.Book.*(..))"/>
<!--配置切面-->
<aop:aspect ref="myBook">
<aop:before method="before1" pointcut-ref="pointcut1"/>
<aop:after method="after1" pointcut-ref="pointcut1"/>
<aop:around method="around1" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
4.写个测试类
import com.jnshu.aspectj.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnno {
@Test
public void testService(){
ApplicationContext context =
new ClassPathXmlApplicationContext("Bean.xml");
Book book = (Book) context.getBean("book");
book.add();
}
}
明天计划的事
jdbcTemplate实现crud操作,spring配置c3p0连接池,dao使用jdbcTemplate
遇到的问题
无
收获
了解了log4j的作用,,通过log4j可以看到程序运行中更详细的过程
log4j使用方法
1.导入log4j的jar包
2.复制log4j的配置文件到根目录下(log4j.properties)
在配置文件中
//info:看到基本信息
log4j.rootLogger=info,stdout
//debug:看到更详细信息
log4j.rootLogger=debug,stdout
评论