spring之Holle World项目

1、在IDEA中新建一个java项目,在里面新建lib包和在src下新建com包和cxt包(包名随便)。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GToucR9K-1593671552819)(spring学习.assets/image-20200702112623706.png)]

2、spring官网上下载最新的spring框架,复制进lib中

在这里插入图片描述

3、添加必需的库

在这里插入图片描述

4、编写Bean文件,xml文件,main文件

Bean文件:

package com.bean;

public class TestDemo {
    private String name;
    private String msg;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMsg() {
        System.out.println("测试执行:"+msg);
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

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="testDemo" class="com.bean.TestDemo"></bean>
</beans>

main文件:通过ApplicationContext接口的方式

package com.cxt;

import com.bean.TestDemo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Spring_demo1 {
    public static void main(String[] args) {
        // 通过xml名字加载bean
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        TestDemo testDemo = context.getBean("testDemo", TestDemo.class);
        testDemo.setMsg("这是一个测试的demo");
        testDemo.getMsg();

        // 通过路径加载Bean,地址是绝对地址
        FileSystemXmlApplicationContext con = new FileSystemXmlApplicationContext("D:/workspace/Spring_demo1/src/bean1.xml");
        TestDemo testDemo1 = con.getBean("testDemo", TestDemo.class);
        testDemo1.setMsg("通过地址");
        testDemo1.getMsg();
    }
}
5、程序运行结果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值