Spring框架的详细介绍( 五 )


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>

  1. 编写配置目标类
<bean id="orderDao" class="demo1.OrderDao"></bean>
public class OrderDao {
public void save(){
System.out.println("save order");
}
public void update(){
System.out.println("update order");
}
public void delete(){
System.out.println("delete order");
}
public void find(){
System.out.println("find order");
}
}
  1. 开启aop注解自动代理
<aop:aspectj-autoproxy/>
  1. 编写切面类并配置
@Aspect
public class MyAspectAnno {
@Before(value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"execution(* demo1.OrderDao.save(..))")
public void before(){
System.out.println("before");
}
}
<bean id="myAspect" class="demo1.MyAspectAnno">
注解通知类型
  • @Before: 前置通知
  • @AfterReturning: 后置通知
@AfterReturning(value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"execution(* demo1.OrderDao.save(..))",returning="result")
public void after(Object result){
System.out.println("after "+result);
}
  • @Around:环绕通知
@Around(value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"execution(* demo1.OrderDao.save(..))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("before");
Object obj=joinPoint.proceed();
System.out.println("after");
return obj;
}
  • @AfterThrowing: 抛出异常
@AfterThrowing(value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"execution(* demo1.OrderDao.save(..))",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("exception:"+e.getMessage();
}
  • @After: 最终通知
@After(value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"execution(* demo1.OrderDao.save(..))")
public void after(){
System.out.println("finally");
}
  • @PointCut:切入点注解
@PointCut(value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"execution(* demo1.OrderDao.save(..))")
private void pointcut1(){}
此时,在上述通知的注解中,value可以替换为该函数名,例如:
@After(value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"MyAspect.pointcut1()")
public void after(){
System.out.println("finally");
}
这个注解的好处是,只需要维护切入点即可,不用在修改时修改每个注解 。
 
Spring 的 JDBC 模板Spring 对持久层也提供了解决方案,也就是 ORM 模块和 JDBC 的模板 。针对 JDBC ,提供了 org.springframework.jdbc.core.JdbcTemplate 作为模板类 。
 
使用 JDBC 模板
  1. 引入jar包,数据库驱动,Spring 的 jdbc 相关包 。
  2. 基本使用:
public void demo1(){
//创建连接池
DriverManagerDataSource dataSource=new DriverManagerDataSource();
dataSource.setDriverClassName("com.MySQL.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring4");
dataSource.setUsername("root");
dataSource.setPassword("123456");
//创建JDBC模板
JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
jdbcTemplate.update("insert into account values (null,?,?)", "xiaoming",1000d);
}
  1. 将连接池和模板交给 Spring 管理
  • 配置文件:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource;">
<property name="driverClassName" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"com.mysql.jdbc.Driver">
<property name="url" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"jdbc:mysql:///spring4">


推荐阅读