spring HelloWorld!

来源:互联网 发布:传奇npc算法 编辑:程序博客网 时间:2024/06/05 23:58

spring会自动帮你管理你的类的生命周期


创建第一个spring例子


导入相关的jar包(spring.jar  commons-logging.jar)(http://pan.baidu.com/s/1pKQYwqv)


HelloWorld.java

public class HelloWorld {public void hello(){System.out.println("hello world");}}

在src目录下创建一个spring的配置文件

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-2.5.xsd"><!-- 将一个类放进spring容器中  id值得唯一的--><bean id="helloWorld" class="com.mo.entity.HelloWorld"> </bean>           </beans>

测试

public class HelloWorldTest {@Testpublic void testHello(){//加载配置文件,启动spring容器ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//调用getbean方法,spring自动帮你创建一个对象返回HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");helloWorld.hello();}}

打印输出
hello world



0 0