Spring概述与 IOC (inverse of control)控制反转

Spring框架概述

1.轻量级的开源的javaee框架
2.spring可以解决企业开发的复杂性
3、Spring 有两个核心部分:IOC 和 Aop
(1)IOC:控制反转,把创建对象过程交给 Spring 进行管理
(2)Aop:面向切面,不修改源代码进行功能增强

maven依赖

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

IOC

编写spring xml文件

在这里插入图片描述

什么是IOC

控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理

IOC底层原理

xml 解析、工厂模式、反射
框架 =  设计模式 +  反射 + 标识

IOC过程

配置xml文件,然后写User类,然后调用
        ApplicationContext context = new ClassPathXmlApplicationContext("src/main/resources/bean.xml");

        //2.获取配置创建的对象
        User user = context.getBean("user", User.class);

        System.out.println(user);	

IOC bean管理

1、什么是 Bean 管理
(0)Bean 管理指的是两个操作
(1)Spring 创建对象
(2)Spirng 注入属性

2、Bean 管理操作有两种方式
(1)基于 xml 配置文件方式实现
(2)基于注解方式实现
(1)基于xml
1.普通方法
<bean id =" "  class = "" >
<property name = "" value=" "></property>
或者
<property name = "" ref=" "></property>
 </bean>
 2.有参注入
 <bean id="  " class="  ">
 		<constructor-arg name="  " value="  "></constructor-arg>
			 或者
 		<constructor-arg name="  " ref=" "></constructor-arg>
</bean>
3.p空间
xmlns:p="http://www.springframework.org/schema/p"
(其实就是把自带的bean改成p)我不喜欢这种方法
  <bean id="  " class="  k" p:属性1=" " 
	p:属性2="  "></bean>
特殊值
1.null
<!--null 值--> 
<property name="address">
	<null/>
</property>
2.特殊符号
<property name="address">
	<value><![CDATA[ 内容 ]]></value>
</property>
内部引用
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
 <!--设置两个普通属性-->
 <property name="ename" value="lucy"></property>
 <property name="gender" value="女"></property>
 <!--设置对象类型属性-->
 	<property name="dept">
 		<bean id="dept" class="com.atguigu.spring5.bean.Dept">
 			<property name="dname" value="安保部"></property>
 		</bean>
 	</property>
</bean>
级联赋值
 <bean id="emp" class="com.atguigu.spring5.bean.Emp">
 	<property name="ename" value="lucy"></property>
 	<property name="gender" value="女"></property>
 <!--级联赋值-->
 	<property name="dept" ref="dept"></property>
</bean> 
<bean id="dept" class="com.atguigu.spring5.bean.Dept">
 	<property name="dname" value="财务部"></property>
</bean>2)第二种写法   我不喜欢这种做法
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
 	<property name="ename" value="lucy"></property>
 	<property name="gender" value="女"></property>
 <!--级联赋值-->
 	<property name="dept" ref="dept"></property>
 	<property name="dept.dname" value="技术部"></property>
</bean> 
<bean id="dept" class="com.atguigu.spring5.bean.Dept">
 <property name="dname" value="财务部"></property>
</bean>

xml中写集合

<bean id="stu" class="com.atguigu.spring5.collectiontype.Stu">
 <!--数组类型属性注入-->
 	<property name="courses">
 		<array>
 			<value>java 课程</value>
 			<value>数据库课程</value>
		 </array>
 	</property>
 <!--list 类型属性注入-->
 	<property name="list">
		 <list>
			 <value>张三</value>
 			<value>小三</value>
 		</list>
	 </property>
 <!--map 类型属性注入-->
 	<property name="maps">
 		<map>
 			<entry key="JAVA" value="java"></entry>
 			<entry key="PHP" value="php"></entry>
 		</map>
 	</property>
 <!--set 类型属性注入-->
	 <property name="sets">
		 <set>
			 <value>MySQL</value>
 			<value>Redis</value>
 		</set>
	 </property>
</bean>

把集合注入部分提取出来

(1)引入名称空间util
<?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:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util.xsd">
(2)使用 util 标签完成 list 集合注入提取
<!--1 提取 list 集合类型属性注入--> <util:list id="bookList">
 <value>易筋经</value>
 <value>九阴真经</value>
 <value>九阳神功</value>
</util:list>
<!--2 提取 list 集合类型属性注入使用--> <bean id="book" class="com.atguigu.spring5.collectiontype.Book">
 <property name="list" ref="bookList"></property>
</bean>

工厂bean

有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)
2、普通 bean:在配置文件中定义 bean 类型就是返回类型
3、工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样
第一步 创建类,让这个类作为工厂 bean,实现接口 FactoryBean
第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型

public class MyBean implements FactoryBean<Course> {
 //定义返回 bean
 @Override
 public Course getObject() throws Exception {
 Course course = new Course();
 course.setCname("abc");
 return course;
 }
 @Override
 public Class<?> getObjectType() {
 return null;
 }
 @Override
 public boolean isSingleton() {
 return false;
 } }

 <bean id="myBean" class="com.atguigu.spring5.factorybean.MyBean">
</bean>

@Test
public void test3() {
 ApplicationContext context =
 new ClassPathXmlApplicationContext("bean3.xml");
 Course course = context.getBean("myBean", Course.class);
 System.out.println(course);
}

bean管理

多实例还是单实例

在 Spring 里面,默认情况下,bean 是单实例对象
在 spring 配置文件 bean 标签里面有属性(scope)用于设置单实例还是多实例

(2)scope 属性值
第一个值 默认值,singleton,表示是单实例对象
第二个值 prototype,表示是多实例对象

 <bean id="myBean" class="com.atguigu.spring5.factorybean.MyBean" scope="prototype">
</bean>

生命周期

(1)通过构造器创建 bean 实例(无参数构造)
(2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
(3)把 bean 实例传递 bean 前置处理器的方法 postProcessBeforeInitialization 
(4)调用 bean 的初始化的方法(需要进行配置初始化的方法)
(5)把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization
(6)bean 可以使用了(对象获取到了)
(7)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)
添加initMethod和destropMethod
<bean id="orders" class="com.atguigu.spring5.bean.Orders" 
initmethod="initMethod" destroy-method="destroyMethod">

演示添加后置处理器效果

public class MyBeanPost implements BeanPostProcessor {
 @Override
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
 		System.out.println("在初始化之前执行的方法");
 		return bean;
 }
 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName) 
throws BeansException {
 		System.out.println("在初始化之后执行的方法");
 		return bean;
 } }

自动装配

根据属性名称自动注入
bean 标签属性 autowire,配置自动装配
autowire 属性常用两个值:
byName 根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样
byType 根据属性类型注入

<bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byName">
		<!--<property name="dept" ref="dept"></property>-->
</bean> 
<bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>2)根据属性类型自动注入
<bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byType">
<!--<property name="dept" ref="dept"></property>-->
</bean> 
<bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值