spring5应用 看这一篇就够了!(参考尚硅谷)

spring框架搭建(准备工作)

spring框架jar包下载

jar下载链接
在这里插入图片描述
我所使用的是5.2.6,如果需要其他版本自己进行选择
在这里插入图片描述
点进对应版本选择(spring-5.2.6.RELEASE-dist.zip) 此项其他版本对应其他的选项。下载后自己进行解压。

spring框架使用

1.首先建立一个普通Java工程(后面创建过程省略,名字可以修改)(也可以直接选择创建Spring)
在这里插入图片描述
2.创建之后的样子
在这里插入图片描述
3.导入基础的五个spring(上面的下载的五个jar包就完成框架的搭建)
IDEA不会导入jar包见此处

一、Spring5 框架概述

1、Spring 是轻量级的开源的 JavaEE 框架
2、Spring可以解决企业应用开发的复杂性
3、Spring 有两个核心部分:IOC 和 Aop
(1)IOC:控制反转,把创建对象过程交给 Spring 进行管理
(2)Aop:面向切面,不修改源代码进行功能增强
4、Spring 特点
(1)方便解耦,简化开发
(2)Aop 编程支持
(3)方便程序测试
(4)方便和其他框架进行整合
(5)方便进行事务操作
(6)降低 API 开发难度

入门案例

包结构

在这里插入图片描述

代码展示及详解

1.首先在普通User类中创建一个普通方法add()

package com.it;

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

2、创建 Spring 配置文件,在配置文件配置创建的对象

<?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.it.User"></bean>
</beans>

3、编写测试代码

package test;

import com.it.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

二、IOC(概念和原理)

1、什么是 IOC

IOC具体细节看这里
(1)控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理
(2)使用 IOC 目的:为了耦合度降低
(3)做入门案例就是 IOC 实现

2、IOC 底层原理

(1)xml 解析、工厂模式、反射

3、画图讲解 IOC 底层原理

在这里插入图片描述

IOC(BeanFactory 接口)

1、IOC 思想基于 IOC 容器完成,IOC 容器底层就是对象工厂

2、Spring 提供 IOC 容器实现两种方式:(两个接口)
(1)BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用 * 加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象
(2)ApplicationContext:BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人 员进行使用 * 加载配置文件时候就会把在配置文件对象进行创建

ApplicationContext 接口有实现类

再写Tomcat时,使用ApplicationContext接口,可以在开启服务时创建对象,避免在使用时创建对象。

IOC 操作 Bean 管理(概念)

1、什么是 Bean 管理
(0)Bean 管理指的是两个操作
(1)Spring 创建对象
(2)Spirng 注入属性
2、Bean 管理操作有两种方式
(1)基于 xml 配置文件方式实现
(2)基于注解方式实现

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

基于 xml 方式创建对象

在这里插入图片描述
(1)在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
(2)在 bean 标签有很多属性,介绍常用的属性

  • id 属性:唯一标识
  • class 属性:类全路径(包类路径)
  • name属性:标识基本等同于id但是name属性可以为特殊符号
    (3)创建对象时候,默认也是执行无参数构造方法完成对象创建

基于 xml 方式注入属性

(1)DI:依赖注入,就是注入属性 (IOC和DI区别:DI是IOC的一种具体实现,他就表示依赖注入,就是注入属性,注入属性在创建对象的基础上进行!)

第一种注入方式:使用 set 方法进行注入 (原始方法)

package com.it;
public class Book {
    private String bookname;
    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
    public static void main(String[] args) {
        Book book = new Book();
        book.setBookname("heima");
    }
}

spring方法
创建类,定义属性和对应的 set 方法

package com.it;

public class Book {
    // 创建属性     
     private String bname;     
     private String bauthor;     
    // 创建属性对应的 set 方法     
     public void setBname(String bname) {         
     this.bname = bname;     }     
     public void setBauthor(String bauthor) {         
     this.bauthor = bauthor;     
     } 
}

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

    <!--set方法注入属性-->
    <!--对象创建-->
    <bean id="book" class="com.it.Book">
        <!--使用完成属性注入-->
        <!-- 使用 property 完成属性注入         
            name :类里面属性名称         
            value :向属性注入的值    
             -->
        <property name="bname" value="黑马"></property>
        <property name="bauthor" value="博文"></property>  
    </bean>
</beans>

第二种方式:有参数构造注入(原始方法)

package com.it;

public class Book {

    private String bookname;

public Book(String bn){
        this.bookname = bn;

    }

    public static void main(String[] args) {
        Book book1 = new Book("heima");
    }
}

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

package com.it;

public class Order {
    private String name;
    private String address;
//参数构造
    public Order(String name , String address) {
        this.name = name;
        this.address = address;
    }
    public void orders(){
        System.out.println(aname+"||||"+address);
    }
}

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

<!--    有参数构造注入属性-->
    <bean id="order" class="com.it.Order">
        <constructor-arg name="aname" value="苹果"></constructor-arg>
        <constructor-arg name="address" value="临沭"></constructor-arg>
    </bean>

p 名称空间注入(了解)

(1)使用 p 名称空间注入,可以简化基于 xml 配置方式 第一步 添加 p 名称空间在配置文件中

<?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">
</beans>

第二步 进行属性注入,在 bean 标签里面进行操作

<bean id="book" class="com.it.Book" p:bauthor="黑马" p:bname="博文">
</bean>

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

1、字面量

(1)null 值

    <bean id="order" class="com.it.Order">
        <property name="aname" value="wo"></property>
        <property name="address" value="dizhi"></property>
		<!--设置null值-->
        <property name="bathor" >
            <null/>
        </property>
    </bean>

(2)属性值包含特殊符号

    <bean id="order" class="com.it.Order">
        <property name="aname" value="wo"></property>
        <property name="address" value="dizhi"></property>
        <!-- 属性值包含特殊符号
        1 把 <> 进行转义 &lt; &gt;
        2 把带特殊符号内容写到 CDATA -->
        <property name="bathor">
            <value><![CDATA[<<南京>>]]></value>
        </property>
    </bean>

2、注入属性-外部 bean

(1)创建两个类 service 类和 dao 类
(2)在 service 调用 dao 里面的方法
(3)在 spring 配置文件中进行配置

package com.it.service;
import com.it.User;
import com.it.dao.UserDao;
import com.it.dao.UserDaoImpl;

public class UserService {

//    创建UserDao类型的属性,生成set方法
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void add(){
        System.out.println("adding.........");

//        //创建UserDao对象(原始方法)
//        UserDao dao = new UserDaoImpl();
//        dao.updata();
    }
}

<?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="Us" class="com.it.service.UserService">
<!--        注入名称-->
 <! -- 注入 userDao 对象
          name 属性:类里面属性         
          ref 属性:创建 userDao 对象 bean 标签 id     
 -- >   
        <property name="userDao" ref="Ud"></property>
    </bean>
    <bean id="Ud" class="com.it.dao.UserDaoImpl"></bean>
</beans>
package test;

import com.it.Book;
import com.it.Order;
import com.it.User;
import com.it.dao.UserDaoImpl;
import com.it.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class testSpring {
    @Test
    public void testorder() {
    //1.加载Spring配置文件
    ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
    //2.获取配置的创建对象
    UserService userService = context.getBean("Us",UserService.class);
        userService.add();
    }

}

3、注入属性-内部 bean

(1)一对多关系:部门和员工 一个部门有多个员工,一个员工属于一个部门 部门是一,员工是多
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

package com.it.ben;

public class dept {
    private String dname;

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

    @Override
    public String toString() {
        return "dept{" +
                "dname='" + dname + '\'' +
                '}';
    }
}
package com.it.ben;

import com.it.ben.dept;
// 员工类

public class Emp {
    private String name;
    private String gender;
//    职工属于一个部门,使用对象形式表示
    private dept dept;

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

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

    public void setDept(dept dept) {
        this.dept = dept;
    }
    public void add(){
        System.out.println(name+"|"+gender+"|"+dept);
    }
}

注入bean的spring配置

<?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  -->
    <bean id="emp" class="com.it.ben.Emp">
        <property name="name" value="张三"></property>
        <property name="gender" value=""></property>
        <property name="dept" >
            <bean id="dept" class="com.it.ben.dept">
                <property name="dname" value="安保"></property>
            </bean>
        </property>
    </bean>
</beans>

4、注入属性-级联赋值

(1)第一种写法 (spring配置)

<?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="emp" class="com.it.ben.Emp">
        <property name="name" value="张三"></property>
        <property name="gender" value=""></property>
        <property name="dept" ref="dept" ></property>
    </bean>
    <bean id="dept" class="com.it.ben.dept">
        <property name="dname" value="人力资源"></property>
    </bean>
</beans>

(2)第二种写法
需要对上方的Emp进行修改(添加get方法,获取dept对象)

package com.it.ben;

import com.it.ben.dept;
// 员工类

public class Emp {
    private String name;
    private String gender;
//    职工属于一个部门,使用对象形式表示
    private dept dept;

    public com.it.ben.dept getDept() {
        return dept;
    }

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

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

    public void setDept(dept dept) {
        this.dept = dept;
    }
    public void add(){
        System.out.println(name+"|"+gender+"|"+dept);
    }
}

spring配置

<?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="emp" class="com.it.ben.Emp">
        <property name="name" value="张三"></property>
        <property name="gender" value=""></property>
        <property name="dept" ref="dept" ></property>
        <property name="dept.dname" value="安全部"></property>
    </bean>
    <bean id="dept" class="com.it.ben.dept">
    </bean>
</beans>

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

1、什么是注解

(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…) (2)使用注解,注解作用在类上面,方法上面,属性上面
(3)使用注解目的:简化 xml 配置

2、Spring 针对 Bean 管理中创建对象提供注解

(1)@Component
(2)@Service
(3)@Controller
(4)@Repository
上面四个注解功能是一样的,都可以用来创建 bean实例

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

第一步 引入依赖
在这里插入图片描述
第二步 开启组件扫描
开启组件扫描
1 如果扫描多个包,多个包使用逗号隔开
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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
    <context:component-scan base-package="dao"></context:component-scan>
</beans>
package dao;

        import org.springframework.stereotype.Component;
// 在注解里面 value 属性值可以省略不写,
// 默认值是类名称,首字母小写
// 例UserService -- userService
@Component(value = "user")  //<bean id="userService" class=".."/>
public class user {

    public void add() {
        System. out .println("service add.......");
    }
}
package test;
import dao.user;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//面向切向aop
public class test1 {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        user ur = context.getBean("user", user.class);
        System.out.println(ur);
        ur.add();
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊~小 l i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值