当前位置: 首页 > news >正文

第一个Spring程序基于Spring6

第一步创建模块然后导入maven依赖,编写pom.xml文件。

<packaging>jar</packaging>

设置打包方式

<repositories>
    <repository>
        <id>repository.spring.milestone</id>
        <name>Spring Milestone Repository</name>
        <url>https://repo.spring.io/milestone</url>
    </repository>
</repositories>

引入spring仓库

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

引入测试依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>6.1.14</version>
</dependency>

引入spring context依赖

第二部编写spring.xml文件

在这里可以直接生成Spring.xml文件

然后配置好bean

    <bean id="user" class="com.bac.jinngh.User">
        <property name="vip" ref="vip"/>
    </bean>

    <bean id="vip" class="com.bac.jinngh.Vip"/>

第三步写好实体类 

package com.bac.jinngh;

public class User {

    private Vip vip;


    public User() {
        System.out.println("user的构造方法运行了");
    }

    public Vip getVip() {
        return vip;
    }

    public void setVip(Vip vip) {
        this.vip = vip;
    }
}

package com.bac.jinngh;

public class Vip {

    public Vip() {
        System.out.println("vip的构造方法运行了");
    }
}

 第四步写测试代码

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring2.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
        System.out.println(user.getVip());
    }

 运行可以看到我们的User和Vip对象都创建好了

我们来分析可以看到我们运行了

ApplicationContext context = new ClassPathXmlApplicationContext("spring2.xml");

的时候对象已经被创建出来了

所以可以得出我们的ApplicationContext context = new ClassPathXmlApplicationContext("spring2.xml");这个代码运行就会扫描spring.xml文件并且把里面的bean创建。

然后我们的User里面的Vip属性也可以被创建它是通过set注入创建的,我们把set去掉user里面的vip就是空的

通过构造方法注入

cat类

package com.bac.user;

public class Cat {

    private Catim catim;

    public Cat(Catim catim) {
        this.catim = catim;
    }

    public Catim getCatim() {
        return catim;
    }

    public Cat() {
    }
}

Catim类 

package com.bac.user;

public class Catim {
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="catim" class="com.bac.user.Catim"/>

    <bean id="cat" class="com.bac.user.Cat">
        <constructor-arg index="0" ref="catim"/>
    </bean>


</beans>

测试代码

    @Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring3.xml");
        Cat cat = context.getBean("cat", Cat.class);
        System.out.println(cat.getCatim());
    }

  

可以看到我们通过构造方法把Catim对象注入了 

如果构造方法有多个参数可以在后面加 

   <bean id="cat" class="com.bac.user.Cat">
        <constructor-arg index="0" ref="catim"/>

在这里加<constructor-arg index="" ref=""/>标签
    </bean>

可以通过下标 可以通过参数名 也可以不指定下标和参数名,可以类型⾃动推断所以我们可以不用写index情况下也可以通过类型注入

 

 

 

相关文章:

  • ROS从小白到入门:知识点速通与面试指南
  • CatFlag 原理
  • 密码学(Public-Key Cryptography and Discrete Logarithms)
  • Docker 部署 XXL-JOB
  • 粘包半包以及Netty的解决办法
  • HCITool 的详细介绍、安装指南及使用说明
  • 合批Batching
  • MySQL 中利用 mysql.help_topic 实现行转列的深入剖析
  • 物理标签与逻辑标签的区别
  • 第七节 MATLAB数据类型
  • Pytorch使用手册—自定义 C++ 和 CUDA 扩展(专题五十二)
  • Altium Design元件管理笔记
  • PolyBench基准程序详解:编译器优化评测指标
  • IDEA 出现 Cannot access aliyunmaven in offline mode 问题解决方案
  • 【愚公系列】《高效使用DeepSeek》020-专业术语解释
  • 脚本语言 Lua
  • K8S学习之基础四十:K8S配置altermanager发送告警到钉钉群
  • 在麒麟系统(基于Ubuntu或Debuntu)的离线环境中创建本地APT仓库
  • 基于django美团美食销售数据分析与可视化系统设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
  • Redis 本地安装
  • 沙青青评《通勤梦魇》︱“人机组合”的通勤之路
  • 将人工智能送上太空,我国太空计算卫星星座成功发射
  • 《克莱默夫妇》导演罗伯特·本顿去世,终年92岁
  • 视频丨美国两名男童持枪与警察对峙,一人还试图扣动扳机
  • 有关部门负责人就《新时代的中国国家安全》白皮书答记者问
  • 教育部基础教育教指委:小学阶段禁止学生独自使用开放式内容生成功能