一:IOC
IOC(Inversion of Control) ,反转资源控制.容器主动地将资源推送到它所管理的组件,组件所需要做的仅仅是选择一种合适的方式来接受资源,翻转指的是获取对象的方式。
将创建对象,属性值的方式进行了翻转,从new,setXX()翻转成从IOC容器getbean。
二:DI
组件以一些预先定义好的方式接受来自容器的资源注入。
将属性值注入给了属性,将属性注入bean,将bean注入给了IOC容器。
三:BeanFactory、ApplicationContext
Spring提供了两种类型的ioc实现,分别是BeanFactory和ApplicationContext。
- BeanFactory
IOC容器的基本实现,是Spring框架的基础设施,面向Spring本身。
- ApplicationContext
ApplicationContext提供了更多的高级特性,是BeanFactory的子接口。面向使用Spring框架的开发者,几乎所有应用场合都使用ApplicationContext,而不是BeanFactory。
//1.创建Spring的IOC容器对象
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContent.xml");
//2.从IOC获取bean实例(通过id),要求ioc容器必须有一个该类型的bean
HelloWorld helloWorld= (HelloWorld) ctx.getBean("helloWorld");
//3.调用hello方法
helloWorld.hello();
四:bean依赖注入的配置方式
属性注入即通过setter方法注入bean的属性值或者依赖的对象,分为属性注入和构造器注入。
- XML文件
1.set方法
<!--
class:bean的全类名,通过反射在IOC容器创建bean,所以要求bean必须有无参构造器,此选项可以忽略,但是必须设置abstract="true",设置为抽象bean.
id:bean的唯一标识符,若id没有指定,spring自动将权限定性类名作为bean的名字,id可以指定多个名字,名字之间可用逗号,分号,或者空格隔开。
-->
<bean id="helloWorld" class="com.spring.beans.HelloWorld">
<!--
使用property 标签来属性注入
name:指定bean的属性名称
value:指定属性的属性值
ref :引用其他对象
-->
<property name="name" value="Spring"></property>
<property name="name" >
<value>Spring</value>
</property>
<property name="car" ref="car"></property>
<!--级联属性赋值,注意:属性需要先初始化才可以为级联属性赋值,否则会有异常-->
<property name="car.price" value="260"></property>
<!--使用list节点为LIST属性赋值-->
<property name="cars">
<list>
<ref bean="car" />
<bean></bean>
<!-- 基本数据类型-->
<value></value>
</list>
</property>
<!--使用map节点及其map的entry子节点为属性赋值-->
<property name="cars">
<map>
<entry key="AA" value-ref="car"></entry>
<entry key="BB" value-ref="car"></entry>
<!-- 基本数据类型-->
<entry key="abc" value="123"></entry>
</map>
</property>
<!--配置properties属性值-->
<property name="properties">
<!--使用props和prop子节点来为properties属性赋值-->
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="jdbcUrl">jdbc:mysql</prop>
<prop key="driveClass">com.mysql.jdbc</prop>
</props>
</property>
</bean>
<!--使用p标签,需要先引用p的命名空间,相对于传统的配置方式更加简洁-->
<bean id="car" class="com.spring.Car" p:brand="Audi" p:price="300000">
</bean>
使用子元素注入 | 使用value属性注入 | |
---|---|---|
参数值的位置 | 写在首尾标签value标签值之间,不必加引号 | 写在value的属性值中,必须加引号 |
type | 有,可选 | 无 |
特殊值处理 | xml引用实体或者CDATA | xml引用实体 |
2.构造方法
<bean id="car" class="com.spring.beans.Car">
<!--
value:构造参数的属性值,如果字面值包含特殊字符,可以使用<![CDATA[]]>包裹起来
index:构造参数的位置
type :构造参数的类型
name:参数的名字
-->
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="Shanghai" index="1"></constructor-arg>
<constructor-arg value="10" index="2"></constructor-arg>
<constructor-arg value="20" index="3"></constructor-arg>
<!-- null值专有标志-->
<constructor-arg><null/></constructor-arg>
</bean>
- 注解
1.基于注解的方式配置bean
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。
默认的命名策略:使用非限定类名,第一个字母小写,也可以在注解中通过 value属性值标识组件的名称。
组件名称 | 组件说明 |
---|---|
@Component | 基本注解,标识一个受spring管理的组件 |
@Responitory | 标识持久层组件 |
@Service | 标识服务层(业务层) 组件 |
@Controller | 标识表现层组件 |
<context:component-scan base-package="com.spring.annotation" >
<!-- 排除哪些指定表达式的组件-->
<!--<context:exclude-filter type="annotation" expression=""/>-->
<!-- 包含指定的表达式组件,与use-default-filters="false"配合使用 -->
<!--<context:include-filter type="annotation" expression=""/>-->
</context:component-scan>
2.基于注解的方式来装配bean的属性
加autowire注解
@Autowired
private TestObject testObject;
几点说明
1.若某一属性不被设置,可以设置@Autowire注解的required的属性为false
2.spring允许对方法的入参标注为@Qualifiter已指定注入的bean名称
- FactoryBean
1.自定义的FactoryBean需要实现FactoryBean接口
2.实现三个方法
//返回bean的对象
getObject()
//返回bean的类型
getObjectType()
//判断bean是不是单例
isSingleton()
3.配置xml文件
<!--
class:指向FactoryBean的全类名
property:配置FactoryBean的属性,但实际返回的实例却是FactoryBean的getObject()返回方法的实例
-->
<bean id="car" class="com.spring.factorybean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
五:自动装配(autowire)
1.类型
- byType
根据类型自动装配,若IOC容器中有多个与目标Bean类型一致的bean,在这种情况下,Spring将无法判断哪个bean最适合该属性,所以不能执行自动装配。 - byName
根据名称(xml标签的id)自动装配,必须与目标bean的名称和属性名设置的完全相同。
<bean id="address" class="com.spring.autowire.Address" p:city="Beijing" p:street="HuiLong"></bean>
<bean id="car" class="com.spring.autowire.Car" p:brand="Audi" p:price="300000"></bean>
<!--通过名字,自动装配-->
<bean id="person" class="com.spring.autowire.Person" p:name="Tom" autowire="byName"></bean>
- construcor
通过构造器自动装配,当bean中存在多个构造器时,此时自动装配会很复杂,不推荐使用。
2.缺点
- 在bean配置文件里设置autowire属性将自动装配Bean的所有属性,然而,只希望装配个别属性的时候,显得不够灵活。
- autowire属性要么根据类型自动装配,要么根据名称自动装配,不能兼而有之
一般情况下,在实际项目很少使用自动装配功能,因而与自动装配所带来的好处相比,明确清晰的配置文档更具有说服力。
六:bean之间的关系
- 继承
<bean id="address" class="com.spring.autowire.Address" p:city="Beijing" p:street="WuDaoKou" ></bean>
<bean id="address2" class="com.spring.autowire.Address" parent="address"></bean>
1.spring允许继承bean的配置,被继承的bean称为父bean,继承这个父bean的bean称之为子bean
2.子bean可以覆盖从父bean继承过来的配置
3.autowire,abstract的属性将不会被继承
4.父bean可以作为配置模板,也可以作为bean实例,若只想把父bean作为模板,可以设置bean的abstract为true,这样spring将不会初始化这个bean.
5.也可以忽略父bean的class属性,让子bean指定自己的类,而共享相同的属性配置,此时abstract必须设置为true.
- 依赖
<!--depends-on指定特定的bean-->
<bean id="address" class="com.spring.autowire.Address" p:city="Beijing" p:street="WuDaoKou" depends-on="car"></bean>`
1.spring允许用户通过depends-on属性设定bean前置依赖的bean,前置依赖好的bean会在本bean实例之前创建好。
2.如果前置有多个bean,则可以通过逗号,空格的方式来配置bean的名称。
七:bean的作用域
<!--
使用bean的scope属性来配置bean的作用域
singleton:默认值,容器初始值创建bean实例,在整个容器的生命周期只创建这一个bean,单例
prototype:原型的,容器初始化时不创建bean的实例,而每次请求都创建一个新的bean实例,并返回
-->
<bean id="car" class="com.spring.autowire.Car" >
<property name="brand" value="Audi"></property>
<property name="price" value="123"></property>
</bean>
八:bean的生命周期
- 通过构造器或者工厂方法创建bean实例
- 为bean的属性设置值和对其他bean的引用
- 将bean实例传递给bean的后置处理器postProcessBeforeInitialization
- 调用bean的初始化方法
init-method=“init” - 将bean实例传递给bean的后置处理器postProcessAfterInitialization
- bean可以使用
- 当容器关闭调用destroy-method方法
destroy-method=“destory”
<bean id="car" class="com.spring.cycle.Car" init-method="init" destroy-method="destory">
<property name="brand" value="Audi"></property>
</bean>
来源:CSDN
作者:Not Talk
链接:https://blog.csdn.net/qq_39109805/article/details/103851752