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


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

    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'标记

    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

  • 相关阅读:
    Pythoy 数据类型序列化——json&pickle 模块
    Python xml 模块
    Spring MVC <mvc:annotation-driven/>的作用
    Spring MVC学习笔记
    springboot配置logback日志
    Git master合并分支时提示“Already up-to-date”
    解决idea tomcat乱码问题
    MYSQL 八大优化方案
    SpringBoot项目集成PageHelper使用
    Git--远程仓库版本回退方法
  • 原文地址:https://www.cnblogs.com/huangjian2/p/6101687.html
Copyright © 2020-2023  润新知