zoukankan      html  css  js  c++  java
  • 第一个Spring程序(ApplicationContext 容器)

    首先创建两个类,HelloWorld.java和MainApp.java

    HelloWorld.java用来创建具体的类对象,

    MainApp.java用来执行操作。

    然后要创建xml的配置文件映射spring框架和项目。

    当然,首先要把spring框架的lib导入项目中。

    以下是三个文件的具体代码:

    package test;
    
    public class HelloWorld {
    	private String message;
    
    	public String getMessage() {
    		return message;
    	}
    
    	public void setMessage(String message) {
    		System.out.println("Your Message :"+message);
    	}
    	
    
    }
    

      

    package test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainApp {
    	public static void main(String[] args) {
    		//通过xml文件生成bean
    		ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");//通过配置classpath确定的xml文件位置
    		HelloWorld obj = (HelloWorld)context.getBean("helloworld");//根据bean的id获取bean,再强转为要操作的类型
    		obj.getMessage();
    	}
    
    }
    

      

      

    <?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="helloworld" class="test.HelloWorld">
           <property name="message" value="你好世界!"/>
       </bean>
    
    </beans>
  • 相关阅读:
    分享一下前一段时间的开发总结
    循环,梦
    从C#程序中调用非受管DLLs
    大学生零工资就业,谁之过?
    国外宽带用户的上网速度能达到多少呢?
    天沉沉,来个好天气吧
    虚伪,不只是形容一个人
    回头思考关于xml的使用
    从毕业生当中看人与人的差距
    C# 编码规则
  • 原文地址:https://www.cnblogs.com/ssC2H4/p/8533401.html
Copyright © 2011-2022 走看看