1、创建一个新的项目,并添加Spring框架

Spring框架学习笔记(1)——HelloWorld

Spring框架学习笔记(1)——HelloWorld

Spring框架学习笔记(1)——HelloWorld

Spring框架学习笔记(1)——HelloWorld

Spring框架学习笔记(1)——HelloWorld

2、创建HelloWorld.java

package com.broadtext.beans.helloworld;

public class HelloWorld {
    private String name;

    public HelloWorld() {
    }

    @Override
    public String toString() {
        return "HelloWorld [name=" + name + "]";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

3、修改applicationContext.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="helloWorld" class="com.broadtext.beans.helloworld.HelloWorld">
        <property name="name" value="hjj"></property>
    </bean>

</beans>

这时候HelloWorld.java文件上会多出'S'标记

Spring框架学习笔记(1)——HelloWorld

4、添加Main.java和Main方法测试HelloWorld bean

package com.broadtext.beans.helloworld;

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

public class Main {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
        System.out.println(helloWorld);
    }

}

5、运行,看看控制台打印的HelloWorld

Spring框架学习笔记(1)——HelloWorld

 

相关文章:

  • 2021-04-19
  • 2022-03-02
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
  • 2021-09-29
猜你喜欢
  • 2022-03-05
  • 2021-11-05
  • 2021-11-02
  • 2021-12-20
  • 2021-10-23
  • 2021-06-19
  • 2021-07-03
相关资源
相似解决方案