Spring(二) 初识Spring HelloWorld

本文详细介绍了一个简单的Spring框架应用实例,包括必要的7个核心jar包导入、HelloSpring类编写、Spring XML配置文件设置及如何通过测试类加载XML文件验证类实例化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.项目总体概要
1)导入Spring的7个核心jar包
2)编写java类
3)配置Spring的xml文件,实例化java类
4)加载Spring的xml文件,测试java类是否被实例化
hellospring 的总体概要

2. 导入Spring的7个核心jar包
核心jar包我在百度云上面已经进行了分享,可以自行下载
https://pan.baidu.com/s/1i4Cqr6t

3. 编写java类
编写HelloSpring类

/**
 * 第一个测试Spring的例子
 * @author 49882
 *
 */
public class HelloSpring {
    public void sayHello() {
        System.out.println("hello Spring");     
    }

}

4. 配置Spring的xml文件,实例化java类
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">

<!-- 实例化了一个类的对象 hellospring -->
<bean id="hellospring" class="com.hlf.spring.HelloSpring"></bean>

</beans>

5. 加载Spring的xml文件,测试java类是否被实例化
测试类

package com.hlf.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hlf.spring.HelloSpring;

public class testHelloSpring {

    public static void main(String[] args) {

        //1 加载 spring.xml 
        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");

        //2 得到配置文件中实例化的类的对象
        HelloSpring hs= (HelloSpring) context.getBean("hellospring");

        //3 调用该对象的方法
        hs.sayHello();

    }

}

执行结果
测试的执行结果
6.Spring4_01的项目下载
实例下载 https://pan.baidu.com/s/1hsOJNyk

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值