您的位置:首页 > 编程语言 > Java开发

Spring 1 利用idea搭建spring hello world程序

2017-12-12 00:05 197 查看
参考http://blog.csdn.net/cflys/article/details/70598903

1 HelloWorld.java

public class HelloWorld {
private String name;

public void setName(String name){
this.name = name;
}

public void sayHello(){
System.out.println("Hello" + name);
}
}


Main.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
helloWorld.sayHello();
}


spring-config.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.xsd"> 
<bean id="helloWorld" class="HelloWorld">
<property name="name" value="Spring"></property>
</bean>
</beans>


利用spring来对对象进行创建和管理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring idea helloworld