目录
5.IOC:控制反转,将原本在程序中手动创建对象的控制权交给了Spring框架来管理。
6.DI:依赖注入,在Spring负责创建Bean对象时,动态的将依赖对象属性注入到bean组件
8.BeanFactory接口和ApplicationContext接口的区别
21.是否直接可以在Servlet中加载Spring配置文件
Spring
1.谈谈Spring
Spring是分层的JavaEE/SE的一站式的轻量级的开源框架,是为了简化企业级开发应用而诞生的,其核心是IOC和AOP,IOC是控制反转,AOP是面向切面编程,Spring框架提供了JavaEE每一层的解决方法,web层:SpringMVC ,业务层:SpringBean、事务管理,AOP,持久层:jdbcTemplate、ORM框架等如:MyBatis和Struts2等,利用Spring框架,可以让开发者通过配置文件创建和组装对象及对象之间的依赖关系,Spring面向切面编程,可以帮助开发者无耦合实现日志记录,权限管理,性能分析,事务管理等。
2.Spring包含JavaEE中三层每一层的解决方案
Web层 :SpringMVC
业务逻辑层:SpringBean管理、AOP、事务管理
数据持久层:SpringJDBCTemplate、ORM模块
3.Spring 框架的优点
(1)方便解耦,简化开发:Spring是一个大场,可以将所有对象的创建和依赖关系的维护都交给Spring来管理
(2)AOP编程的支持:Spring提供面向切面编程,可以方便的对程序进行权限拦截、运行监控等
(3)声明式事务的管理:只需要通过配置就可以完成对事务的管理,而无需手动编程
(4)方便程序的测试:支持junit4
(5)方便集成各种优秀的框架
(6)降低了JavaEE API的使用难度
4.首先需要导入的4个包
bean(提供框架基础部分,控制反转和依赖注入)
core(常用工具类)
context(拓展类)
expression -Spring表达式语言包
5.IOC:控制反转,将原本在程序中手动创建对象的控制权交给了Spring框架来管理。
6.DI:依赖注入,在Spring负责创建Bean对象时,动态的将依赖对象属性注入到bean组件
7.IOC和DI的区别
(1)IOC是将原来对象的创建权交给了Spring框架来管理
(2)DI是指在Spring负责创建Bean对象时,将依赖属性通过配置注入到bean组件当中
(3)DI依赖于IOC
8.BeanFactory接口和ApplicationContext接口的区别
(1)ApplicationContext借口继承BeanFactoy接口,Spring核心工厂是BeanFactory
BeanFactory采取延时加载,第一次getBean时才会初始化Bean
(2)ApplicationContext是在加载配置文件的时候初始化Bean,ApplicationContext是对
BeanFactory的扩展
9.Spring提供配置Bean的三种实例化方式
(1)使用构造器实例化
<bean id="bean1" class="com.guigu.hello.Bean1"></bean>
(2)使用静态工厂方法实例化
<bean id="bean2" class="com.guigu.hello.Bean2">
factory-method="getBean2"
</bean>
public static BeangetBean2(){
return new Bean();
}
其中:class表示的是类的全名称,factory-method表示的是静态方法
(3)使用实例工厂方法实例化
<bean id="beanfactory" class="com.guigu.hello.Bean3"></bean>
<bean id="bean3" factory-bean="beanfactory"></bean>
public Bean getBean3(){
return new Bean();
}
10.<bean>元素的id属性和name属性的区别
(1)通常指定id属性作为Bean的名称
(2)id属性必须是唯一的
(3)id属性的命名服从XML对ID属性的命名规范
(4)如果Bean的名称中含有特殊字符,就需要使用name属性
(5)name属性可以重复,后出现的Bean会覆盖之前出现的Bean
11.Bean元素的作用域
scope=“singleton”单例,在SpringIOC容器中仅存有一个Bean实例
scope=“prototype”多例,每次调用getBean()时,都获得一个新的实例
scope=“request” 用于web开发中,该作用域适用于WebApplicationContext环境当中
scope=“session” 用于web开发中,同样,适用仅适用于WebApplicationContext环境当中
scope=“globalSession”一般用于Porlet应用环境 ,仅适用于WebApplicationContext环境当中
12. Bean的初始化和销毁
<bean id="bean" class="com.guigu.hello.bean" init-method="setup" destory-method="teardown"></bean>
通过 init-method 指定Bean的初始化方法,通过 destroy-method 指定Bean销毁方法
destroy-method 只对 scope="singleton" 有效,销毁方法,必须关闭ApplicationContext对象
13.Bean 属性的依赖注入
(1)构造函数注入
<bean id="car1" class="com.guigu.hello.car1">
<constructor-agr index="0" value="保时捷"></constructor-agr>
</bean>
(2)属性setter方法注入
<bean id="car2" class="com.guigu.hello.car2">
<property name="name" value="保时捷"></property>
<property name="price" value="1000"></property>
</bean>
(3)复杂类型注入
<bean id="car2" class="com.guigu.hello.car2">
<property name="name" value="保时捷"></property>
<property name="car1" ref="car1"></property>
</bean>
<property name=""></property>
(4)p空间的使用
<bean id="person"class="com.guigu.hello.Person">
<property name="name" value="李四"></property>
<property name="car" ref="car2"></property>
</bean>
改写
<bean id="person" class="com.guigu.hello.Person" p:name="李四" p:car-ref="car2"></bean>
(5)EL表达式
1.完成对象的注入
<bean id="car"class="com.guigu.hello.car>
<property name="car2" ref="car2"></property>
</bean>
改写为
<bean id="car"class="com.guigu.hello.car>
<property name="car2" value="#{car}"></property>
</bean>
2.使用另外一个Bean属性完成注入
<bean id="carInfo" class="com.guigu.spring.e_di.CarInfo"></bean>
<bean id="car2_2" class="com.guigu.spring.e_di.Car2">
<property name="name" value="#{carInfo.name}"></property>
</bean>
3.使用另外一个Bean方法完成注入
<bean id="carInfo" class="com.guigu.spring.e_di.CarInfo"></bean>
<bean id="car2_2" class="com.guigu.spring.e_di.Car2">
<property name="price" value="#{carInfo.getPrice()}"></property>
</bean>
14.集合的注入
(1)List集合
<property name="hubbies">
<list>
<!-- <value>注入简单类型,<ref />注入复杂类型 -->
<value>爬山</value>
<value>游泳</value>
</list>
</property>
(2)set集合
<property name="num">
<set>
<!-- <value>注入简单类型,<ref />注入复杂类型 -->
<value>10</value>
<value>2</value>
</set>
</property>
(3)Map集合
<property name="map">
<!-- 复杂类型<entry key-ref="" value-ref=""></entry> -->
<entry key="name" value="guigu"></entry>
<entry key="name" value="guigu"></entry>
<entry key="adress" value="杭州"></entry>
</map>
</property>
15.XML文件的引入
(1)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans1.xml", "beans2.xml");
(2)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
在applicationContext.xml 中
<import resource="classpath:bean1.xml"/>
<import resource="classpath:bean2.xml"/>
16.注解形式进行开发 @Component
17.三个衍生注解
@Controller (表现层) @Services(业务层) @Repository(持久层)
18.属性注入
(1)简单属性注入@Value
@Service("UserService")
public class UserService{
@Value("张三")
private String name;
}
(2)复杂属性注入@Autowired
@Service("UserService")
public class UserService{
@Autowired("UserDao")
private UserDao userDao;
}
@Repository("UserDao")
public class UserDao{
}
(3)指定名称注入@Resource
@Resource(name="userDAO")
private UserDAO userDAO ;
19.Bean的初始化和销毁(注解)
@PostContruct 初始化
@PreDestroy 销毁
20.Bean 的作用范围
@Scope(value="prototype") //默认为singleton
21.是否直接可以在Servlet中加载Spring配置文件
每次请求都会加载Spring环境,初始化所有Bean ,产生性能问题!
22.AOP底层实现
AOP底层使用JDK动态代理和CGlib的动态代理
面向对象编程中,我们将事物纵向抽象成了一个个对象。而在面向切面编程中,我们将
对象的某些相似的方法横向抽成一个切面,对这个切面进行权限控制,事务管理,记录日志等操作。
23.代理模式
抽象角色:声明真实角色和代理角色的共同接口
代理角色:对真实对象进行封装
真实角色:我们最终要引用的对象
24.JDK动态代理只是针对于接口
25.常见概念
JoinPoint:连接点,指那些被拦截到的点,Spring实际中拦截到的是方法
Pointcut:切入点,指我们要对那些Joinpoint进行拦截,可以是一个或者一组
Advice:增强/通知,拦截到连接点后要做的事情
Target:目标对象,要代理的目标对象
Weaving:织入,指把增强应用到目标对象来创建新的代理对象的过程
Proxy:代理,一个类被AOP织入增强后,就产生了代理结果类
Aspect:切面,是切入点和通知的结合,一个切面可以由多个切点或者通知组成
26.AOP的五类通知
(1)前置通知:MethodBeforeAdvice
(2)后置通知:AfterReturningAdvice
(3)环绕通知:MethodInterceptor
(4)异常通知:ThrownAdvice
(5)引介通知
27.常用注解
@AspectJ常用注解
@Aspect 定义切面
@Before 前置通知:MethodBeforeAdvice
@AfterReturning后置增强:AfterReturningAdvice
@Around:环绕通知,MethodInterceptor
@AfterThrowing 异常通知,ThrowAdvice
28.Spring 对事物的管理
(1)编程式事物管理
(2)声明式事物管理
第一步:在需要管理事物类上或者方法上添加@Transactional
第二步:在ApplicationContext.xml中配置
补充
1.Spring中的模式:代理,工厂模式,模板模式
2.log4j日志级别:
致命错误(fatal)->普通错误(error)->警告(warn)->信息(info) ->调试(debug)-> 堆栈(trace)
3.Spring 可以帮我们做什么
(1)Spring可以根据配置文件创建及组装对象之间的依赖关系
(2)Spring面向切面编程可以帮助我们无耦合实现日志记录,事务管理,性能分析等操作
(3)集成第三方框架
4.BeanFactory的实现类
XmlBeanFactory、ApplicationContext
5.Spring配置文件有什么用
Spring配置文件是个XMl文件,包含了类信息,描述了如何配置他们,以及相互之间如何调用
6.IOC的优点,降低代码量,简化编程
7.ApplicationContext的实现类
ClasspathXMLApplicationContext、WebXmlApplicationContext
8. 什么是Spring Beans
Spring beans 是那些形成Spring应用的主干java对象,它们被Spring IOC容器初始化,装配和管理
9.Spring的内部Bean
当一个Bean仅仅被用作另一个bean的属性时,他就是一个内部bean
10.什么是bean的自动装配
无须在Spring配置文件中描述JavaBean之间的依赖关系,IOC容器自动建立javaBean之间的关联关系
11.自动装配
(1)no:默认的方式是不进行自动装配,通过显式设置 ref 属性来进行装配
(2)byName:通过参数名自动装配
(3)byType::通过参数类型自动装配
12.SpringAOP中,关注点和横切关注点的区别是什么
关注点是应用中一个模块的行为,一个关注点,可能会被定义成一个我们想要实现的功能。横向关注点是一个整个应用都会使用的功能,影响整个应用,比如日志等。