spring学习

本文深入探讨了Spring框架的关键功能,包括IOC/DI、AOP、事务管理、JDBC支持等,详细解释了如何通过配置文件和注解方式管理对象,以及Spring在事务处理、数据库操作方面的优势。

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

Spring的功能:管理对象、aop/ioc、springjdbc、aop事务、junit测试

spring的包(beans、context、core、expression、aop、aspects、c3p0、jdbc、tx、新老日志、weaver、aopalliance等):

    

spring添加约束(添加约束是为了方便配置文件的书写):

    各种约束文件的位置:

    

    添加约束的方式:选择windows---perference----xml Catalog,具体去网上搜索一下,最后达到的效果是,在配置文件中自动给生辰头:

   <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://www.springframework.org/schema/beans" 
       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.2.xsd 
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.2.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

</bean>

spring IOC/DI:

     IOC:控制反转

    DI:依赖注入

spring 的几个属性:

    (1)scope对象范围

        

        singleton(在spring容器中只存在一个对象如service、model)、prototype(多例,每次对象都是重新创建)

    (2)init/destory 对象的初始化/销毁

        

        方法是User类的方法:

        

    (3)配置文件模块导入import

        可以有很多的spring的配置文件,将多个applicationContext-X.xml文件放在同一个配置文件中,统一管理。

        

    (4)属性注入

      普通属性注入,类中的属性需要有get/set方法,属性名称和配置文件中的要一致:

    <bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
    </bean>

    <bean name="dataSource" class="XXX">
        <property name="user" value="${jdbc.user}"></property>
    </bean>  

        数组集合属性注入,注意与类中属性的名称要对应:

        

        数组:

            

        List集合:

        

        Map:

        

        

Spring管理对象:

    (1)配置文件的方式

在applicationContext.xml中添加要管理的对象。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
    <context:property-placeholder location="classpath:db.properties"/>
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <bean name="accountDao" class="cn.hu.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean name="service" class="cn.hu.service.impl.AccountServiceImpl">
        <property name="dao" ref="accountDao"></property>
    </bean>
db.properties:

jdbc.jdbcUrl=jdbc:mysql:///hibernate
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=mysql

    可以为对象添加scope、init/destory等。具体使用service等对象的时候是不需要加注解的。

    (2)注解的方式

    在service、Controller中添加注解,Spring来负责管理对象

applicationContext.xml:

<!-- 使用注解时的扫描 -->
<context:component-scan base-package="cn.hu.bean"></context:component-scan>

在具体需要被管理的类上添加@Service、@Component、@Controller等:

    

    

    

引用类型属性的注入:
(1)@Autowired方式,不用添加get/set,根据类型做区分的,如果注入了多个同类型的就不能区分,所以可以添加@Qualifier("car2")注解:

Class Sdervice{

    @Autowired

    @Qualifier("car2")

    public Dao dao;//不需要get/set方法

}

(2)@Resource方式推荐,属性需要有get/set方法。

类中的方法:

也可以在一个类中添加@Before、@After。加注解都是自动的方式。

Spring-junit整合:

        需要的包:spring-test包、Junit测试包

        

spring-aop:

    aop是一种思想:面向切面编程。使用场景:过滤器、拦截器、事务。

spring-aop配置文件的方式:

    这是我们想要达到的效果:

    (1)目标对象

    

    (2)通知类

    

    

    

    (3)将通知织入目标对象

    

    

    (4)测试

    

spring-aop的xml注解的方式:

    

    

    

    

    优化的方式:

    

    spring-aop可以用于添加日志、事务、拦截器。因为spring本身封装实现了事务,springmvc封装实现了拦截器,所以对于事务可以直接使用spring来实现,拦截器可以直接使用springmvc来实现。

spring事务:

    spring本身封装实现了事务,所以不用spring-aop复杂的操作,可以简单的使用,下面进行介绍。

    事务的四个特性:

    原子性:一个操作不可再被分,要不全部成功,要不全部失败

    一致性:前后一致,比如AB之间转账,操作完成之后两者的总额应该是不变的

    隔离性:并发事物之间的操作应该是互相不影响的,隔离的

    持久性:事务操作一旦成功就是在数据库中进行了变化,是肯定的操作

    并发事务给隔离性带来的问题:

    脏读:读了别人还未提交的数据,有可能别人会回滚,读到的数据就是脏数据了

    不可重复读:第二次读的数据是被别人改过的,导致两次读的数据不一致

    幻读:读的数据是虚幻的,就是A事务正在读,这个时候B事务正在添加,所以A读的数据是有问题的

    为了解决并发事务引起的问题,spring提供了不同的隔离级别来解决并发问题:

    读未提交:还是读没有提交的事务,这个级别是没有解决问题的

    读已提交:读已经提交的数据,解决了脏读

    可重复读:可以重复读,A事务读的时候B事务就不能对共享数据操作了,保证重复读的是正确的数据,解决了脏读、不可重复读

    串行化:并共享数据加锁,保证并发事务串行执行,解决了脏读、不可重复读、幻读

 spring对事物提供的属性

    (1)隔离级别

    (2)是否只读:一般用于只读的方法,保证不能修改表数据

    (3)传播行为

        PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 
        PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。 
        PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。 
        PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。 
        PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 
        PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。

Spring使用事务的时候必须有事务管理器。spring实现有两种方式:(1)配置文件方式(2)注解的方式

(1)配置文件方式

    <context:property-placeholder location="classpath:db.properties"/>
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean name="accountDao" class="cn.hu.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean name="service" class="cn.hu.service.impl.AccountServiceImpl">
        <property name="dao" ref="accountDao"></property>
    </bean>

    

    

    也可以直接这样写:

    <aop:advisor>中不使用pointcut-ref,不使用引用,直接使用包

    <aop:advisor id="managerTx1" advice-ref="txAdvice" pointcut="execution(* com.pcitc.stnpool.base.service.inter.*.*(..))" order="1" />

    service层使用的时候就还是正常的使用,事务是自动进行管理的。

(2)注解方式

       <context:property-placeholder location="classpath:db.properties"/>
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean name="accountDao" class="cn.hu.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean name="service" class="cn.hu.service.impl.AccountServiceImpl">
        <property name="dao" ref="accountDao"></property>
    </bean>
    <tx:annotation-driven/><!-- 事务管理的注解开启 -->

    <context:component-scan base-package="cn.hu.bean"></context:component-scan>
Service层使用的时候:

Spring-jdbc管理:

     spring提供了操作数据库的JdbcTemplate。它肯定是作用在service或者dao上的。

    

    

    

    当然使用的时候也可以直接将使用的对象在applicationContext.xml文件中配置好:

    

    测试使用:

    

spring-jdbc扩展之JDBCDaoSupport使用:

    在spring-jdbc的基础上作了进一步封装,直接使用JDBCDaoSupport提供的方法。

Spring JdbcTemplate与Spring namedParameterJdbcTemplate区别:

JDBC使用?占位符:

String getdataSql = "SELECT * FROM CPE_NOTE_QUERY_ITEM WHERE REG_ID = ? ";

return jdbcTemplate.queryForList(getdataSql,reg_id);

namedParameterJdbcTemplate使用:字段名占位符:

//插入贴现录入主表

String insert_dis_info = "INSERT INTO C (FU,CO) VALUES(":FU,:CO)";

namedParameterJdbcTemplate.update(insert_dis_info,params[0]);

总结:Spring提供了直接操作数据库的方式:JdbcTemplate和namedParameterJdbcTemplate

        Spring整合Mybatis之后可以直接使用Mapper

 

 

 

 

 

        

     

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3161662/blog/2465915

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值