返回通知,异常通知
1.required包中所有jar包
2.xml中配置自动扫描包
3.加注解
4.做日志类@Aspect@Before@After
5.加配置
5.切面类里边可以加链接点JoinPoint
@AfterReturning 返回通知,在方法正常结束后执行的代码,返回通知可以访问到方法的返回值!returning=“result”
@AfterThrowing 异常通知,在方法出现异常时会执行的代码,可以访问到异常对象,且可以指定在出现特定异常时在执行通知代码 throwing=“ex”
@Around环绕通知 相当于动态代理,需要携带ProceedingJoinPoint类型的参数,这个类型的参数可以决定是否执行目标方法且环绕通知必须有返回值,返回值即为目标方法的返回值
切面优先级:@Order(1) 值越小,优先级越高
重用切点表达式:
@Point声明切入点表达式
//定义一个方法,用于声明切入点表达式,一般的该方法中在不需要添加其他的代码
@Pointcut("execution(全类名.*(..))")//类中所有的方法
public void declareJointPointExpression(){}
//其他方法引用时
@After("declareJointPointExpression()")//不同包下要写包名
基于配置文件的方式来配置AOP:
1.配置bean
2.配置切面的bean
3.配置AOP
<aop:config>
<!--配置切点表达式-->
<aop:pointcut expression="execution()" id="pointcut">
<!--配置切面及通知-->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
Spring对JDBC 的支持
1.jar包:c3p0;数据库驱动;required中所有jar包
2.建数据库表:department表和employee表
3.src–applicationContext.xml
4.file–db.properties(建数据库源)
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring4
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
6.xml中
<!--导入资源文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置c3p0数据源-->
<bean id="dataSouce" class="com----combopooledDataSource">
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="jdbcUrl" value="${jdbc.jdbcurl}"/>
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="initPoolSize" value="${jdbc.initPoolSize}"/>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
</bean>
7.测试类JDBCtest
public class JDBCTest<ApplicationContext> {
private ApplicationContext ctx=null;
{
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void testDataSource() throws SQLException{
DataSource datasource=ctx.getbean(DataSource.class);
System.out.println(dataSource.getConnection());
}
}
8.配置Spring 的JDBCTemplate
<bean id="jdbcTemplate" class="com----JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
9.获取JDBCTemplate的bean
private ApplicationContext ctx=null;
private JdbcTemplate jdbcTemplate;
{
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate=(jdbcTemplate)ctx.getBean("jdbcTemplate");
}
//执行insert,update,delete
public void testUpdate(){
String sql="update employee set last_name=? where id=?";
jdbcTemplate.update(sql,"Jack",5);
}