zoukankan      html  css  js  c++  java
  • 第一个Spring Demo

    1、Main文件

    package com.pb;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**   
    *    
    * 项目名称:PB_SpringDemo   
    * 类名称:HellpSpring   
    * 类描述:   第一个Spring项目
    * 创建人:Administrator   
    * 创建时间:2019年7月6日 上午7:23:43   
    * 修改人:Administrator   
    * 修改时间:2019年7月6日 上午7:23:43   
    * 修改备注:   
    * @version    
    *    
    */
    public class HelloSpring {
    
        //需要注入的属性,这个名字跟Bean里的没有任何关系
        private String input_str=null;
        
        /*
         * 注意点1:依赖注入的是靠 get和set方法的名字来确认的,比如本例子中是getMyStr和setMyStr,那么Bean里的属性名字就必须配置为myStr,否则出错
         * 注意点2:Bean的属性名字必须是首字母小写,如本例中是 myStr,不能写成MyStr,否则报[Invalid property 'MyStr' of bean class [com.pb.HelloSpring]: No property 'MyStr' found]
         * 注意点3:get和set方法必须对应起来,不能是这样 getMYStr和setMyStr,大小写不一致也会出错
         */
        public String getMyStr() {
            return this.input_str;
        }
        public void setMyStr(String strParam) {
            this.input_str=strParam;
        }
        
        public void Print()
        {
            System.out.println("Hello,"+this.getMyStr());
        }
        public static void main(String[] args) {
            // 创建Spring上下文
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            
            //获取bean的实例
            HelloSpring helloSpring=(HelloSpring)context.getBean("myFirstSpringDemo");
            helloSpring.Print();
      
        }
    
    }

    2、applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    <beans>
        <bean id="myFirstSpringDemo" class="com.pb.HelloSpring">
            <property name="myStr">
                <value>我是Spring</value>
            </property>
        </bean>
    </beans>

     运行效果

    3、注意点


     * 注意点1:依赖注入的是靠 get和set方法的名字来确认的,比如本例子中是getMyStr和setMyStr,那么Bean里的属性名字就必须配置为myStr,否则出错
     * 注意点2:Bean的属性名字必须是首字母小写,如本例中是 myStr,不能写成MyStr,否则报[Invalid property 'MyStr' of bean class [com.pb.HelloSpring]: No property 'MyStr' found]
     * 注意点3:get和set方法必须对应起来,不能是这样 getMYStr和setMyStr,大小写不一致也会出错

  • 相关阅读:
    poj 3321 Apple Tree
    hdu 1520 Anniversary party
    Light OJ 1089 Points in Segments (II)
    Timus 1018 Binary Apple Tree
    zoj 3299 Fall the Brick
    HFUT 1287 法默尔的农场
    Codeforces 159C String Manipulation 1.0
    GraphQL + React Apollo + React Hook 大型项目实战(32 个视频)
    使用 TypeScript & mocha & chai 写测试代码实战(17 个视频)
    GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频)
  • 原文地址:https://www.cnblogs.com/schangxiang/p/11141587.html
Copyright © 2011-2022 走看看