Aop1
一、Aop底层实现
(一)设置异常增强的步骤
- spring 设置增强bean
- 设置异常增强
- 首先设置切面(类似蛋糕的截面)
- 设置切点
- 切面与增强bean进行关联(引入增强bean)
<aop:aspect ref="aopError"></aop:aspect>
- 设置异常增强
<aop:after-throwing method="affterError" pointcut-ref="pointcut" throwing="e"></aop:after-throwing>
(二)详解
(1)所建包
(2)所需jar包
(3)完整代码
com.offcn.aop
package com.offcn.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import java.util.Arrays;
public class AopError {
/*JoinPoint 这个类所有动态代理类的信息*/
public void affterError(JoinPoint joinPoint, RuntimeException e){
/*joinPoint.getSignature() 这个方法是拿到你的动态代理的签名文件*/
System.out.println(joinPoint.getSignature().getName()+"这个方法异常信息"+e.getMessage());
}
public void after(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName());
}
/*环绕增强 传递的是其JoinPoint子类*/
public void around(ProceedingJoinPoint proceedingJoinPoint){
System.out.println(proceedingJoinPoint.getTarget()+ Arrays.toString(proceedingJoinPoint.getArgs()));
/*调用此方法执行目标对象相应的方法*//*这个方法又相当于一个分割线,也就是前置与后置的分割线*/
try {
proceedingJoinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println(proceedingJoinPoint.getSignature().getName());
}
}
dao及其实现类
com.offcn.dao和impl
package com.offcn.dao;
public interface UserDao {
public void add();
}
package com.offcn.dao.impl;
import com.offcn.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public void add() {
System.out.println("增加成功");
// throw new RuntimeException("出错了!");
}
}
service和impl
package com.offcn.service;
public interface UserService {
public void add();
}
package com.offcn.service;
import com.offcn.dao.UserDao;
import com.offcn.service.UserService;
public class UserServiceDaoImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void add() {
userDao.add();
}
}
test
package com.offcn.test;
import com.offcn.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void test(){
//加载xml文件
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userServiceDao = (UserService) app.getBean("userService");
userServiceDao.add();
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!--设置dao层-->
<bean id="userDao" class="com.offcn.dao.impl.UserDaoImpl"></bean>
<!--设置Service层-->
<bean id="userService" class="com.offcn.service.UserServiceDaoImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!--设置增强aop-->
<bean id="aopError" class="com.offcn.aop.AopError"></bean>
<aop:config>
<!--对那一方法或者哪一个类进行植入--><!--expression 每个参数对哪一个类进行注入,这句话execution代表所有方法,*代表全部方法-->
<aop:pointcut id="pointcut" expression="execution(* com.offcn.service.UserService.*(..))"></aop:pointcut>
<!--引入方法增强bean-->
<aop:aspect ref="aopError">
<!--在这个里面可以配置--><!--这个方法必须对应引入增强bean的方法,在这里面要设置切点-->
<!--<aop:after-throwing method="affterError" pointcut-ref="pointcut" throwing="e"></aop:after-throwing>-->
<!--后置增强-->
<!--<aop:after method="after" pointcut-ref="pointcut"></aop:after>-->
<!--前置增强-->
<!--<aop:before method="."-->
<!--环绕增强,在前后都做相关操作,相当于一个前置和一个后者增强-->
<aop:around method="around" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
</aop:config>
</beans>
(4)代码执行流程
- 先执行Test
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
- 执行applicationContext.xml
<aop:config>
<!--对那一方法或者哪一个类进行植入--><!--expression 每个参数对哪一个类进行注入,这句话execution代表所有方法,*代表全部方法-->
<aop:pointcut id="pointcut" expression="execution(* com.offcn.service.UserService.*(..))"></aop:pointcut>
<!--引入方法增强bean-->
<aop:aspect ref="aopError">
<!--在这个里面可以配置--><!--这个方法必须对应引入增强bean的方法,在这里面要设置切点-->
<!--<aop:after-throwing method="affterError" pointcut-ref="pointcut" throwing="e"></aop:after-throwing>-->
<!--后置增强-->
<!--<aop:after method="after" pointcut-ref="pointcut"></aop:after>-->
<!--前置增强-->
<!--<aop:before method="."-->
<!--环绕增强,在前后都做相关操作,相当于一个前置和一个后者增强-->
<aop:around method="around" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
</aop:config>
- 执行AopError
package com.offcn.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import java.util.Arrays;
public class AopError {
/*JoinPoint 这个类所有动态代理类的信息*/
public void affterError(JoinPoint joinPoint, RuntimeException e){
/*joinPoint.getSignature() 这个方法是拿到你的动态代理的签名文件*/
System.out.println(joinPoint.getSignature().getName()+"这个方法异常信息"+e.getMessage());
}
public void after(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName());
}
/*环绕增强 传递的是其JoinPoint子类*/
public void around(ProceedingJoinPoint proceedingJoinPoint){
System.out.println(proceedingJoinPoint.getTarget()+ Arrays.toString(proceedingJoinPoint.getArgs()));
/*调用此方法执行目标对象相应的方法*//*这个方法又相当于一个分割线,也就是前置与后置的分割线*/
try {
proceedingJoinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println(proceedingJoinPoint.getSignature().getName());
}
}
- 执行Test
UserService userServiceDao = (UserService) app.getBean("userService");
userServiceDao.add();
二、JDBC
(一)概述
Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常,处理事务,到最后关闭连接。
所以当从数据库中获取数据时,你所做的是定义连接参数,指定要执行的 SQL 语句,每次迭代完成所需的工作。
(二)步骤详解
- 所建包
- 导包(jar包)
- 获取数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--拿到数据库的信息-->
<property name="url" value="jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=utf-8"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
- 创建实体类pojo或者entity
package com.offcn.pojo;
import java.io.Serializable;
public class Account implements Serializable {
private int aid;
private double abalance;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public double getAbalance() {
return abalance;
}
public void setAbalance(double abalance) {
this.abalance = abalance;
}
}
- 创建dao、service类及其实现类impl
package com.offcn.dao;
import com.offcn.pojo.Account;
import java.util.List;
public interface AccountDao {
//根据ID查询
public Account selectById(int aid);
//查询所有
public List<Account> selectAll();
//根据ID修改,影响的行数
public int update(Account account);
//添加
public int insert(Account account);
//根据ID删除
int deleteById(int aid);
}
package com.offcn.dao.impl;
import com.offcn.dao.AccountDao;
import com.offcn.pojo.Account;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class AccountDaoImpl implements AccountDao {
//spring提供操作数据库的类
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public Account selectById(int aid) {
String sql = "select * from account where aid = ?";
Object[] objects = {aid};
/*返回发每一行数据都在这个类型,第一个参数为:返回的结果集,第二个参数为:当前索引*/
RowMapper<Account> rowMapper = new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
System.out.println(i+"dhhddhdhd");
Account account = new Account();
account.setAid(resultSet.getInt("aid"));
account.setAbalance(resultSet.getDouble("abalance"));
return account;
}
};
Account account1 = jdbcTemplate.queryForObject(sql,objects,rowMapper);
return account1;
}
@Override
public List<Account> selectAll() {
String sql = "select * from account";
RowMapper<Account> accountrowMapper = new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setAid(resultSet.getInt("aid"));
account.setAbalance(resultSet.getDouble("abalance"));
return account;
}
};
List<Account> list = jdbcTemplate.query(sql,accountrowMapper);
return list;
}
@Override
public int update(Account account) {
String sql = "update account set abalance = ? where aid = ?";
Object[] objects ={account.getAbalance(),account.getAid()};
int num = jdbcTemplate.update(sql,objects);
return num;
}
@Override
public int insert(Account account) {
String sql = "insert into account(abalance) values(?)";
Object[] objects = {account.getAbalance()};
int num = jdbcTemplate.update(sql,objects);
return num;
}
@Override
public int deleteById(int aid) {
String sql = "delete from account where aid = ?";
Object[] objects = {aid};
int num = jdbcTemplate.update(sql,objects);
return num;
}
}
package com.offcn.service;
import com.offcn.pojo.Account;
import java.util.List;
public interface AccountService {
//根据ID查询
public Account selectById(int aid);
//查询所有
public List<Account> selectAll();
//根据ID修改,影响的常熟
public int update(Account account);
//添加
public int insert(Account account);
//根据ID删除
int deleteById(int aid);
}
package com.offcn.service.impl;
import com.offcn.dao.AccountDao;
import com.offcn.pojo.Account;
import com.offcn.service.AccountService;
import java.util.List;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public Account selectById(int aid) {
return accountDao.selectById(aid);
}
@Override
public List<Account> selectAll() {
return accountDao.selectAll();
}
@Override
public int update(Account account) {
return accountDao.update(account);
}
@Override
public int insert(Account account) {
return accountDao.insert(account);
}
@Override
public int deleteById(int aid) {
return accountDao.deleteById(aid);
}
}
- 配置xml文件
<!--spring 给我们提供操作数据库的类-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--设置dao层-->
<bean id="accountDao" class="com.offcn.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--设置service层-->
<bean id="accountService" class="com.offcn.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
- 创建测试类text
package com.offcn.text;
import com.offcn.pojo.Account;
import com.offcn.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Text {
@Test
public void text(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountService");
Account account = accountService.selectById(1);
System.out.println(account.getAid()+"\t"+account.getAbalance());
}
@Test
public void text1(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountService");
List<Account> list = accountService.selectAll();
for (Account a:list) {
System.out.println(a.getAid()+"\t"+a.getAbalance());
}
}
@Test
public void text2(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountService");
Account account = new Account();
account.setAid(1);
account.setAbalance(10000);
int num = accountService.update(account);
System.out.println(num);
}
@Test
public void text3(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountService");
Account account = new Account();
account.setAbalance(50000);
int num = accountService.insert(account);
System.out.println(num);
}
@Test
public void text4(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountService");
int num = accountService.deleteById(6);
System.out.println(num);
}
}