3

Spring5框架概述、入门案例

 1 year ago
source link: https://blog.51cto.com/u_15740728/5918810
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

最核心的莫过于IOC和Aop。也是面试经常问到的地方、面试中几乎必定会问到

spring模块

Spring5框架概述、入门案例_jar包

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、入门案例

2.1 下载spring所依赖的核心jar包

Spring5框架概述、入门案例_jar包_02

友情提示:你也可以直接使用IDEA直接建立一个spring的项目、这样就不需要导入jar包(创建的spring项目所依赖的jar包更完善、不仅仅只有这几个)

<hr>

2.2 创建一个普通的java项目

Spring5框架概述、入门案例_插入图片_03

2.3 将jar包导入项目

2.3.1 在项目中新建一个lib包

Spring5框架概述、入门案例_插入图片_04

2.3.2 将jar包复制粘贴到lib中

Spring5框架概述、入门案例_spring_05

2.3.2 file–>Project Structure–>modules

Spring5框架概述、入门案例_插入图片_06

2.3.3 选择刚刚的jar包

Spring5框架概述、入门案例_spring_07
Spring5框架概述、入门案例_spring_08

2.4 、创建一个普通类

package com.zyz.spring5;

/**
 * @author zyz
 * @version 1.0
 * @data 2022/9/19 22:11
 */
public class Person {
    public void say(){
        System.out.println("我是一个普通类");
    }
}

<hr>

2.5 创建Spring配置文件,在配置文件中配置创建对象

Spring5框架概述、入门案例_jar包_09
Spring5框架概述、入门案例_插入图片_10
<?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">
    <!-- 配置Person对象创建-->
    <bean id="person" class="com.zyz.spring5.Person"></bean>

</beans>

<hr>

2.6 测试

package com.zyz.testdemo;

import com.zyz.spring5.Person;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author 静小文
 * @version 1.0
 * @data 2022/9/19 22:16
 */
public class TestSpring {
    @Test
    public void test(){
        //1、加载spring配置文件
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

        //2、获取配置创建的对象
        Person person = context.getBean("person", Person.class);
        System.out.println(person);
        person.say();

    }
}

<hr>

2.7 测试结果

Spring5框架概述、入门案例_插入图片_11


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK