Spring介绍(四)(事务、监听器)

本文介绍了Spring中的事务控制,包括PlatformTransactionManager接口及其常用实现,如DataSourceTransactionManager和HibernateTransactionManager,以及TransactionDefinition和TransactionStatus在事务管理中的作用。此外,还探讨了Spring的监听器,特别是ContextLoaderListener如何在应用启动时初始化全局唯一的Spring容器,提高性能并解决资源消耗问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Spring中的事务控制

1.Spring中事务控制的API介绍:
------PlatformTransactionManager:平台事务管理器是Spring真正管理事务的对象,是一个接口。常用实现类有如下两个:
 DataSourceTransactionManager :针对JDBC和mybatis事务管理
 HibernateTransactionManager :针对Hibernate事务管理
TransactionDefinition:事务定义的对象
TransactionStatus:事务状态信息的对象
Spring框架进行事务的管理,首先使用TransactionDefinition对事务进行定义。通过PlatformTransactionManager根据TransactionDefinition的定义信息进行事务的管理。在事务管理过程中产生一系列的状态:保存到TransactionStatus中。
事务的Demo
要求:模仿转账,如果出现了异常,事务回滚,金额不变

//实体类
public class Account {
    private Integer id;
    private String name;
    private Float money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
//账户的业务层接口:
public interface IAccountService {
    void transfer(String sourceName,String targetName,Float money);
}
//账户的业务层实现类:
public class AccountServiceImpl implements IAccountService {
    //注入AccountDaoImpl
    private IAccountDao accountDao;

    public void setAccountDao(IAccountDao accountDao) {
     this.accountDao = accountDao;
    }
    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        //通过账户名称查询账户
        Account source = accountDao.queryAccountByName(sourceName);
        Account target = accountDao.queryAccountByName(targetName);

        //修改金额
        source.setMoney(source.getMoney()-money);
        target.setMoney(target.getMoney()+money);

        //执行修改
        accountDao.updateAccount(source);
        accountDao.updateAccount(target);
    }
//持久层接口:
public interface IAccountDao {
    Account queryAccountByName(String name);
    void updateAccount(Account account);
}
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
    @Override
    public Account queryAccountByName(String name) {
        return (Account) getJdbcTemplate().queryForObject("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),name);
    }
    @Override
    public void updateAccount(Account account) {
        getJdbcTemplate().update("update account set money = ? where id = ?",account.getMoney(),account.getId());
    }
}
<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/tx/spring-tx.xsd
              http://www.springframework.org/schema/aop
              http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd">

    <!--加载外部资源文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--装配AccountServiceImpl-->
    <bean id="accountService" class="cn.itcast.service.impl.AccountServiceImpl">
        <!--注入AccountDaoImpl-->
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!--装配AccountDaoImpl-->
    <bean id="accountDao" class="cn.itcast.dao.impl.AccountDaoImpl">
        <!--将DataSource注入到JdbcDaoSupport-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--装配数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--1装配事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--2配置事务策略-->
    <tx:advice id="txAdvice">
        <!-- 配置事务的属性-->
        <tx:attributes>
            <!--
                指定对哪些方法使用事务
                name:方法名称,使用通配符* 代表对所有方法使用事务
                isolation:配置事务的隔离级别,默认使用当前数据库默认的隔离级别
                read-only:是否只读,一般对增删改方法使用false,表示读写,对查询方法使用true,表示只读即可,默认是读写。
                propagation:指定事务的传播行为,默认REQUIRED,增删改的时候使用,SUPPORTS:用于查询
                no-rollback-for:指定对哪种异常不回滚
                rollback-for:指定对哪种异常进行回滚
                timeout:设置事务的超时时间,单位是秒,默认是-1 :永不超时。
             -->
            <tx:method name="*"/>

            <!-- 表示只对query开头的方法使用只读事务和SUPPORTS的传播行为
                相似度越高,匹配度越高。因此query*代表了所有以query开头的方法都会使用它的事务配置。
            -->
            <!--<tx:method name="query*" read-only="true" propagation="SUPPORTS"/>-->
        </tx:attributes>
    </tx:advice>

    <!--3配置AOP-->
    <aop:config>
        <!--配置切入点表达式-->
        <aop:pointcut id="pt1" expression="execution(* cn.itcast.service.impl.*.*(..))"></aop:pointcut>
        <!--配置事务管理器应用到切入点-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>
</beans>

Spring的监听器

//Service接口:
public interface IHelloService {
    void sayHello();
}
public class HelloServiceImpl implements IHelloService {
    private IHelloDao helloDao;
    public void setHelloDao(IHelloDao helloDao) {
        this.helloDao = helloDao;
    }
    @Override
    public void sayHello() {
        System.out.println("业务层的sayHello");
        helloDao.sayHello();
    }
}
//Dao层接口:
public interface IHelloDao {
    void sayHello();
}
//Dao层实现类:
public class HelloDaoImpl implements IHelloDao {
    public void sayHello() {
        System.out.println("持久层的sayHello");
    }
}
//Servlet
public class HelloServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        //初始化容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        IHelloService helloService = (IHelloService) ac.getBean("helloServiceImpl");
        helloService.sayHello();
    }
}

问题:每一个servlet都要手动初始化容器,多个servlet就要初始化多个spring容器。
每次初始化容器,都创建了新的容器对象,消耗了资源,降低了性能。
Spring容器在客户端发送请求的时候,请求到达服务端后才初始化spring容器,效率低。
解决方法:保证容器只有一个,并在应用加载的时候启动,应用卸载的时候销毁。
解决方案:让spring容器在应用加载的时候创建一次即可。spring提供了一个监听器ContextLoaderListener,该监听器会帮助我们初始化一个全局唯一的spring容器。

监听器其实就是用来监听一个对象的创建时机的。
Spring的ContextLoaderListener这个监听器其实就是用来监听ServletContext对象的创建时机。当spring的监听器一旦监听到ServletContext对象被创建了,监听器就会去加载applicationContext.xml,去初始化IOC容器,并且将IOC容器存入了ServletContext域中。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <!--指定applicationContext.xml的位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--配置spring监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>cn.itcast.web.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>
//servlet
public class HelloServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        //初始化容器
        //ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //通过工具类获取spring容器
        WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IHelloService helloService = (IHelloService) ac.getBean("helloService");
        helloService.sayHello();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值