栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring5 新手快速入门教程(含代码演示) 一、Spring5入门与注入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring5 新手快速入门教程(含代码演示) 一、Spring5入门与注入

引言: 最近在学 Spring5 ,但网上文档类的教程复杂难入门,但b站有不少视频教程,所以打算边学边写,做个记录

推荐及资料来源尚硅谷Spring5框架教程(idea版)

1.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 开发难度

2.Spring5 下载

Spring5 下载

3.Spring5 入门案例

初探Spring5功能,方便解耦,xml,便于类管理

<1>.创建Students类
package com.company.test;

public class Students {
    private String name;
    private String age;

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

    public void setAge(String age) {
        this.age = age;
    }

    public void showStudents(){
        System.out.println(this.name+" :: "+this.age);
    }
}
<2>.创建xml文件



    
    

<3>.创建测试类,并测试
package com.company.testdemo;


import com.company.test.Students;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudents {
    @Test
    public void testShow() {
        //1 加载 spring 配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beanTest.xml");
        //2 获取配置创建的对象
        Students students = context.getBean("students", Students.class);

        students.setName("张三");
        students.setAge("19");
        students.showStudents();
    }
}

4.属性注入 <1>.有参数构造注入属性 [1].修改Students类
package com.company.test;

public class Students {
    private String name;
    private String age;

    
    public Students(String name, String age) {
        this.name = name;
        this.age = age;
    }

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

    public void setAge(String age) {
        this.age = age;
    }

    public void showStudents(){
        System.out.println(this.name+" :: "+this.age);
    }
}
[2].修改xml文件



    
    
        
        
        
    

[3].修改测试类,并测试
package com.company.testdemo;

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

public class TestUser {
    @Test
    public void testShowUser() {
        //1 加载 spring 配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beanTest.xml");
        //2 获取配置创建的对象
        User user = context.getBean("user", User.class);

//        user.setUserName("张三");
//        user.setAge("19");
        user.showUser();
    }
}

<2>.set 方法注入属性 [1].修改xml文件
```java



    
    
        
        
        
    

5.类对象注入 <1>.外部注入 [1].创建Teachers类
package com.company.test;

public class Teachers {
    private String name;
    private String subjects;

    public Teachers(String name, String subjects) {
        this.name = name;
        this.subjects = subjects;
    }

    public void showStudents(){
        System.out.println(this.name+" :: "+this.subjects);
    }
}

[2].修改Students类
package com.company.test;

public class Students {
    private String name;
    private String age;
    private Teachers teachers;//新增

    public Students(String name, String age) {
        this.name = name;
        this.age = age;
    }

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

    public void setAge(String age) {
        this.age = age;
    }

    public void setTeachers(Teachers teachers) {
        this.teachers = teachers;
    }

    public void showStudents(){
        System.out.println(this.name+" :: "+this.age);
    }

    public void showTeachers(){
        this.teachers.showStudents();
    }
}

[3].修改xml文件



    
    
        
        
        
        
    
    
        
        
    

[4].修改测试类,并测试
package com.company.testdemo;


import com.company.test.Students;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudents {
    @Test
    public void testShow() {
        //1 加载 spring 配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beanTest.xml");
        //2 获取配置创建的对象
        Students students = context.getBean("students", Students.class);

        students.showTeachers();
    }
}

<2>.内部注入 [1].修改xml文件



    
    
        
        
        
        
            
                
                
            
        
    


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号