zoukankan      html  css  js  c++  java
  • 【Spring】第一个hello world程序

    我们先写一个类 里面有个字符串

    public class Hello {
    
        private String str;
    
        public String getStr() {
            return str;
        }
    
        public void setStr(String str) {
            this.str = str;
        }
    
        @Override
        public String toString() {
            return "Holle [str=" + str + "]";
        }
    }

    注意里面的set方法 非常重要

    然后我们用spring的配置文件进行类的注册

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
        <!--在Spring中创建对象,在Spring这些都称为bean
            类型 变量名 = new 类型();
            Holle holle = new Holle();
    
            bean = 对象(holle)
            id = 变量名(holle)
            class = new的对象(new Holle();)
            property 相当于给对象中的属性设值,让str="Spring"
        -->
    
        <bean id="hello" class="com.lei.pojo.Hello">
            <property name="str" value="Spring"/>
        </bean>
    </beans>

    可以看到最后那几行是非常重要的 我们声明了一个bean 名字叫hello 类是来自com lei pojo 的Hello类

    然后此实例对象的字段str为"Spring“(这里使用到了set方法)

    我们进行测试一下

    public class HelloTest extends TestCase {
        @Test
        public void test(){
            //获取Spring的上下文对象
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            //我们的对象下能在都在spring·中管理了,我们要使用,直接取出来就可以了
            Hello holle = (Hello) context.getBean("hello");
            System.out.println(holle.toString());
        }
    }

    分享一个好用的快捷键(Crtl+H)查看继承关系图

  • 相关阅读:
    smtplib.py
    淘宝链接中的spm参数
    with 上下文管理
    python RecursionError: maximum recursion depth exceeded while calling
    GraphQL两年实战
    Exception 异常处理
    Simple decorator that intercepts connection errors and ignores these if settings specify this.
    namedtuple
    服务治理在猫眼娱乐的演进之路
    路由、限流、熔断 微服务治理
  • 原文地址:https://www.cnblogs.com/cckong/p/14359241.html
Copyright © 2011-2022 走看看