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);
    }
}
相关推荐
chenchihwen7 分钟前
大模型应用班-第2课 DeepSeek使用与提示词工程课程重点 学习ollama 安装 用deepseek-r1:1.5b 分析PDF 内容
人工智能·学习
超浪的晨26 分钟前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
心之语歌1 小时前
Spring AI MCP 客户端
人工智能·spring·github
Edingbrugh.南空1 小时前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring
使二颗心免于哀伤2 小时前
《设计模式之禅》笔记摘录 - 10.装饰模式
笔记·设计模式
悠哉悠哉愿意2 小时前
【电赛学习笔记】MaxiCAM 项目实践——与单片机的串口通信
笔记·python·单片机·嵌入式硬件·学习·视觉检测
快乐肚皮3 小时前
ZooKeeper学习专栏(五):Java客户端开发(原生API)详解
学习·zookeeper·java-zookeeper
慕y2743 小时前
Java学习第七十二部分——Zookeeper
java·学习·java-zookeeper
岩中竹3 小时前
广东省省考备考——常识:科技常识(持续更新)
笔记
★YUI★3 小时前
学习游戏制作记录(剑投掷技能)7.26
学习·游戏·unity·c#