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

Spring框架的搭建

2016-05-06 13:10 501 查看
零基础搭建Spring框架

需要准备的工具 spring-framework-4.2.3.RELEASE-dist

下载地址:http://maven.springframework.org/release/org/springframework/spring/4.0.2.RELEASE/

另外下载:commons-logging-1.2-bin 文件

下载好之后就可以开始eclipse的配置工作了

1.新建一个Java工程

2.右键工程添加jar包,把spring-framework-4.2.3.RELEASE-dist里面libs目录下的所有jar包全部导入。

另外把commons-logging-1.2-bin里面的两个jar包也要导入。

3.配置Xml文件,文件位置自己放,以src目录为例,在src目录下新建一个Beans.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="helloWorld" class="com.app.springdemo.HelloWorld">

<property name="message" value="Hello World!"/>

</bean>

</beans>

其中class="com.app.springdemo.HelloWorld"就是你测试时候用的bean文件

private String message ;

public void getMessage() {

System.out.println("Your Message:"+message);

}

public void setMessage(String message) {

this.message = message;

}

主函数:

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml") ;

HelloWorld obj = (HelloWorld)context.getBean("helloWorld") ;

obj.getMessage() ;

运行结果:

Your Message:Hello World!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: