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

第一个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情况下也可以通过类型注入

 

 

 


http://www.kler.cn/a/594116.html

相关文章:

  • 使用C#创建安装Windows服务程序
  • 蓝桥杯十天冲刺-day1(日期问题)
  • 软考笔记——程序设计语言基础知识
  • Git 回退操作详解:带示例的“小白”指南
  • 编译构建google R8源码
  • 谈谈最近AI在我工作生活中的深度应用
  • JVM常用概念之对象对齐
  • Linux目录操作学习总结
  • squirrel语言全面介绍
  • LeetCode[242]有效的字母异位词
  • SpringBoot 第二课(Ⅰ) 整合springmvc(详解)
  • NO.51十六届蓝桥杯备战|堆算法题|第k小|除2|最小函数值|序列合并|舞蹈课(C++)
  • K8S学习之基础四十:K8S配置altermanager发送告警到钉钉群
  • SpringCache @Cacheable 在同一个类中调用方法,导致缓存不生效的问题及解决办法
  • Springboot项目搭建(9)-分页与文件上传
  • 论文阅读——高光谱与多光谱图像融合:通过自监督表示实现任意分辨率
  • 用爬虫解锁 Shopee 店铺商品数据,开启电商新洞察
  • Thinkphp指纹识别
  • HarmonyOS Next~鸿蒙系统架构设计解析:分层、模块化与智慧分发的技术革新
  • POI和EasyExcel---处理Excel