spring的IOC

spring IOC(DI)总结

spring基于配置文件管理bean

1、创建spring配置文件,对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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 
		<bean>:定义spring所管理的一个对象
		id:该对象的唯一标示,注意:不能重复,在通过类型获取bean的过程中可以不设置
		class:此对象所属类的全限定名
	 -->
	<bean id="beanId" class="类的全限定名">
		<!-- 
			<property>:为对象的某个属性赋值
			name:属性名
			value:属性值
		 -->
		<property name="id" value="10010"></property>
		<property name="name" value="小明"></property>
	</bean>
	<bean id="beanId" class="类的全限定名">
		<!-- 
			<constructor-arg>标签表示通过构造函数为bean赋值,
			如果存在多个参数相同的构造函数,可以通过index和type为指定位置的参数指定类型
		 -->
		<constructor-arg value="10086"></constructor-arg>
		<constructor-arg value="张三"></constructor-arg>
		<constructor-arg value="19" index="2" type="java.lang.Integer"></constructor-arg>
		<constructor-arg value=""></constructor-arg>

	</bean>
	<!-- 引入P命名空间,通过p:属性=值的方式为属性赋值 -->
	<bean id="beanID" class="类型全限定名" p:id="10033" p:name="李四" p:age="26" p:sex="" p:teacher-ref="teacher"></bean>

</beans>

通过上面例举大概的注入(DI)方式:
(1)、bean中通过<property>标签赋值,表示为set方式注入
(2)、bean中通过<constructor-arg>标签辅赋值,表示为构造器方式注入
(3)、也可以通过引入p命名空间的方式通过p:属性=值得方式为bean注入

2、bean的生命周期

<!-- 
	1、创建bean:由反射通过构造器或工厂方法创建
	2、注入和引用:通过constructor-arg或是property进行注入
	3、初始化:调用bean的初始化,根据bean的作用域不同,初始化方式不同,单例跟随着容器一起初始化
	4、使用:通过getBean获得bean并使用bean
	5、销毁: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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="beanId" class="类型全限定名" init-method="init" destroy-method="destory">
		<property name="id" value="1001"></property>
		<property name="sex" value=""></property>
	</bean>
	

</beans>

在bean的生命周期中可以通过bean标签的init-method属性为bean指定初始化方法,同时可以通过destroy-method属性为bean指定销毁方法。可以通过一个普通类继承BeanPostProcessor接口来为spring设置后置处理器。后置处理器作用初始化前后,加上后置处理器,bean的生命周期就由五步变成了起步:
1、创建bean
2、注入:赋值或引用
3、后置处理器处理前
4、初始化:获取容器时初始化,根据bean的作用域不同有所差异
5、后置处理器处理后
6、使用bean
7、销毁bean:一般指关闭spring容器

3、基于配置文件,bean的自动注入方式
在bean的标签中通过autowire="注入方式"指定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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 
		autowire:根据某种策略自动为非字面量属性赋值
		autowire="byName|byType"
		byName:通过属性名和spring容器中bean的id进行比较,若一致则可直接赋值
		byType:通过spring容器中bean的类型,为兼容性的属性赋值
			   在使用byType的过程中,要求spring容器中只能有一个能为属性赋值的bean
		选用建议: 当设置autowire属性,会作用于该bean中所有的非字面量属性,因此谁都不用
	 -->
	
	<bean id="beanId" class="类型全限定名" autowire="byName">
		<property name="eid" value="1001"></property>
		<property name="ename" value="张三"></property>
	</bean>
	
	<bean id="beanId" class="类型全限定名">
		<property name="cid" value="666666"></property>
		<property name="cname" value="霸道"></property>
	</bean>
	
	<bean id="beanId" class="类型全限定名">
		<property name="cid" value="8888888"></property>
		<property name="cname" value="奥迪A8"></property>
	</bean>
	
	<bean id="beanId" class="类型全限定名">
		<property name="did" value="11111"></property>
		<property name="dname" value="开发部"></property>
	</bean>

</beans>

在bean的注入方式中,常用的方式有两种:
1、byName:通过名字注入,即bean引用的beanId名和引用bean的属性名一致时完成自动注入
2、byType:通过类型注入,即通过引用bean的类型或是子类型满足时完成自动注入
注意:要求bean中有且仅有一个bean能作为赋值的bean

强调:自动注入只能是非字面量的注入

以上两种自动注入方式均存在一定的弊端,所以建议选用注解的方式,注解方式后面讲。

4、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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- 以下两种方式效果一样,通过context:property-placeholder标签的方式引用看起来更简洁,其实最终它也是通过解析为bean的方式进行处理的 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="引用文件路径名称"></property>
	</bean>
	<context:property-placeholder location="引用文件路径名称"/>

</beans>

5、spring的作用域 singleton | prototype | request | session
此处主要讲两个:singleton | prototype

<?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 id="beanId" class="类型全限定名" scope="singleton">
		<property name="id" value="10010"></property>
		<property name="sex" value=""></property>
	</bean>
	<bean id="beanId" class="类型全限定名" scope="prototype">
		<property name="id" value="10020"></property>
		<property name="sex" value=""></property>
	</bean>

</beans>

singleton:单独的,设置为单例模式,spring默认为单例模式。跟随着spring容器一起初始化。
prototype:原型,将bean设置为多例的,每次获取都会生成新的bean,每次获取时初始化并返回。
request:请求,每次请求重新生成
session:会话,每次会话重新生成

spring基于注解方式管理bean

spring基于注解管理bean两步走

一、将需要管理的bean添加注解

注解: @Component 组件
@Controller 控制
@Service 服务
@Respository 存储

通过添加上面四个注解中的一个来告诉spring,添加了以上四个中其中一个注解的类需要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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 
		<context:component-scan>:扫描组件:通过base-package属性设置需要扫描的包,会将加上注解的类当作组件进行加载
		组件:指spring中管理的bean
		作为spring的组件进行加载:会自动在spring的配置文件中生成相对应的bean,这些bean的id会以类名首字母小写为值
		<context:include-filter>:在指定的包结构下,再次通过注解或类型包含到某个或某几个类
		注意:使用包含时一定要将<context:component-scan>中的use-default-filters设置为false
		<context:exclude-filter>:在指定的包结构下,再次通过注解或类型排除到某个或某几个类
		注意:使用排除时一定要将<context:component-scan>中的use-default-filters设置为true
		重点:在一个<context:component-scan>中可以同时出现多个包含或多个排除,但是包含和排除不能同时存在。
		
	 -->
	<context:component-scan base-package="包名" use-default-filters="true">
		<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
		<!-- <context:include-filter type="assignable" expression="com.atguigu.ioc.userMod.service.UserServiceImpl"/> -->
		<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> -->
		<!-- <context:exclude-filter type="assignable" expression="com.atguigu.ioc.userMod.dao.UserDaoImpl"/> -->
	</context:component-scan>
	
</beans>

在使用注解时,我们不能看到生成的bean,此时我们需要通过注解的方式完成自动注入

public class AutowiredTest {
	@Autowired
	private UserService userService;
	public void show() {
		System.out.println("show()");
	}
}

@Autowired装配时,会默认使用byType的方式,此时要求spring容器中只有一个能够为其赋值 当byType实现不了装配时,会自动切换到byName,此时要求spring容器中,有一个bean的id和属性名一致 若自动装配时,匹配到多个能够复制的bean,可使用@Qualifier(value=“beanId”)指定使用的bean @Autowired和@Qualifier(value=“beanId”)可以一起作用域一个带形参的方法上,此时,@Qualifier(value=“beanId”)所指定的bean作用于形参

不论是通过配置文件的方式还是通过注解的方式使用spring管理bean对象,我们都需要通过ApplicationContext接口及其实现类的方式完成spring容器的初始化。

public class TestApplicationContext {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		className bean = ac.getBean("beanId", className.class);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值