实现一个简单的Spring实例

来源:互联网 发布:淘宝联盟导购名称 编辑:程序博客网 时间:2024/05/10 14:23

 代码:

1.配置文件

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-2.5.xsd"><bean id="hw" class="com.springtest1.HelloWorld">   </bean></beans>

2.源代码

 HelloWorld.java

package com.springtest1;public class HelloWorld {    //定义方法show,在控制台输出信息public void show(){System.out.println("欢迎学习Spring框架");}}

TestHelloWorld.java

package com.springtest1;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestHelloWorld {public static void main(String[] args) {//加载applicationContext.xml配置ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//获取配置中的实例HelloWorld hw=(HelloWorld) context.getBean("hw");//调用方法hw.show();}}

截图:



0 0