SSM+R之Spring

1.Spring?
Spring所提供的是一个支持它们开发的模板。

2.控制反转(IoC)?
①控制反转是一种通过描述(在Java中可以是XML或者注解)并通过第三方去产生或获取特定对象的方式。
②在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入。(Dependency Injection, DI)

3.SpringIoC容器的作用?
①SpringIoC容器可以容纳我们所开发的各种Bean,并且我们可以从中获取各种发布在SpringIoC容器里的bean,并通过描述得到它。
②SpringIoC容器的设计主要是基于BeanFactory和ApplicationContext两个接口,其中ApplicationContext是BeanFactory的子接口之一。主要使用ApplicationContext接口,
因为它扩展了很多功能。

4.SpringIoC容器的初始化和依赖注入?
①Bean的初始化和依赖注入在SpringIoC容器中是两大步骤,它是在初始化之后,才会进行依赖注入的。
②Bean的初始化分三步:Resource定位、BeanDefinition的载入(获取对应的POJO)和BeanDefinition的注册(把POJO注册给IoC容器,让别人可以通过IoC容器获取)。
③对于依赖注入,Bean有一个lazy-init选项,其含义是是否初始化SpringBean。默认default为false,即自动初始化Bean。当设置为true时,只有在调用getBean方法时,它才会
进程初始化,完成依赖注入。

5.Spring Bean的生命周期?
①SpringIoC容器的本质目的就是为了管理Bean。
②对于Bean而言,在容器中存在生命周期,它的初始化和销毁也需要一个过程,在一些需要自定义的过程中,我们可以插入代码去改变它们的一些行为,以满足特定的需求。
③初始化->BeanNameAware的setBeanName方法->BeanFactoryAware的setBeanFactory方法->ApplicationContextAware的setApplicationContext->
BeanPostProccessor的postProcessBeforeInitialization方法->InitializationBean的afterPropertiesSet方法->自定义初始化->
BeanPostProcessor的postProcessAfterInitialization方法->生存期->DisposableBean的destroy方法->自定义销毁方法。
④BeanPostProcessor的接口是针对所有Bean而言,DisposableBean是针对IoC容器的。其它都是针对单个Bean而言。ApplicationContextAware要容器实现
ApplicationContext接口。

6.依赖注入的三种方式?
①构造器注入
②setter注入(最常用)
③接口注入

7.装配Bean的三种方式?
①自动装配(@Autowired解决注入对象,应该理解为按类型自动注入。@Primary首要注入和@Qualifier按名称注入。)>②注解装配(自己开发的Bean)>③XML装配(第三
方的Bean)

8.Spring提供4种作用域?
①单例(singleton):默认,整个应用中,Spring只为其生成一个Bean的实例。
②原型(prototype):当每次注入,或者通过SpringIoC容器获取Bean时,Spring都会为它创建一个新的实例。
③会话(session):在Web应用中使用,就是在会话过程中Spring只创建一个实例。
④请求(request):在Web应用中使用,就是在一次请求中Spring会创建一个实例,但是在不同的请求会创建不同的实例。

9.Spring对AOP的支持?
SpringAOP是一种基于方法拦截的AOP。

10.Spring动态代理模式?
①当类的实现存在接口,则通过JDK动态代理模式。
②当类的实现不存在接口,则通过CGLIB动态代理模式。

11.AOP的术语?
①切面(Aspect):切面就是在一个什么样的环境中工作。(可以理解为一个拦截器)
②通知(Advice):通知是切面开启后,切面的方法。Before、After、AfterReturning、AfterThrowing、Around。
③引入(Introduction):引入允许我们在现有的类里添加自定义的类和方法。
@DeclareParents(value=”RoleServiceImpl+”, defaultImpl=RoleVerifier.class)
public RoleVerifier roleVerifier;
RoleVerifier roleVerifier = (RoleVerifier) roleService; //通过绑定真实对象下挂接口实现。
④切点(Pointcut):在动态代理中,被切面拦截的方法就是一个切点。(真是业务逻辑)
⑤连接点(join point):连接点是一个判断条件,由它可以指定哪些是切点。
⑥织入(Weaving):织入是一个生成代理对象的过程。

12.使用@Aspect开发AOP的流程?
①选择切点。(接口加实现类JDK动态代理)
②创建切面。(通过定义@Pointcut简化)
③创建Config类,通过@Bean得到一个Aspect实例。(@Configuration、@EnableAspectJAutoProxy、@ComponentScan)

13.注意注解和XML两种方式区别?
①注解的around before在before前打印。(感觉bug)
②XML的around before在before 后打印。(正确)

14.Spring多个切面问题?
①当有多个切面时,在测试过程中它不会存在任何顺序,这些代码会顺序随机生成。
②Spring的底层是通过责任链模式(后进先出)来处理多个切面的。

15.配置MyBatis-Spring项目的步骤:
①配置数据源。
②配置SqlSessionFactory。
③可以选择的配置有SqlSessionTemplate(线程安全类,确保每个线程使用SqlSession唯一且不冲突),在同时配置SqlSessioinTemplate和SqlSessionFactory的情况下,优先采
用SqlSessionTemplate。
④配置Mapper,可以配置单个Mapper,也可以通过扫描的方法生成Mapper,比较灵活。此时SpringIoC会生成对应的接口的实例,这样就可以通过注入的方式来获取资源了。
⑤事务管理。

16.配置MapperScannerConfigurer:
①basePackage:指定让Spring自动扫描什么包。
②annotationClass:@RepositoryMyBatis作为持久层,@ServiceSpring作为业务层,@ControllerSpringMVC作为控制器层。
③SqlSessionFactoryBeanName:SqlSessionFactory这个Bean的名字。通常就为SqlSessionFactory。
④markerInterface:指定实现了什么接口就认为它是Mapper。我们需要提供一个公共接口去标记。(不常用,与annotationClass功能一样。)

17.数据库的ACID特性?
原子性:记录之前的版本,允许回滚
一致性:事务开始和结束之间的中间状态不会被其他事务看到
隔离性:适当的破坏一致性来提升性能与并行度 例如:最终一致~=读未提交。
持久性:每一次的事务提交后就会保证不会丢失

18.丢失更新?
①第一种:一个回滚,一个提交成功导致不一致。(大部分数据库包括MySQL和Oracle都消灭了)
②第二种:在不同事务中,无法探知其他事务的操作。所以提出了隔离级别,在不同程度上减少丢失更新的可能性。

19.隔离级别(isolation)?
选择隔离级别的出发点在于两点:性能和数据一致性。性能下降,数据一致性提高。
①脏读(dirty read):允许一个事务去读取另一个事务未提交的数据。
②读/写提交(read commit):一个事务只能读取另一个事务提交的数据。(企业常选择的方式)
③可重复读(repeatable read):解决不可重复读的问题。
④序列化(Serializable):克服幻读(phantom read)。

20.传播行为(propagation)?
①REQUIRED:当方法调用时,如果不存在当前事务,那么就创建事务。;如果之前的方法已经存在了事务,那么就沿用之前的事务。
②REQUIRES_NEW:无论是否存在当前事务,方法都会在新的事务中运行。
③NESTED:嵌套事务,也就是调用方法如果抛出异常只回滚自己内部执行的SQL,而不回滚主方法的SQL。

21.数据库资源的产生和释放如果没有委托给数据库管理器,那么就由jdbcTemplate管理,但是此时已经委托给了事务管理器,所以jdbcTemplate的数据库资源和事务已经由事务
管理器处理了。

22.使用声明式事务需要配置注解驱动,需要在配置文件中加入如下配置就可以使用@Transactional配置事务了。

<tx:annotation-driven transaction-manager="transactionManager" />

23.常用代码

/*****************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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
</beans>

/*****************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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
</beans>

/*****************配置MyBatis-Spring项目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:p="http://www.springframework.org/schema/p"
    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.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <context:component-scan base-package=""></context:component-scan>
    <context:property-placeholder ignore-resource-not-found="false"
    location="database.properties, dbcp2.properties"/>
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${database.driver}"></property>
        <property name="url" value="${database.url}"></property>
        <property name="username" value="${database.username}"></property>
        <property name="password" value="${database.password}"></property>
        <property name="maxTotal" value="${dbcp2.maxTotal}"></property>
        <property name="maxIdle" value="${dbcp2.maxIdle}"></property>
        <property name="maxWaitMillis" value="${dbcp2.maxWaitMillis}"></property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="mybatis-config.xml"></property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value=""></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <property name="annotationClass" value="org.springframework.stereotype.Repository"></property>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值