1.IOC,这个最简单了,哪怕没有用过spring的人肯定也都听说过这个
spring的注入方式:
##############Set注入,这是最简单的注入方式,假设有一个SpringAction,类中需要实例化一个SpringDao对象,那么就可以定义一个private的SpringDao成员变量,然后创建SpringDao的set方法(这是ioc的注入入口)。
##############构造器注入,这种方式的注入是指带有参数的构造函数注入,看下面的例子,我创建了两个成员变量SpringDao和User,但是并未设置对象的set方法,所以就不能支持第一种注入方式,这里的注入方式是在SpringAction的构造函数中注入,也就是说在创建SpringAction对象时要将SpringDao和User两个参数值传进来
##############静态工厂的方法注入,实例工厂的意思是获取对象实例的方法不是静态的,所以你需要首先new工厂类,再调用普通的实例方法。
http://blessht.iteye.com/blog/1162131
通常都用set和constructor注入
2.命名空间:这个东西有点类似于struts2的OGNL表达式,反正外形都是一模一样的。p命名空间一般用于注入 属性,P的单词就是property. AOP命名空间主要用来做切面编程,非常常见的。spring命名空间的出现主要是为IOC服务的,既然是注入就要 有一个谁将注入给谁,在配置文件里面描述。命名空间能让我们在配置文件里面少写几个字。别的可以参考这 个人的博客:http://blog.csdn.net/joker_zhou/article/details/8551692 像AOP那种命名空间自然是不能省的,其他可以拿别的来代替。 层层架构里面相互的依赖在配置文件里描述为<bean autowire=“bytype”>,但是这个autowire="bytype"是写 在上一层里面的。因为上一层里面有下一层属性的引用,有引用就会有引用类型,bytype就是看中了这个特点 来注入。还有no不装配,byname,constructor,都不如bytype好。 3.AOP:面向切面的编程,是对面向对象编程的补充,面向对象肯定有很多涉及不到的地方。在不改变原有代码 的基础上为这段代码添加新的功能。比如说日志,比如说事务。spring之所以支持AOP是因为spring集成了 AspectJ,AspectJ可是专门搞这个的。先说配置文件这边,要有切点,切面。点就是地点,就是AOP在哪里动手 。切面,要插一脚过来的那个类是谁。 <aop:pointcut exepression="execute(public void addUser(com.pb.entity.User) id="servicePointcut")">这个是切点 <aop:aspect ref="serviceLogging">这个是切面 <aop:before method="beforeService" pointcut-ref="servicePointcut">这个是指引位置和具体到切面上的 beforeService, 不可能拿一整个类去切,我刚学的时候死活认不清是什么意思,现在我明白了看AOP后面的单 词就能猜出来。 4.增强处理:一般的情况下日志,事务就够了,我都没见过其他的 后置增强:具体位置的标签<aop:after-returning metnod="...." point-cut="......" returning="returnVal"> returning="returnVal"为增强的方法返回值,如果不要返回值这个就不用配 异常增强:具体位置的标签<aop:after-throwing method="......" point-cut="....." returning="returnVal"> 最终增强:<aop:after>
环绕增强:<aop:around>
5.spring事物
理解spring事物是一项难度较大的挑战。
#########声明式事物:
这个不好理解,我当时怎么都不懂这些乱7 8糟的名词,声明式事物是以配置的方式管理事物,它基于AOP实现,与代码完全分离,配置即可用,减少了工作量。
事物是一个抽象接口,根据底层框架的不同选择不同的实现类。
父接口:public interface PlatformTransactionManager
TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
void commit (TransactionDefinition definition) throws TransactionException;
void rollback (TransactionDefinition definition) throws TransactionException;
getTransaction() 根据类型为TransactionDefinition的参数返回一个TransactionStatus对象.
返回的 TransactionStatus对象可能代表一个新的或已经存在的事务(如果在当前调用堆栈有一个
符合条件的事务)
TransactionDefinition definition 的属性如下:
1.Propagation (事务的传播属性) 目的是为了防止创建多个事物,具体以后再研究 http://blog.sina.com.cn/s/blog_551d2eea0101e6ul.html
----------------propagation-required 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
----------------propagation-supports 支持当前事务,如果当前没有事务,就以非事务方式执行。
----------------propagation-mandatory 使用当前的事务,如果当前没有事务,就抛出异常。
----------------propagation-requires-new 新建事务,如果当前存在事务,把当前事务挂起。
----------------propagation-not-supported 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
----------------propagation-never 以非事务方式执行,如果当前存在事务,则抛出异常。
----------------propagation-nested 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。
2.Isolation (事物的隔离等级) 目的貌似是和线程排队是一个道理,以后再研究
http://www.2cto.com/database/201409/331764.html 这篇博客看隔离等级那一部分,写的挺好
http://www.cnblogs.com/zhangpengme/archive/2011/11/24/2261975.html 数据库事务隔离的文章,spring中的事务隔离和数据库的事务隔离一回事
http://blog.sina.com.cn/s/blog_616e189f0100zp16.html 从源码角度分析
http://blog.csdn.net/moneyshi/article/details/50786786 面试1
http://blog.csdn.net/moneyshi/article/details/52917254 面试2
http://blog.csdn.net/moneyshi/article/details/53086927 面试3
Hibernate 实现类:org.springFramework.orm.Hibernate3.HibernateTransactionManager
JDBC实现类 : org.springframework.jdbc.datasource.DataSourceTransactionManager
配置文件里面这样写:
http://www.blogjava.net/robbie/archive/2009/04/05/264003.html
<!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="interceptorPointCuts" expression="execution(* com.bluesky.spring.dao.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" /> </aop:config>
6.生命周期
spring生成的bean的生命周期
********找到bean并且实例化
********实现对Bean依赖注入
********假设Bean实现了什么什么接口,走其中的方法
********Bean定义文件中定义init-method,配置文件中定义了init-method方法,则执行initBean()方法
******Bean定义文件中定义destroy-method,配置文件中定义了destroy-method方法,则执行destory()方法
http://www.newhua.com/2011/0512/122322.shtml
7.作用域
在spring生成bean的时候可以声明bean的作用域,只需要在bean里面定义scope即可,
######singleton 单例----------------------->无状态或者状态不能变的类用单例
###### prototype 原型,指的是每次调用时,会重新创建该类的一个实例,比较类似于我们自己自己new的对象实例
###### request 每次HTTP请求都会创建一个新的Bean
###### session 同一个HttpSession共享同一个Bean,不同的HttpSession不共享同一个Bean
###### globleSession 同一个全局session共享一个Bean
bean默认的scope属性是’singleton‘
8.BeanFactory和ApplicationContext的区别
public class Test{
//使用ApplicationContext接口的实现类ClassPathXmlApplicationContex加载Spring的配置文件
ApplicationContext ctx =new ClassPathXmlApplicationContex ("applicationContext.xml");
//通过ApplicationContext接口的getBean()方法获取id或name为userBiz的Bean实例
IUserBiz biz=(IUserBiz) ctx.getBean("userBiz")
User user =new User();
biz.addNewUser(user)
}
从代码可以看出是在分解spring的配置文件并且从中拿取Bean
http://blog.sina.com.cn/s/blog_517d8cf70100ayyy.html 这个博客是看原理的
http://blog.csdn.net/u011202334/article/details/51509235 这个博客是看区别和联系
BeanFactory还能在实例化对象的时生成协作类之间的关系。此举将bean自身与bean客户端的配置中解放出来。BeanFactory还包含了bean生命周期的控制,调用客户端的初始化方法(initialization methods)和销毁方法(destruction methods)
http://www.blogjava.net/yejiansuo/archive/2010/10/18/335442.html
表面上看,application context如同bean factory一样具有bean定义、bean关联关系的设置,根据请求分发bean的功能。但application context在此基础上还提供了其他的功能。
1. 提供了支持国际化的文本消息
2. 统一的资源文件读取方式
3. 已在监听器中注册的bean的事件
常见的application的实现类有:ClassPathXmlApllicationContext ,FileSystemApplicationContext 。ApplicationContext建立在BeanFactory的基础之上,可以对企业级开发提供更全面的支持
9.在web项目中配置spring IOC,个人觉得这道题是在考spring IOC的入口
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param>
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>,
ContextLoaderListener:
public class ContextLoaderListener extends ContextLoader implements ServletContextListener
static { try { ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class); defaultStrategies = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException ex) { throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage()); } }
http://www.tuicool.com/articles/raumQ3
只是我不明白为什么有的源码我查不到,别人能查到例如 initWebApplicationContext,实在对Spring ioc的启动过程不了解可以百度initWebApplicationContext
10.在web项目中配置spring MVC,个人觉得这道题是在考spring mvc的入口
<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-value>org.springframework..web.servlet.DispatcherServlet</servlet-value>
<load-on-startup>1</load-on-startup>
<servlet>
<servlet-mapping>example></servlet-mapping>
<url-pattern>*.html</url-pattern>
</web-app>