第三阶段---day03

本文详细解读Spring中的IoC控制反转与DI依赖注入,涉及对象生命周期管理、@PostConstruct/@PreDestroy注解、@Component与@ComponentScan,以及MVC设计模式的应用。通过实例演示如何在Spring中使用@Autowired和@Value进行属性注入,以及解决多实现类问题和配置文件应用。
摘要由CSDN通过智能技术生成

1. Spring—IOC

1.1 spring对象的生命周期

1.1.1 生命周期说明

1.对象的创建
2.初始化数据
3.调用方法, 完成功能
4.对象销毁
核心:生命周期的方法都是自动调用

1.1.2 编辑测试类
package com.jt.demo6;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 第三天课程
 */
public class User {

    //1.对象的创建
    public User(){
        System.out.println("用户对象创建成功!!!");
    }

    //2.进行初始化操作
    @PostConstruct
    public void init(){
        System.out.println("为属性赋值!!!");
    }

    //3.进行业务调用  业务方法 用户手动调用!!!
    public void hello(){
        System.out.println("我爱学java");
    }

    //4.销毁方法
    @PreDestroy
    public void destroy(){
        System.out.println("调用销毁方法,释放资源!!!");
    }
}
1.1.3 注解说明

1.@PostConstruct 标识初始化方法
2.@PreDestroy 标识销毁方法

1.1.4 编辑测试类

容器的接口没哟提供关闭容器的操作,如果需要关闭容器,则使用实现类中的方法


package com.jt.demo6;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestUser {

        public static void main(String[] args) {
                AnnotationConfigApplicationContext context =
                        new AnnotationConfigApplicationContext(SpringConfig.class);
                User user = context.getBean(User.class);
                //调用业务方法
                user.hello();
                //如果需要执行销毁方法,则需要先关闭容器对象
                //思想:销毁动作是敏感行为,特殊处理 实现类中有关闭方法.
                context.close();
        }
}


1.2 Spring管理对象 --@Component

1.2.1 @Component注解的作用
package com.jt.demo6;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 第三天课程
 * Spring自动为该注解标识的类通过反射实例化对象,交给Spring容器管理.
 *  Key: 类名首字母小写 user
 *  value: 反射的对象
 *  类似于@Bean注解
 *
 *  @Component/@Bean区别:
 *  1.@Component spring容器通过反射自动创建对象
 *    @Bean     是用户自己手动创建对象
 *  2.@Component 标识类的
 *    @Bean   标识配置类中的方法
 *  3.@Component 对象的Id是类名首字母小写
 *    @Bean   对象的Id是方法名
 */
@Component
public class User {

    //1.对象的创建
    public User(){
        System.out.println("用户对象创建成功!!!");
    }

    //2.进行初始化操作
    @PostConstruct
    public void init(){
        System.out.println("为属性赋值!!!");
    }

    //3.进行业务调用  业务方法 用户手动调用!!!
    public void hello(){
        System.out.println("我爱学java");
    }

    //4.销毁方法
    @PreDestroy
    public void destroy(){
        System.out.println("调用销毁方法,释放资源!!!");
    }
}
1.2.2 @ComponentScan说明

为了让Spring的注解生效. 需要单独使用包扫描注解.
在这里插入图片描述

2.Spring-DI

2.1Spring中依赖注入

说明:依赖注入就是将Spring容器中管理对象(数据),赋值给对象属性
核心机制:如果需要使用依赖注入,对象的属性必须有setxxx()方法

2.2 xml配置文件写法

2.2.1 编辑Person类
package com.jt.demo7_di;

public class Person {
    //定义属性
    private Integer id;
    private String name;

    //为属性赋值,必须有set方法!!!!!!!
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public void sayHi(){
        System.out.println("你好:"+id+":"+name);
    }
}
2.2.2 编辑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">

    <!--xml配置文件 为属性赋值-->
    <bean id="person" class="com.jt.demo7_di.Person">
        <property name="id" value="200"/>
        <property name="name" value="元宵节"/>
    </bean>
</beans>
2.2.3 编辑测试类
package com.jt.demo7_di;

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

public class TestPerson{
	public static void main (String[] args){
		String resource = "spring_di.xml";
		ApplicationContext context =
					new ClassPathXmlApplicationContext(resource);
		Person person = context.getBean(Person.class);
		person.sayHi();
	}
}

2.3 Spring为对象赋值

2.3.1 构建Car对象
package com.jt.demo7_di;

public class Car {

    public void go(){
        System.out.println("我可以带你去任何地方!!!");
    }
}
2.3.2 编辑xml配置文件
<!--2.xml配置文件 为属性赋值-->
<bean id="person" class="com.jt.demo7_di.Person">
	<property name="id" value="200"/>
	<property name="name" value="元宵节"/>
	<!--3.如果将对象赋值给属性,使用ref属性-->
	<property name="car" ref="car"/>
</bean>
2.3.3构建Person类

package com.jt.demo7_di;

public class Person {
    //定义属性
    private Integer id;   //注入简单的数据int String
    private String name;
    //private Car car = new Car(); //耦合性高
    private Car car;    //spring容器为属性赋值.

    //为属性赋值,必须有set方法!!!!!!!
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }

    public void sayHi(){
        System.out.println("你好:"+id+":"+name);
    }

    public void toGo(){
        car.go();
    }
}

2.3.4 编辑测试类
package com.jt.demo7_di;

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

public class TestPerson {

    public static void main(String[] args) {
        String resource = "spring_di.xml";
        ApplicationContext context =
                new ClassPathXmlApplicationContext(resource);
        Person person = context.getBean(Person.class);
        person.sayHi();
        //调用car对象的方法
        person.toGo();
    }
}

IOC/DI 意义:
spring中的IOC/DI两种方式互相配合,实现了代码的松耦合

2.4 注入方式说明

2.4.1 Set方法注入

说明:通过set方法为属性赋值
在这里插入图片描述

2.4.2 构造方法注入

说明:通过构造方法为属性赋值

 <!--1.让spring容器管理car 参数的个数 决定调用的构造方法 -->
    <bean id="car" class="com.jt.demo7_di.Car">
        <constructor-arg name="name" value="奔驰大G"/>
        <constructor-arg name="color" value="绿色"/>
    </bean>

在这里插入图片描述
总结:set和构造都是为属性赋值的常规方法,没有优劣之分,根据习惯调用即可,set注入用的更多一些

2.5 spring属性赋值-----注解用法:@Autowired

2.5.1编辑配置类
package com.jt.demo8_anno;

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

@Configuration
@ComponentScan("com.jt.demo8_anno")
public class SpringConfig {

}
2.5.2 编辑tiger类
package com.jt.demo8_anno;

import org.springframework.stereotype.Component;
//对象套对象
@Component //将该对象交给Spring容器管理
           // key:tiger       value:反射机制生成的对象
public class Tiger implements Pet{

    @Override
    public void hello() {
        System.out.println("窝草:啥玩样");
    }
}
2.5.3 编辑User类
package com.jt.demo8_anno;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 1.该类主要测试 注解的注入方式.
 * 2.练习对象的嵌套关系
 * 3.注解的写法与xml写法的原理相同,形式不同
 */
@Component //key:user value:反射后的对象
public class User { //用户
    /**
     * 注解说明:
     *  1.@Autowired 自动注入
     *  规则:
     *      1.默认条件下使用set方式注入.按照类型匹配.
     *      2.set方式注入也可以按照name名称进行匹配
     *      3.只要使用注解,默认自动生成SetXX方法
     *  实现原理:
     *      1.类型匹配: 如果是接口,则自动匹配其实现类对象
     *      2.name名称匹配: 根据spring中的key进行注入.
     */
    @Autowired
    private Pet pet;

    public void hello(){
        pet.hello();
    }
}
2.5.4 编辑测试类
package com.jt.demo8_anno;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestUser{
	public static void main(String[] args){
	ApplicationContext context = 
				new AnnotationConfigApplicationContext(SpringConfig.class);
	User user = context.getBean(User.class);
	user.hello();
}

}

2.6 多实现类情况处理

2.6.1 异常情况说明

Spring中的DI注入,一般情况下,采用按照类型方式匹配,但是如果遇到多实现类的情况下,则会出现报错:一个接口匹配了多个实现类,程序异常
在这里插入图片描述

2.6.2 指定key进行赋值 使用@Qualifier
package com.jt.demo8_anno;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * 1.该类主要测试 注解的注入方式.
 * 2.练习对象的嵌套关系
 * 3.注解的写法与xml写法的原理相同,形式不同
 */
@Component //key:user value:反射后的对象
public class User { //用户
    /**
     * 注解说明:
     *  1.@Autowired 自动注入
     *  规则:
     *      1.默认条件下使用set方式注入.按照类型匹配.
     *      2.set方式注入 可以按照name名称进行匹配
     *      3.只要使用注解,默认自动生成SetXX方法
     *  实现原理:
     *      1.类型匹配: 如果是接口,则自动匹配其实现类对象
     *      2.name名称匹配: 根据spring中的key进行注入.
     *  注解说明:
     *      @Qualifier必须和@Autowired联用.
     *      并且需要指定value的名称,就是spring中的key
     *
     *  准则: 一般条件下,Spring都是单实现!!!!
     */
    @Autowired
    @Qualifier(value = "pig")
    private Pet pet;

    public void hello(){
        pet.hello();
    }
}

2.7 spring属性赋值–@value注解

2.7.1编辑properties配置文件
#1.数据结构 key=value
#2.其中的数据默认都是String类型,数值可以切换为数值类型
#3.程序读取时,默认的字符集ISO-8859-1 不支持中文,
#  如果支持中文,使用utf-8
dept.id=101
dept.name=行政部

在这里插入图片描述

2.7.2 为属性赋值操作
package com.jt.demo9;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = "classpath:/dept.properties",encoding = "UTF-8")
//当spring容器启动时,加载指定的配置文件,将数据保存到容器中!!!  指定编码格式
public class Dept {

    //取值方式: 从spring容器中获取数据 ${pro中的key}
    @Value("${dept.id}")
    private Integer id;
    @Value("${dept.name}")
    private String name;

    //@Value注解可以为简单属性赋值.
    //@Value("财务部")
    //private String name;

    public void hello(){
        System.out.println("获取数据:"+id+":"+name);
    }
}

2.7.3 编辑测试类
package com.jt.demo9;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestDept {

    public static void main(String[] args) {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        Dept dept = context.getBean(Dept.class);
        dept.hello();
    }
}

3.项目开发思想介绍

3.1 MVC设计思想

mvc说明:
1.M model 数据层 数据处理/数据的持久化
2.V view 视图层 前端页面相关的内容(看到的内容)
3.C control 控制层 控制数据的流转的过程
在这里插入图片描述

3.2 层级代码结构

前提: 在项目中代码需要松耦合. 根据MVC指导思想.后端代码进行了优化,演变为现在的层级代码结构.
层级结构:
1.Controller层 控制层 主要实现与前端页面的交互!!!
2.Service层 业务层 主要实现数据的处理
3.Dao层/Mapper层 持久层 与数据库进行交互.实现数据CURD操作.
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值