spring框架底层逻辑(面向切面和事务管理)

AOP(面向切面)和事务管理

目录

情景1:(面向切面)

 情景2:(事务控制)


情景1:(面向切面)

需要导入的jar包有

创建IBankDao接口,并在接口里面写入四个未实现的方法

package dao;

public interface IBankDao {
    public void saveMoney();
    public void sendMoney();
    public void loan();
    public void manageMoney();
}

新建接口实现类BankDaoImpl实现IBankDao未实现的方法

package impl;

import dao.IBankDao;

public class BankDaoImpl implements IBankDao {

    @Override
    public void saveMoney() {
        System.out.println("存钱操作");
    }

    @Override
    public void sendMoney() {
        System.out.println("取钱操作");
    }

    @Override
    public void loan() {
        System.out.println("贷款操作");
    }

    @Override
    public void manageMoney() {
        System.out.println("理财操作");
    }
}

银行的业务逻辑一般要先认证身份,所以在这里我们新建一个验证身份的类AdminCheck 

package impl;

public class AdminCheck {
    public void check(){
        System.out.println("正在验证身份。。。。");
    }
}

新建TransactionManager类

package impl;

import org.aspectj.lang.ProceedingJoinPoint;

public class TransactionManager {
    public void beginTransaction(){
        System.out.println("事务的开始");
    }
    public void commitTransaction(){
        System.out.println("提交事务");
    }

    public void doincepter(ProceedingJoinPoint point){
        try {
            beginTransaction();
            point.proceed();
            commitTransaction();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}

在新建一个写日志的类LogInfo

package impl;

public class LogInfo {
    public void writerLog(){
        System.out.println("正在写入日志文件");
    }
}

在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-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    <!--依赖注入-->
    <bean id="adminCheck" class="impl.AdminCheck"></bean>
    <bean id="transactionManager" class="impl.TransactionManager"></bean>
    <bean id="logInfo" class="impl.LogInfo"></bean>
    <bean id="bankDao" class="impl.BankDaoImpl"></bean>
    
    <aop:config>
        <aop:pointcut id="servicepoint" expression="execution(* impl.*.*(..))"/>

        <!--前端切入点-->
        <aop:aspect ref="adminCheck">
            <aop:before method="check" pointcut-ref="servicepoint"/>
        </aop:aspect>
        <!--切入的方式:环绕切入-->
        <aop:aspect ref="transactionManager">
            <aop:around method="doincepter" pointcut-ref="servicepoint"/>
        </aop:aspect>
        <!--后端切入点-->
        <aop:aspect ref="logInfo">
            <aop:after method="writerLog" pointcut-ref="servicepoint"/>
        </aop:aspect>

    </aop:config>


</beans>

新建测试类Test进行测试

package test;

import dao.IBankDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {
    public static void main(String[] args) {
        test1();
    }

    private static void test1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        IBankDao bd = ac.getBean("bankDao", IBankDao.class);
        bd.sendMoney();
    }
}

测试结果

 情景2:(事务控制)

这里我们通过spring来连接数据库,需要导入的jar包有

 接着我们需要在XML文件中配置数据源,以方便我们连接MySQL数据库进行操作

<?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"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
    <!--配置数据源-->
    <bean id="dataSoure" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/school"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

其中org.apache.commons.dbcp.BasicDataSource这个class可以在我们导入的jar目录下找到

 创建数据库,新建一个表并且在表中添加几条数据

 新建Class的实体类

package entity;

public class Class {
    private int classid;
    private String classname;

    public int getClassid() {
        return classid;
    }

    public void setClassid(int classid) {
        this.classid = classid;
    }

    public String getClassname() {
        return classname;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    public Class(int classid, String classname) {
        this.classid = classid;
        this.classname = classname;
    }

    public Class() {
    }

    public Class(int classid) {
        this.classid = classid;
    }
}

新建接口IClass并在接口里面写入查询和修改的方法

package dao;

import entity.Class;

import java.util.List;

public interface IClass {
    public List<Class> findAll();
    public int updateClass(Class c);
}

新建ClassDaoImpl实现接口IClass

package impl;

import dao.IClass;
import entity.Class;
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 ClassDaoImpl implements IClass {

    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Class> findAll() {
        return jdbcTemplate.query("select * from class", new RowMapper() {
            @Override
            public Class mapRow(ResultSet rs, int i) throws SQLException {
                Class c = new Class(rs.getInt(1),rs.getString(2));
                return c;
            }
        });
    }

    @Override
    public int updateClass(Class c) {
        return jdbcTemplate.update("update class set classname=? where classid=?",c.getClassname(),c.getClassid());
    }
}

在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"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
    <!--配置数据源-->
    <bean id="dataSoure" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/school"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSoure"></property>
    </bean>
    <bean id="classDao" class="impl.ClassDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSoure"></property>
    </bean>
    <!--通知式事务-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="txpoint" expression="execution(* impl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txpoint"/>
    </aop:config>
</beans>

测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月与清酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值