Spring---HelloWord

导包:spring-webmvc

<dependencies>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.18</version>
        </dependency>


    </dependencies>

pojo:

package com.kuang.pojo;

public class Hello {

private  String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}

spring核心文件bean.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-3.0.xsd">


    <!--spring 创建对象称为beans,Bean
id=对象名
class=new 对象
property:属性设置-->

    <bean id="word" class="com.kuang.pojo.Hello">

        <property name="str" value="spring属性值"/>

    </bean>
</beans>

测试类:

import com.kuang.pojo.Hello;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
/**
 作者
 时间
 类的描述
 ***/


public static void main(String[] args) {
//    获取spring上下文
    BeanFactory context = new ClassPathXmlApplicationContext("beans.xml");
    Hello hello =(Hello) context.getBean("word");
    System.out.println(hello.toString());
}
}

输出:
在这里插入图片描述
1、在spring创建对象,在spring中被称为bean
2、java创建对象:

对象 对象名=new 对象();

3、spring中创建对象:

Bean
id=对象名
class=new 对象
property:属性设置

所谓的Ioc就是spring来创建对象、管理、装配

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值