Spring5-入门&IOC

1.课程内容介绍以及Spring框架的概述

在这里插入图片描述

2.spring5入门案例

2.1 项目创建以及相关依赖的导入

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.2 代码演示

普通类User代码

package com.atguigu.spring5;

public class User {
    public void add(){
        System.out.println("add......");
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置User对象创建-->
    <bean id="user" class="com.atguigu.spring5.User"></bean>
</beans>

测试类代码

package com.atguigu.spring5;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {
    @Test
    public void userTest(){
        //1.加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取配置文件创建对象
        User user = context.getBean("user",User.class);
        System.out.println(user);
        user.add();
    }
}

3.IOC底层原理

3.1 工厂模式降低耦合示意图

在这里插入图片描述

3.2 IOC的概念和原理

在这里插入图片描述

3.3 IOC(BeanFactory接口)

使用FileSystemXmlApplicationContext传入参数为配置文件的绝对路径,具体到那一个盘下。
使用ClassPathXmlApplicationContext传入参数为src下配置文件名
在这里插入图片描述

4.ICO操作Bean管理

4.1 概念和Bean管理的两种操作

在这里插入图片描述
在这里插入图片描述

4.2 IOC操作Bean管理(基于xml方式)

4.2.1 基于xml方式创建对象

在这里插入图片描述

4.2.2 基于xml方式注入属性

在这里插入图片描述

4.2.2.1 第一种注入方式:使用 set 方法进行注入

(1)创建类,定义属性和对应的 set 方法

Book类代码

package com.atguigu.spring5;

public class Book {
    private String name;
    private String bauthor;

    public void setName(String name) {
        this.name = name;
    }

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

    public void testDemo(){
        System.out.println(name+"::"+bauthor);
    }
}

(2)在 spring 配置文件配置对象创建,配置属性注入

ean id="book" class="com.atguigu.spring5.Book">
        <!--使用 property 完成属性注入
        name:类里面属性名称
        value:向属性注入的值
        -->
        <property name="name" value="易筋经"></property>
        <property name="bauthor" value="老子"></property>
    </bean>

(3)编写测试类测试代码

@Test
    public void bookTest(){
        //1.加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取配置文件创建对象
        Book book = context.getBean("book",Book.class);
        System.out.println(book);
        book.testDemo();
    }
4.2.2.2 第二种注入方式:使用有参构造进行注入

(1)创建类,定义属性,创建属性对应有参数构造方法

package com.atguigu.spring5;

public class Order {
    private String oname;
    private String address;

    //有参构造

    public Order(String oname, String address) {
        this.oname = oname;
        this.address = address;
    }

    public void test(){
        System.out.println(oname +"::"+address);
    }
}

(2)在 spring 配置文件中进行配置

<!--有参构造注入属性-->
    <bean id="orders" class="com.atguigu.spring5.Order">
        <constructor-arg name="oname" value="电脑"></constructor-arg>
        <constructor-arg name="address" value="China"></constructor-arg>
    </bean>

(3)编写测试类测试代码

@Test
    public void orderTest(){
        //1.加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取配置文件创建对象
        Order order = context.getBean("orders", Order.class);
        System.out.println(order);
        order.test();
    }
4.2.2.3 p 名称空间注入(了解)

在这里插入图片描述

4.2.3 注入其他类型属性

4.2.3.1 字面量的介绍

在这里插入图片描述

4.2.3.2 注入bean属性方式一:外部Bean

在这里插入图片描述
(1)创建 dao 类
UserDao接口

package com.atguigu.spring5.dao;

public interface UserDao {
    public void update();
}

UserDao实现类

package com.atguigu.spring5.dao.impl;

import com.atguigu.spring5.dao.UserDao;

public class UserDaoImpl implements UserDao {

    @Override
    public void update(){
        System.out.println("update.......");
    }
}

(2)在 service 调用 dao 里面的方法

package com.atguigu.spring5.server;

import com.atguigu.spring5.dao.impl.UserDaoImpl;

public class UseServer {
    private UserDaoImpl userDao;

    public void setUserDao(UserDaoImpl userDao) {
        this.userDao = userDao;
    }

    public void add(){
        System.out.println("add......");
        userDao.update();
    }
}

(3)在 spring 配置文件中进行配置

 <!--1 service 和 dao 对象创建-->
    <bean id="userServer" class="com.atguigu.spring5.server.UseServer">
        <!--注入 userDao 对象
            name 属性:类里面属性名称
            ref 属性:创建 userDao 对象 bean 标签 id 值
        -->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="userDaoImpl" class="com.atguigu.spring5.dao.impl.UserDaoImpl"></bean>

(4)编写测试类

	@Test
    public void UserServerTest(){
        //1.加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        //2.获取配置文件创建对象
        UseServer useServer = context.getBean("userServer", UseServer.class);
        System.out.println(useServer);
        useServer.add();
    }
4.2.3.3 注入bean属性方式二:内部Bean

在这里插入图片描述
部门类代码

package com.atguigu.spring5.bean;

public class Dept {
    private String dname;

    public void setDname(String dname) {
        this.dname = dname;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "dname='" + dname + '\'' +
                '}';
    }
}

员工类代码

package com.atguigu.spring5.bean;

public class Emp {
    private String ename;
    private String gender;
    //所属的部门
    private Dept dept;

    public void setEname(String ename) {
        this.ename = ename;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public void test(){
        System.out.println(ename+"::"+gender+"::"+dept);
    }
    
}

xml配置文件

<!--内部bean注入属性-->
    <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>

测试方法

est
    public void EmpTest(){
        //1.加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        //2.获取配置文件创建对象
        Emp emp = context.getBean("emp", Emp.class);
        System.out.println(emp);
        emp.test();
    }
4.2.3.4 级联赋值

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.2.3.5 xml注入集合属性

在这里插入图片描述
(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法

package com.atguigu.spring5.collectiontype;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Stu {
    private String[] courses;
    private List<String> list;
    private Map<String,String> maps;
    private Set<String> sets;

    public void setCourses(String[] courses) {
        this.courses = courses;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    public void setSets(Set<String> sets) {
        this.sets = sets;
    }

    public void test(){
        System.out.println(Arrays.toString(courses));
        System.out.println(list);
        System.out.println(maps);
        System.out.println(sets);
    }
}

(2)在 spring 配置文件进行配置

 <!--集合类属性注入-->
    <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>

(3)编写测试代码

@Test
    public void collectionsTest(){
        //加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //创建stu对象
        Stu stu = context.getBean("stu",Stu.class);
        stu.test();
    }
4.2.3.5 在集合里面设置对象类型值

(1)编写Course类

package com.atguigu.spring5.collectiontype;

public class Course {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "CourseList{" +
                "name='" + name + '\'' +
                '}';
    }
}

(2)在上面Stu类的代码基础上再创建Couse对象集合CouseList
在这里插入图片描述
(3)先在配置文件中创建多个course对象,并将它们传入stu类的cousrList集合中
在这里插入图片描述
在这里插入图片描述

4.2.3.6 把集合注入部分提取出来

(1)创建Book类

package com.atguigu.spring5.collectiontype;

import java.util.List;

public class Book {
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }
    public void test(){
        System.out.println(list);
    }
}

(2)在spring配置文件件中引入名称空间util并使用util标签完成list集合的注入
在这里插入图片描述
(3)编写测试类

 @Test
    public void bookTest(){
        //加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        //创建stu对象
        Book book = context.getBean("book",Book.class);
        book.test();
    }
4.2.3.7 工厂bean

在这里插入图片描述
Mybean类代码

package com.atguigu.spring5.fatorybean;

import com.atguigu.spring5.collectiontype.Course;
import org.springframework.beans.factory.FactoryBean;

public class MyBean implements FactoryBean<Course> {
    //通过这个方法来设置返回的bean
    @Override
    public boolean isSingleton() {
        return false;
    }

    @Override
    public Course getObject() throws Exception {
        Course course = new Course();
        course.setName("abc");
        return course;
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }
}

xml配置文件

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

4.2.4 bean作用域

作用域就是设置bean实例是单实例还是多实例
在这里插入图片描述

4.2.5 bean生命周期

4.2.5.1 bean的常见五个生命周期

在这里插入图片描述
Orders类代码

package com.atguigu.spring5.collectiontype;

public class Orders {
    private String name;

    //创建无参构造
    public Orders() {
        System.out.println("第一步 执行无参构造创建bean实例");
    }

    //调用set方法设置属性值
    public void setName(String name) {
        this.name = name;
        System.out.println("第二步 调用set方法设置属性值");
    }

    //创建执行的初始化方法(需要自己配置)
    public void initMethod(){
        System.out.println("第三步 调用执行的初始化方法");
    }

    //第四步,获取创建bean实例对象

    //创建销毁的方法
    public void destroyMethod(){
        System.out.println("第五步 执行销毁的方法");
    }

}

配置文件

<?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="orders" class="com.atguigu.spring5.collectiontype.Orders"
          init-method="initMethod" destroy-method="destroyMethod">
        <property name="name" value="手机"></property>
    </bean>
</beans>

测试代码

@Test
    public void testBean4(){
        //加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
        //创建stu对象
        Orders orders = context.getBean("orders",Orders.class);
        System.out.println("第四步 获取创建bean实例对象");
        System.out.println(orders);
        //close方法时ClassPathXmlApplicationContext类中的
        ((ClassPathXmlApplicationContext)context).close();
    }

运行结果截图
在这里插入图片描述

4.2.5.2 后置处理器

配置文件中添加完后置处理器,会为配置文件中的所有bean都添加上后置处理器
在这里插入图片描述
在这里插入图片描述
(1)创建类,实现BeanPostProcessor接口,创建后置处理器

package com.atguigu.spring5.collectiontype;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.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;
    }
}

(2)配置后置处理器

<?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="orders" class="com.atguigu.spring5.collectiontype.Orders"
          init-method="initMethod" destroy-method="destroyMethod">
        <property name="name" value="手机"></property>
    </bean>
    <!--配置后置处理器-->
    <bean id="myBeanPost" class="com.atguigu.spring5.collectiontype.MyBeanPost"></bean>
</beans>

测试类运行结果
在这里插入图片描述

4.2.6 xml自动装配

在这里插入图片描述
在这里插入图片描述

4.2.7 外部属性文件

在这里插入图片描述
在这里插入图片描述

4.3 IOC 操作 Bean 管理(基于注解方式)

4.3.1 基本概念介绍以及针对bean管理创建对象提供的注解

在这里插入图片描述
在这里插入图片描述

4.3.2 基于注解方式实现对象创建

在这里插入图片描述


    <!--开启组件扫描
         1 如果扫描多个包,多个包使用逗号隔开
         2 扫描包上层目录
    -->
    <context:component-scan base-package="com.atguigu"></context:component-scan>

在这里插入图片描述

package com.atguigu.spring5.service;

import org.springframework.stereotype.Component;
//在注解里面 value 属性值可以省略不写,
//默认值是类名称,首字母小写
//UserService -- userService
//value相当于使用xml配置文件中的id
@Component(value="userService")
public class UserService {
    public void add(){
        System.out.println("add......");
    }
}

4.3.3 开启组件扫描细节配置

在这里插入图片描述

4.3.4 基于注解方式实现属性注入

4.3.4.1 @Autowired实现属性注入

在这里插入图片描述
UserDao代码

package com.atguigu.spring5.dao;

import org.springframework.stereotype.Component;

@Component
public class UserDaoimpl implements UserDao{
    @Override
    public void add() {
        System.out.println("dao add......");
    }
}

service代码

package com.atguigu.spring5.service;

import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service(value="userService")
public class UserService {
    //定义 dao 类型属性
    //不需要添加 set 方法
    //添加注入属性注解
    @Autowired
    private UserDao userDao;

    public void add(){
        userDao.add();
        System.out.println("Service add......");
    }
}

测试类代码

 @Test
    public  void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();

    }
4.3.4.2 @Qualifiler实现属性注入

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

4.3.4.3 @Resource和@Value实现属性注入

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.3.5 完全注解开发

创建类加上相关注解代替xml文件

package com.atguigu.spring5.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration//作为配置类,替代xml文件
@ComponentScan(basePackages = {"com.atguigu"})//扫描的目录
public class SpringCofig {
}

测试类

 @Test
    public  void test1(){
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringCofig.class);//这里传入配置的class类型
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值