1.Spring是什么?有什么好处?
- 概念: SPring是一个支持控制反转(IOC)和面向切面编程(AOP)的容器框架。
- 好处: 两降低>>>两支持>>>两方便
- ①降低了耦合性,提高了开发速度。
- ②降低了JAVAEE API的使用难度。
- ③支持AOP和IOC。
- ④支持声明式事务。
- ⑤方便程序测试。
- ⑥方便集成其他框架。、
2.IOC是什么?有什么好处?简单过程?
- IOC: 是Inverse of Control(控制反转)的简写。
- 好处: 通过IOC,直接把对象创建的权力反转给Spring容器,降低了对象之间的耦合性。
- 简单过程: 程序读取Spring的XML配置文>>>获取需要创建对象的bean>>>通过反射机制创建对象的实例。
3.DI是什么?
- DI: Dependency Injection(依赖注入)的简写。
- 创建对象实例时,同时为对象注入它所依赖的属性,相当于把每个bean和bean之间的关系交给Spring容器来管理。
4.IOC和DI的关系?
- 控制反转(IOC)和依赖注入(DI)是从不同角度描述同一件事情,利用依赖关系注入的方式,实现对像之间的解耦。
- 耦合性(耦合度):是对模块间关联程度的度量。模块之间联系越紧密,其耦合性就越高,模块之间越独立则越低。
5.bean标签的属性有哪些?
- ① id (唯一标识)
- ② name(获取bean的键)
- ③ class(指定bean对应类的全路径)
- ④ scope(单例或者多例设计模式)
- ⑤ lazy-init(延时加载,默认值:false):设置false时,只有调用getBean方法才会创建对象
- ⑥ init-method(指定:监听对象创建的方法)
- ⑦ destroy-method(指定:监听对象销毁的方法)
6.IOC创建对象有哪几种方式?
- ①无参构造
- ②有参构造
- ③静态工厂模式(1个bean标签)
- ④非静态工厂模式(2个bean标签)
//1.无参构造
<bean id="user" class="com.wpq.pojo.User"></bean>
//在bean标签内部使用property标签,相当于调用set方法. name:要赋值的属性的属性名 value:值
<bean id="user" class="com.wpq.pojo.User">
<property name="name" value="zs"></property>
<property name="password" value="123"></property>
</bean>
//2.有参构造
<bean id="user" class="com.wpq.pojo.User">
<constructor-arg index="0" type="java.lang.String" name="name" value="张三"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" name="password" value="123"></constructor-arg>
</bean>
//3.静态工厂模式--createPerson()为静态方法
<bean name="personFactory" class="com.wpq.factory.PersonFactory" factory-method="createPerson"/>
//4.工厂模式
<bean name="personFactory" class="com.wpq.factory.PersonFactory"/>
<bean name="person" factory-bean="personFactory" factory-method="instancePerson"/>
7.Spring是如何实现IOC的?也就是如何创建对象的?
<!--0.对象创建原理:xml解析+反射-->
<!--1.ClassPathXmlApplicationContext根据xml的路径和名称加载xml;-->
<!--2.对该xml文件进行解析-->
<!--3.根据class属性,获取class属性的值:com.wpq.domain.Person-->
<!--4.反射:获取字节码的方式,Class clazz=Class.forName("全路径");p.getClass();Person.class-->
<!--5.根据字节码创建对象:Person p=clazz.newInstance()-->
<!--6.给对象里的属性赋值:Fields[] fields=clazz.getDeclaredFields();-->
<!--7.遍历属性数组:for(Field field : fields){ field.setAccessable(true);field.set(30)}-->
<bean id="person" class="com.wpq.domain.Person">
<property name="name" value="zs"/>
<property name="age" value="30"/>
</bean>
8.Spring Bean的生命周期?
- ①实例化 Instantiation
- ②属性赋值 Populate
- ③初始化 Initialization
- ④销毁 Destruction
9.依赖注入DI的方式有几种?
<bean id="user" class="com.wpq.pojo.User">
<property name="name" value="zs"></property>
<property name="password" value="123"></property>
</bean>
<bean id="user" class="com.wpq.pojo.User">
<constructor-arg index="0" type="java.lang.String" name="name" value="张三"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" name="password" value="123"></constructor-arg>
</bean>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="car" class="com.wpq.domain.Car" p:logo="马车" p:color="黑色"/>
<bean name="person" class="com.wpq.domain.Person" p:name="阮小二" p:age="40" p:car-ref="car"/>
</beans>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="car" class="com.syc.spring.domain.Car">
<property name="logo" value="劳斯莱斯"/>
<property name="color" value="黑色"/>
</bean>
<bean name="person" class="com.wpq.domain.Person">
<property name="name" value="#{car.logo}"/>
</bean>
</beans>
package com.wpq.domain;
import java.util.*;
public class CollectionBean {
private Object[] arr;
private List list;
private Map map;
private Set set;
private Properties props;
public Object[] getArr() {
return arr;
}
public void setArr(Object[] arr) {
this.arr = arr;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
@Override
public String toString() {
return "CollectionBean{" +
"arr=" + Arrays.toString(arr) +
", list=" + list +
", map=" + map +
", set=" + set +
", props=" + props +
'}';
}
}
<?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.xsd">
<bean name="cb2" class="com.wpq.domain.CollectionBean">
<property name="arr">
<array>
<value>李师师</value>
<value>柳如是</value>
<value>苍老师</value>
</array>
</property>
</bean>
<bean name="cb3" class="com.wpq.domain.CollectionBean">
<property name="list">
<list>
<value>大乔</value>
<value>小乔</value>
<value>金莲</value>
</list>
</property>
</bean>
<bean name="cb4" class="com.wpq.domain.CollectionBean">
<property name="map">
<map>
<entry key="name" value="三胖"/>
<entry key="age" value="30"/>
<entry key="job" value="boss"/>
</map>
</property>
</bean>
<bean name="cb5" class="com.wpq.domain.CollectionBean">
<property name="set">
<set>
<value>大乔</value>
<value>小乔</value>
<value>金莲</value>
</set>
</property>
</bean>
<bean name="cb6" class="com.wpq.domain.CollectionBean">
<property name="props">
<props>
<prop key="url">jdbc:mysql://localhost:3306/db01</prop>
<prop key="driver">com.jdbc.mysql.Driver</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
</beans>
10.注解实现IOC和DI的准备工作有哪些?
- ① 在XML文件中引入Context的约束
- ② 配置组件扫描器
- ③使用注解
<?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:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.wpq.domain,com.wpq.web,com.wpq.service,com.wpq.dao"/>
</beans>
11.有哪些注解?分别表示什么含义?
- ①注解实现IOC
- @Component:组件注解,用来创建一个对象,等同于在xml中写了bean标签。
- ②注解实现DI
- @Value("…"): 只能给简单类型注入值,不能给引用类型注入值,使用在成员变量上或set方法上 (简单类型=String+8种基本类型)
- 注意:该注解可以引入配置文件中的变量。 语法: @Value("${age}")
实现步骤: 1. 创建conf.properties配置文件(age=11,name=wpq)
2. XML中配置property-placeholder加载配置文件
<context:property-placeholder location="classpath:conf.properties"/>
- @Autowired: 自动装载对象,默认情况下是按照类型来进行匹配。
- @Qualifier: 该注解一般要结合@Autowired的一起使用,当@Autowired根据类型无法匹配对象的时候,进行辅助,根据名称进行依赖注入.解决无法根据类型进行唯一性对象匹配的问题.
- @Resource: 等同于@Autowired+@Qualifier,该注解是Java原生的注解,既可以根据类型,又可以根据名称进行依赖注入.
- ③ Bean标签的属性对应的注解
- 作用域: @Scope(scopeName=“单例/多例”)
- 延迟加载:@Lazy: 等同于中的lazy-init属性 ,设置是否延迟加载
- 创建对象监听:@PostConstruct 指定监听对象创建的方法
- 销毁对象监听:@PreDestroy 指定监听对象销毁的方法
- ④ 组件注解
- @Component:组件注解
- @Controller:组件注解,一般用于web层对象的创建
- @Service:组件注解,一般用于service层对象的创建
- @Repository:组件注解,一般用于dao层对象的创建
- ⑤ 测试注解
- @RunWith(SpringJUnit4ClassRunner.class) :括号内指定完成测试工作的类
- @ContextConfiguration(“classpath:appication-Collection.xml”) : 指定要加载的XML配置文件
- @Test :写在测试方法上
- ⑥ 元注解
- @Target(ElementType.FIELD):定义注解的作用范围
- @Retention(RetentionPolicy.RUNTIME):定义注解的生命周期(保留策略)
- 自定义注解:必须带上面两个元注解
12.谈谈你对Spring AOP的理解?
- ① 概念:是Aspect Oriented Programming的简写,翻译过来就是面向切面编程。
- ② 核心思想:AOP把系统分为核心关注点和横切关注点两个部分,将应用程序中的业务逻辑同为其提供支持的通用服务进行分离。
- 核心关注点:就是业务处理的主要流程(纵向的业务逻辑处理)
- 横切关注点:就是出现在每个业务逻辑处理模块中的大量重复代码,比如说权限认证,日志,事务处理。
- ③ AOP解决的问题:避免了出现大量的重复性代码,提高了代码的复用性。
- ④ AOP底层使用的两种机制:JDK的动态代理和Java类库的CGLIB代理。
- 如果我们类实现了接口,Spring底层实现AOP就会调用动态代理,否则就调用CGLIB代理。
13.XML方式实现AOP的通知有几种?
- ① 前置通知 before
- ② 环绕通知 around
- ③ 后置通知 after-Returning
- ④ 异常通知 after-Throwing
- ⑤ 最终通知 after
14.注解实现AOP的过程?
1.配置Spring XML文件
开启自动代理 <aop:aspectj-autoproxy/> :声明自动为spring容器中那些配置@Aspect切面的bean创建代理,织入切面
开启组件扫描 <context:component-scan base-package="com.wpq.Spring"/>
2.创建切面类:给类上面添加@Aspect注解
3.切面类中配置切入点 :@Pointcut(value = "execution(* com.wpq.service.impl.*.*(..))")
public void pointCut() {}
4.在切面类不同的方法中添加注解:
前置:@Before(value=“pointCut()”)
环绕: @Around(value=“pointCut()”)
后置: @AfterReturning(value=“pointCut()”)
异常: @AfterThrowing(value=“pointCut()”)
最终: @After(value=“pointCut()”)
15.更改多个切面类的执行顺序的方法有几种?
- ① 默认按照类的首字母来执行,a-z/A-Z
- ② 给切面类添加 @Order(v) 注解,v越小,优先级越高
- ③ 切面类实现Order接口,重写getOrder()方法
16.Spring有哪些主要模块?
- Spring框架至今已经集成了20多个模块。主要是核心容器、数据访问/集成、Web、AOP、工具、消息和测试模块。
17.Spring中的bean是线程安全的吗?
- Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策略,因此可以说spring容器中的Bean本身不具备线程安全的特性,但是还是要结合具体的scope的Bean去研究。
18.Spring支持几种bean的作用域?