zoukankan      html  css  js  c++  java
  • Spring入门

    IMessage.java

    package com.umgsai.spring;
    public interface IMessage {//定义接口
        public abstract String getMessage();
    }

    Message.java

    package com.umgsai.spring;
    public class Message implements IMessage {
        public String getMessage() {
            return "Hello world!";
        }
    }

    Hello.java

    package com.umgsai.spring;
    public class Hello implements IMessage{
        public String getMessage() {
            return "你好!";
        }
    }

    类路径下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"
        xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
                                    
        <bean id="a1" class="com.umgsai.spring.Message"/>
        <bean id="a2" class="com.umgsai.spring.Hello"/>
    </beans>

    MainClass.java

    MainClass.java
    package com.umgsai.spring;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainClass {
        public static void main(String[] args) {
            //IMessage message = MessageFactory.create("1");
            //创建spring Bean工厂
            BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
            //BeanFactory factory = new ClassPathXmlApplicationContext("app*.xml");通配符
            IMessage message = (IMessage)factory.getBean("a1");
            System.out.println(message.getMessage());
            message = (IMessage)factory.getBean("a2");    
            System.out.println(message.getMessage());
        }
    }


    运行,控制台输出结果

    Hello world!

    你好!




    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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
          
        xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
                         
        <bean id="a1" class="com.umgsai.spring.Message"/>
    </beans>



    本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1253534

    积跬步以致千里,积小流以成江海。
    2016年5月之前的博文发布于51cto,链接地址:shamrock.blog.51cto.com
    2016年5月之后博文发布与cnblogs上。
    Github地址 https://github.com/umgsai
    Keep moving~!!!
  • 相关阅读:
    MySQL开发索引创建规范
    如何从传统软件开发顺利过渡到互联网技术开发
    Java中return返回结果的优先级
    mongodb c++ driver 2.0编译使用
    Android MediaScannerJNI源代码具体解释
    构建你的长寿命的API第1部分:规范驱动的API开发
    CoreImage 中的模糊滤镜
    C++编程技巧降低编译时间
    我们年轻,阳光免费
    验证时出错。HRESULT = &#39;8000000A&#39;
  • 原文地址:https://www.cnblogs.com/umgsai/p/3908118.html
Copyright © 2011-2022 走看看