IOC自学笔记,b站尚硅谷Spring5idea

1. 什么是IOC

(1)控制反转,把对象的创建和对象之间调用的过程,交给Spring进行管理 
(2)降低代码之间的耦合度。

2. IOC底层原理

xml的解析,工厂模式,反射

3. IOC底层原理

(1)xml配置文件,配置创建的对象
(2)<bean id="xx" class="xx.xxx.xxx.xxx"></bean>
(3)  。。。。。。

4. IOC接口

1.IOC是想基于IOC容器完成,IOC容器底层是对象工厂
2.Spring提供IOC容器实现两种方式(两个接口)
(1)BeanFactory:IOC容器最基本的实现,是spring内部使用的接口,不提供开发人员使用
	*加载配置文件,不会去创建对象,在获取(使用)对象才回去创建对象

(2)ApplicationContext:BeanFactory接口的子接口,提供更多更强大的功能。
	*加载配置文件会把配置文件对象进行创建(当服务器启动时,就创建对象,比需要时创建对象更快捷
3.ApplicationContext实现类*查看树的形式累的继承关系F4
(1)ClassPathXmlApplicationContext
(2)FileSystemXmlApplicationContext

5. IOC操作Bean管理

1.什么是Bean管理

(0)Bean管理两个操作
(1)Spring创建对象
(2)spring注入属性

2.Bean管理操作有两种方式

(1)基于xml配置文件方式实现
	创建对象
		1/ <bean id="user" class="xx.xxx..x.xx"></bean>
		//使用bean标签,标签里面添加对应属性,就可以实现创建对象
		//id属性:给这个对象起个别名,唯一标识
		//class属性:类全路径
		//默认执行无参构造完成对象创建
	注入属性
		(1)DI:依赖注入,就是注入属性。是IOC的一种实现
			1.使用set注入
				(1)创建类,set方法
package com.zx.spring;

/**
 * 演示使用set方法进行注入属性
 * @author lenovo
 * @data 2020/12/10 —— 10:37
 */
public class Book {
    private  String bname;
    private String bauthor;

    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
}


    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
}
    <bean id="book" class="com.zx.spring.Book">
        <!--使用property完成属性注入-->
        <property name="bname" value="易筋经"/>
        <property name="bauthor" value="达摩老祖"/>

    </bean>
<bean id="order" class="com.zx.spring.Orders">
        <constructor-arg name="oname" value="电脑"></constructor-arg>
        <constructor-arg name="adress" value="China"></constructor-arg>

        
        <!--用方法内的顺序-->
        <!--<constructor-arg index="0" value="xxx"></constructor-arg>-->
    </bean>

3.p名称空间注入(了解,用的不多)(低层是set方法注入)

(1)使用p名称空间注入,可以简化xml配置
<?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">
(2)进行属性注入,在bean标签里面进行操作
<bean id="p" class="com.zx.spring.Book" p:bname="九阳神功" p:bauthor="无名氏"></bean>

IOC操作Bean管理(xml注入其他类型属性)

1.字面量(设置属性的默认值)

(1)null值
<!--设置属性的值为null值-->
<property name="address">
    <null/>
</property>
(2)属性值包含特殊符号
<property name="address" >
    <value><![CDATA[<<南京>>]]></value>
	//会根据value内容变长度,换行是有区别的
</property>

2.注入属性-外部bean

service调用dao层
(1)创建两个类service和dao
(2)在service调用dao
(3)在spring配置文件中进行配置
创建service要声明dao层的属性,用xml赋值;
<!--service和dao对象创建-->
    <bean id="userService" class="com.zx.spring.service.UserService">

        <!--注入userdao对象
            name属性:类里面的属性名称
            ref属性:xml文件中,创建userDao对象bean标签id值-->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="userDaoImpl" class="com.zx.spring.dao.UserDaoImpl"></bean>

3.注入属性-内部bean和级联赋值

(1)一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门
部门是一,员工说多
(2)在实体类之间表示一对多关系

内部ben的写法
   <bean id="emp" class="com.zx.spring.bean.Emp">
        <!--设置两个普通的属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value=""></property>

        <!--设置对象类型属性-->
        <property name="dept">
           <bean id="dept"  class="com.zx.spring.bean.Dept">
               <property name="dname" value="安保"></property>
           </bean>
        </property>
    </bean>

4.注入属性-级联赋值

方法一
   <bean id="emp" class="com.zx.spring.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.zx.spring.bean.Dept">
        <property name="dname" value="财务部"></property>
    </bean>
方法二:需要get方法,(并且需要注入dept对象,否则会报null)
<bean id="emp" class="com.zx.spring.bean.Emp">
    <!--设置两个普通的属性-->
    <property name="ename" value="lucy"></property>
    <property name="gender" value=""></property>

    <!--设置对象类型属性-->
    <property name="dept" ref="dept"></property>


    <!--方法二-->
    <!--需要get方法才能使用-->
    <property name="dept.dname" value="技术部"></property>


</bean>
<bean id="dept" class="com.zx.spring.bean.Dept">
    <property name="dname" value="财务部"></property>
</bean>

IOC操作Bean管理(xml注入集合属性)
1.注入数组类型属性
2.注入list集合类型属性
3.注入Map集合类型属性
(1)创建相关类型的属性

<bean id="stu" class="com.zx.spring.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>


    <!--list集合元素为对象-->
    <property name="courseList">
        <list>
            <ref bean="course1"></ref>
            <ref bean="course2"></ref>
        </list>
    </property>
</bean>
<bean id="course1" class="com.zx.spring.collectiontype.Course">
    <property name="ename" value="one课程"></property>
</bean>
<bean id="course2" class="com.zx.spring.collectiontype.Course">
    <property name="ename" value="two课程"></property>
</bean>

使用util标签提取数据,再对属性赋值

	(1)在Spring配置文件中引入命名空间util
<beans xmlns="http://www.springframework.org/schema/beans"
		
		   xmlns:util="http://www.springframework.org/schema/util"
		   
		   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
		   
			http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
	(2)使用util标签抽取出来
 <!--提取List集合类型属性注入-->
<util:list id="booklist">
	<!--<ref bean=""></ref>-->
	<value>one bok</value>
	<value>two book</value>
	<value>three book</value>
</util:list>

<!--2.属性注入-->
<bean id="book" class="com.zx.spring.collectiontype.Book">
	<property name="list" ref="booklist"></property>
</bean>

IOC 操作bean管理(FactoryBean)

1.Spring有两种类型的bean 一种普通(自己创建的)bean 另外一种工厂bean(FactoryBean)
2.普通bean:在Spring配置文件中定义的bean类型就是返回类型(class中的包路径最后一个类就为bean类型)
3.工厂bean:在配置文件定义bean类型可以和返回类型不一样
第一步 创建类:让这个类作为工厂Bean;实现接口FactoryBean
第二步 实现接口里面的方法,在实现的方法中定义返回的bean类型
实现这个接口FactoryBean<Course>

代码

IOC 操作bean管理(bean作用域)

1.在spring理,设置创建bean实例是单实例还是多实例
2.默认为单实例
3.(1)在spring配置文件bean标签里面有属性(scope)用于设置单实例还是多实例
(2)scope属性值
第一个 默认值:singleton,表示单实例
第二个 prototype,表示是多实例对象

request表示一次请求,每次创建对象放到request
session表示一次会话,每次创建对象放到session
(3)singleton和prototype区别
singleton为单实例,prototype多实例
设置scope为singleton,加载配置文件时就会创建单实例对象
设置为prototype时,在调用getbean是创建多实例对象

IOC 操作bean管理(bean生命周期)

1.生命周期
(1)从对象创建到对象销毁的过程
2.bean生命周期
(1)通过构造器创建bean实例(无参构造)
(2)为bean的属性设置值和其他bean的引用(调用类中set方法)
(3)调用bean的初始化的方法(需要进行配置)
(4)bean可以使用(对象获取到了)
(5)当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法)	

3.演示

4.bean的后置处理器

(1)通过构造器创建bean实例(无参构造)
(2)为bean的属性设置值和其他bean的引用(调用类中set方法)
(3)把bean实例传递bean后置处理器的方法   postProcessBeforeInitialization方法
(4)调用bean的初始化的方法(需要进行配置)
(5)把bean实例传递bean后置处理器的方法  postProcessAfterInitialization方法
(6)bean可以使用(对象获取到了)
(7)当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法)
5.演示添加后置处理器

(1)创建类,实现接口BeanPostProcessor

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;
    }
}

ioc操作bean管理(xml自动装配)
1.什么是自动装配
(1)根据指定装配着(属性名称或属性类型),spring自动将匹配的属性值进行注入
2.演示

<!--实现制动装配
bean标签属性autowire,配置制动装配
autowire属性值常用2个值:
byName根据属性名称注入,注入值bean的id值和类属性名称一样
byType-->
<bean id="emp" class="com.zx.spring.autowire.Emp" autowire="byName">

    <!--&lt;!&ndash;手动装配&ndash;&gt;-->
    <!--<property name="dept" ref="dept"></property>-->


</bean>

<bean id="dept" class="com.zx.spring.autowire.Dept"></bean>

IOC操作bean管理(外部属性文件)
1.直接配置数据库的信息
(1)配置德鲁伊连接池

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql//localhost:3306/UserDb"></property>
    <property name="username" value="root"></property>
    <property name="password" value="262939"></property>

</bean>

2.引入外部属性文件配置数据库连接池
(1)创建外部属性文件,properties格式文件,写数据库信息
在src目录下创建file文件名为jdbc.properties

prop.dirverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql//localhost:3306/UserDb
prop.userName=root
prop.password=262939

(2)把外部properties属性文件引入到spring配置文件
*引入context名称空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
      * xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        *http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

在spring中配置文件使用标签引入外部属性文件

<context:property-placeholder location=“classpath:jdbc.properties”></context:property-placeholder>

<property name="driverClassName" value="${prop.dirverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.userName}"></property>
<property name="password" value="${prop.password}"></property>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值