Spring学习笔记

1.创建Maven工程,pom.xml文件导入依赖

复制代码
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.20</version>
</dependency>

2.创建一个entity包,在包里面创建一个student类,

复制代码
public class student {
    private String name;
    private Integer age;
    private Integer  id;

}

3.然后生成get和set方法以及tostring方法

复制代码
package org.example.entity;

public class student {
    private String name;
    private Integer age;
    private Integer  id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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


    @java.lang.Override
    public java.lang.String toString() {
        return "student{" +
                "name=" + name +
                ", age=" + age +
                ", id=" + id +
                '}';
    }
}

4.我们可以通过lombok依赖快速自动生成get和set方法,先在pom.xml文件中引入lombok依赖

复制代码
<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
</dependency>

然后在类前注入@Data注解

会自动生成get和set等方法

如果只需要get方法就引入get注解

只需要set方法就引入setter注解

需要无参构造方法就引入NoArgsConstructor注解

需要有参构造方法就引入AllArgsConstructor注解

5.使用lombok必须提前在idea中安装插件

6.然后创建一个text包,创建一个text类,

复制代码
package org.example.test;
import org.example.entity.student;
import java.lang.*;
public class test {
    public static void main(String[] args) {
        student student = new student();
        System.out.println(student);
    }
}

7.运行后结果如下:

小结.以上方法是传统方式创建对象,手动创建对象,

Ioc方式

接下来我们用Ioc方式自动创建对象,开发者只需要取出对象即可

1.首先我们需要一个配置文件,在resources包里面创建spring.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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans            
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context                                                                             
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
                       
                        ...<!--中间xml文件部分-->.....
    </beans>

2.IoC容器通过读取spring.xml配置文件,加载bean标签来创建对象

复制代码
 <bean id="student" class="org.example.entity.student"></bean>

<?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-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <bean id="student" class="org.example.entity.student"></bean>

</beans>

3.调用API获取IoC容器中已经创建的对象

复制代码
public class test {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        student student = (student) applicationContext.getBean("student");
        System.out.println(student);
    }
}
相关推荐
_Kayo_2 小时前
node.js 学习笔记3 HTTP
笔记·学习
IT毕设实战小研5 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
CCCC13101635 小时前
嵌入式学习(day 28)线程
jvm·学习
甄超锋6 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
星星火柴9366 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
小狗爱吃黄桃罐头6 小时前
正点原子【第四期】Linux之驱动开发篇学习笔记-1.1 Linux驱动开发与裸机开发的区别
linux·驱动开发·学习
艾莉丝努力练剑7 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
武昌库里写JAVA8 小时前
JAVA面试汇总(四)JVM(一)
java·vue.js·spring boot·sql·学习
Cx330❀9 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
杜子不疼.9 小时前
《Python学习之字典(一):基础操作与核心用法》
开发语言·python·学习