spring2.5 学习笔记 第26页 共46页
@After(\ public void doReleaseLock() { // ... }} 5、 @ Around
在方法之前和之后都要加上
但是需要一个参数ProceedingJoinPoint,并者需要Object retVal = pjp.proceed(); 和返回return retVal; @Aspect public class AroundExample { @Around(\ public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { // start stopwatch Object retVal = pjp.proceed(); // stop stopwatch return retVal; }}
(七) Pointcut
当多个Advice个有相同的织入点。那么我们可以定义一个织入点集合,在需要使用的地方,调用就可以了。 例如:
@Aspect @Component public class LogInterceptor {
(八) annotatin方式的AOP实例 @Pointcut(\) public void myMethod(){}; @Before(value=\) public void before(){ } @AfterReturning(\) public void afterReturning(){ }} System.out.println(\); System.out.println(\); 注意:那个空方法,只是为了给Pointcut起个名字,以方便别处使用
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component spring2.5 学习笔记 第27页 共46页
public class LogInterceptor { }
@Pointcut(\) public void myMethod(){}; @Before(value=\) public void before(){ } @AfterReturning(\) public void afterReturning(){ } @Around(value=\) public void around(ProceedingJoinPoint pjp) throws Throwable{ } //因为@around需要传入一个参数ProceedingJoinPoint进行前后加逻辑 System.out.println(\); //在需要前后逻辑的中间加入下列语句。表示前后逻辑,可能会抛出异常Throwable。 pjp.proceed(); System.out.println(\); System.out.println(\); System.out.println(\); 二、 AOP配置xml方式
xml方式是我们以后使用的比较多的,因为当切面类我们没有源代码时、当我们使用第三方的切面类时,我就不能使用annotation的方式,而且如果使用annotation方式一但程序编译后就不可以修改了。如果使用xml方式就不一样了,我们只需要修改xml文件就可以了。
xml方式与annotation的作用是一样。现在就是实例:
三、 AOP实现动态代理注意
因为Spring要实现AOP(面向切面编程),需要加入切面逻辑的类就会生成动态代理。在动态代理类中加入切面类从而实现面向切面编程,但生成动态代理存在以下注意事项:
1、 被动态代理的类如果实现了某一个接口,那么Spring就会利用JDK类库生成动态代理。
2、 如果被动态代理的类没有实现某一个接口,那么Spring就会利用CGLIB类库直接修改二进制码来生成动态代理(因为利
用JDK生成动态代理的类必须实现一个接口),需要在项目中引用CGLIB类库
第九课:DataSource
DataSource是一个接口javax.sql.DataSource DataSource是一个标准,其它只需要实现它的接口,然后随意怎样实现。
一、 Sping配置数据源:
jdbc.properties文件
然后在其它的bean中自动注入(Spring需要使用的功能),就可以使用了。 @Component(\) public class UserDaoImpl implements UserDao { private DataSource dataSource; public DataSource getDataSource() {return dataSource;} @Resource(name=\) public void setDataSource(DataSource dataSource) { this.dataSource = dataSource;} @Override public void save(User u) { }} try { Connection conn = dataSource.getConnection(); conn.createStatement().executeUpdate(\); conn.close(); } catch (SQLException e) { e.printStackTrace();}System.out.println(\); spring2.5 学习笔记 第30页 共46页
三、 dbcp.BasicDataSource
1、 #
initialSize=10
2、 #
maxIdle=20
3、 #
minIdle=5
4、 #最大连接数量
maxActive=50
5、 #是否在自动回收超时连接的时候打印连接的超时错误
logAbandoned=true
6、 #是否自动回收超时连接
removeAbandoned=true
7、 #超时时间(以秒数为单位)
removeAbandonedTimeout=180
8、 #
maxWait=1000
第十课 Spring整合Hiberante3
Spring整合hibernate3重点就是需要初始化SessionFactory这个bean,需要在Spring的配置文件中进行配置,实现实例如下:
一、 Spring配置hibernate3的SessionFactory
实现上xml方式的配置文件与annotation注解方式的区别,只是在配置时所使用的bean不一样,而且配置实体类所使用的属性也不一样(xml:mappingResources;annotation:annotatedClasses),如下: (一) xml形式的SessionFactory