一、什么是Spring
spring是个容器,用于管理bean(实体类)与bean之间的关系。
spring的核心是ioc(控制反转)、di(依赖注入)、aop(面向切面编程)。spring可以整合许多框架,比如:redis、mq、mongodb等。百度搜索关键词可以输入“spring集成redis”。
二、项目中什么时候用单例?会发生什么问题?
spring默认是单例的。会发生线程不安全问题,尽量避免共享一个变量。
三、spring中 有四种生命周期
Prototype 多例 当你调用getbean方法才会初始化
singleton 单例
request http请求request作用里面使用
session http请求session作用里面使用
四、spring IOC和DI的区别
spring ioc是控制创建对象,由传统的new方法交给容器进行控制对象创建;
di是依赖注入,指spring创建对象过程中,将对象依赖属性通过配置进行注入。
spring ioc创建对象的方式有三种,参考链接:https://github.com/WangShuaiFa/practice-SpringIoc-and-SpringDi.git分别为:
- 构造函数(无参和有参)
- 工厂模式(静态方法和非静态方法)、
- 注解。
其中注解的方式在spring boot和spring cloud中使用的比较多。
@Component 指定把一个对象加入IOC容器
@Repository 作用同@Component; 在持久层使用
@Service 作用同@Component; 在业务逻辑层使用
@Controller 作用同@Component; 在控制层使用
@Resource 属性注入
注意:@Resource和@autowried的区别
1.@Resource是JDK1.6以上版本自带,@autowried是spring提供的注解;
2.@Resource默认根据名称搜索,@autowried默认根据类型搜索
DI注入的方式有三种,参考链接:https://github.com/WangShuaiFa/practice-SpringIoc-and-SpringDi.git分别为:
- set方法
- 构造方法,参考ioc创建方式
- p方法
五、spring aop
aop就是面向切面编程
举个简单例子:歌星都有好多助理,歌星最重要的一件事就是唱歌,其他事他不用关注,比如唱歌前可能需要和其他人谈合作,还要布置场地,唱歌后还要收钱等等,这些统统交给他对应的助理去做。也许哪一天,这个歌星做慈善,免费唱歌了,不收钱了,那么就可以把收钱这个助力给辞退了。这就是 AOP,每个人各司其职,灵活组合,达到一种可配置的、可插拔的程序结构。AOP 的实现原理就是代理模式。代理模式分为静态代理和动态代理(jdk动态代理和cglib动态代理),jdk代理的底层原理是反射机制,cgLib代理的底层原理是asm框架。静态代理和动态代理的区别:代理对象(例子中的zhognjie),要实现与目标对象一样的接口(house对象中的infromation接口),参考代码:https://github.com/WangShuaiFa/pritise-proxy.git
在Spring的AOP编程中,
如果加入容器的目标对象有实现接口,用JDK代理;
如果目标对象没有实现接口,用Cglib代理
aop的作用就是把代码分离。重复代码叫关注点。
六、实现AOP编程的两个方式
1.注解方式
使用aop设置前置通知时,出现如下错误。根据错误提示是由于底层cglib的错误,于是查找maven中央仓库,更换稳定版本的cglib,问题解决。
2.xml方式
spring-005.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启注解权限,此注解权限不是针对aop切入的方式,比如@service的需要 -->
<context:component-scan base-package="SpringAop"></context:component-scan>
<bean class="SpringAop.UserDao">
<property name="flag" value="true"></property>
</bean>
<!--spring aop 使用xml方式-->
<bean id="aop" class="SpringAop.xmlService.XmlAop"></bean>
<aop:config>
<!--定义切入点表达式,定义拦截哪些方法-->
<aop:pointcut id="pt" expression="execution(* SpringAop.xmlService.XmlUserService.add(..))" />
<!--切面-->
<aop:aspect ref="aop">
<aop:before method="begin" pointcut-ref="pt"></aop:before>
<aop:after method="commit" pointcut-ref="pt"></aop:after>
<aop:after-returning method="afterReturn" pointcut-ref="pt"></aop:after-returning>
<aop:after-throwing method="afterThrowing" pointcut-ref="pt"></aop:after-throwing>
</aop:aspect>
</aop:config>
</beans>
https://github.com/WangShuaiFa/practice-SpringIoc-and-SpringDi.git