spring框架
1.IOC和DI
IOC:控制反转,将对象的控制权限交给了spring,对象的生命周期和依赖关系(DI)
DI是ioc的一部分,注册到IOC容器中的对象,他们之间的依赖关系由DI处理
spring配置文件
1.首先,创建一个Person类
注册对象
注册对象
id:为当前对象添加唯一标识,在外部通过该id可以获取到对象
非必须,如果不指定id值,将全限定类类名#数字作为id值
例如注册了三个person对象,不指定id
com.lanou.model.Person#0
class:类全限定名称,根据class结合反射机制来创建对象
init-method:指定初始化方法,当对象实例化完成,自动调用此方法
destroy-method:在对象销毁之前会自动调用用的方法
lazy-init:是否是懒加载,默认值是false;false在加载容器时,就创建该对象,如果是true在使用的时候创建
scope:对象的作用范围(对象的模式)
singleton(默认):单例
prototype:原型,每次getBean都会创建一个新对象
在web容器下(WebApplicationContext)
request
注册的对象默认情况下
1.加载IOC容器的时候就会创建该对象
2.单例
3.DI依赖注入
依赖注入:在注册对象时,为对象的成员变量赋值
1.属性注入(setter注入):通过set方法为对象的属性赋值
2.构造器注入:通过构造方法为属性赋值
3.自动装配:在IOC容器中找到合适的对象,赋值到指定对象的属性
byType:按照类型找
byName:按照名称找
byconstructor:通过构造方法来注入
1.setter注入
<!-- 简单类型的注入-->
<bean id="student" class="org.example.model.Student">
<!-- 属性注入
name:属性名(和set方法匹配)
例如 id 但是set方法中是setPersonId(){this.id = id} 这个时候name中的值写的是personId
value:要赋的值
-->
<property name="Id" value="1"></property>
</bean>
<!-- 为集合类型的属性赋值-->
<bean name="student1" class="org.example.model.Student">
<property name="array">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
<property name="list">
<list>
<value>a</value>
<value>b</value>
<value>c</value>
</list>
</property>
<property name="map">
<map>
<entry key="name" value="张三">
</entry>
<entry key="age" value="19"></entry>
<entry key="性别">
<value>男</value>
</entry>
</map>
</property>
</bean>
<bean id="person" class="org.example.model.Person"></bean>
<!-- 复杂对象的注入-->
<bean name="student2" class="org.example.model.Student">
<!-- 当前容器中不存在没有要注入的对象-->
<!-- <property name="person">-->
<!-- <bean class="org.example.model.Person"></bean>-->
<!-- </property>-->
<!-- 当前容器中有可使用的对象
ref:按照名称引用当前容器中已注册的对象
-->
<property name="person" ref="person"></property>
</bean>
2.构造器注入:通过构造方法进行注入,对应的构造方法必须存在
<bean id="student" class="org.example.model.Student">
<!-- 按照自上而下的顺序将值赋给构造方法对应位置的参数-->
<constructor-arg>
<value>1</value>
</constructor-arg>
<constructor-arg value="张三"></constructor-arg>
<!-- 按照指定名称去找对应的参数-->
<constructor-arg name="id">
<value>1</value>
</constructor-arg>
<constructor-arg name="name" value="张三"></constructor-arg>
<!-- 指定参数位置(用的不多)-->
<constructor-arg index="0" value="1" type="java.lang.Integer"></constructor-arg>
<constructor-arg index="1" value="2" type="java.lang.String"></constructor-arg>
<constructor-arg name="person">
<bean class="org.example.model.Person"></bean>
</constructor-arg>
<constructor-arg name="person" ref="person"></constructor-arg>
<!-- 注入集合类型-->
<constructor-arg name="List">
<list>
</list>
</constructor-arg>
</bean>
3.自动装配
自动装配:从目前ioc容器已有的去找合适的对象注入
byType:按照类型从IOC容器中找
1.没找到,不注入
2.只有一个该对象对象,将该对象注入
3.有多个同类型的,会注入失败,抛出异常
buName:按照名称从IOC容器中找,bean的id值和属性名一样,就会将该bean注入
只关注名称,不关注类型;万一类型不匹配,也会注入失败,抛出异常
constructor:通过构造器并注入
按照bytype的方式从IOC容器中找和构造器参数类型一样的对象注入
1.没找到,不注入了,通过无参的构造方法创建对象即可
2.找到一个,直接注入
3.找到多个,按照byName的方式匹配一次,将id值和参数名一样的bean注入进去;当byName匹配不到的话,注入失败,抛出异常
<bean id="student" class="org.example.model.Student" autowire="constructor">
<bean id="student" class="org.example.model.Student" autowire="byName">
<bean id="student" class="org.example.model.Student" autowire="byType">
spring常见注解
使用注解时需要配置配置文件
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--
base-package:要扫描的包,只有指定包下的spring相关注解才会生效
扫描指定的包以及子孙包下的注解;可以指定多个包,以逗号隔开即可
-->
<!-- <context:component-scan base-package="com.lanou,com.aa"></context:component-scan>-->
<context:component-scan base-package="com.lanou"></context:component-scan>
</beans>
@Component、@Controller、@Service、@Repository
@Component修饰类,spring扫描到该注解时,就会将当前类的对象注册到IOC容器中 ;默认会将首字母小写的类名做为id值
value:为注册对象命名
如果注解中只有一个value属性,属性名可以省略
如果注解的某个属性的类型为数组,形式为{},如果该数组中只有一个元素,{}可以省略
例如@Insert(value={“aa”}) 等效于 @Insert(“aa”)@Controller、@Service、@Repository是@Component的子注解,和@Component作用一样
只是为了做到见名知意,分别作用于web层对象、业务层对象、持久层对象
依赖注入相关注解
@Autowired修饰属性时,直接为属性注入值,不是通过setter方法;byType的方式
@Autowired(required = true):required:是否必须;true(默认值):必须注入;false:有合适的注入,没合适的就不注入了
@Qualifier(“name”):以byName的方式进行自动装配
@Configuration和@Bean
@Configuration是一个注解,用于标识一个类是一个配置类
指定要扫描的包,扫描指定包下的spring相关注解,和context:component-scan作用相同,但是context:component-scan必须要指定包
@ComponentScan(basePackages = {“com.lanou”})
@Bean注解用于定义一个Bean对象
获取bean
当获取配置类对象时,ClassPathXmlApplicationContext获取bean