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!
需要准备的工具 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!
相关文章推荐
- 常见快捷键eclipse
- spring 学习
- 2、Ajax与Java通过POST方式交互
- java底层知识(4)--伪共享
- Windows7配置Java环境变量
- Java 多线程
- springmvc Rest 风格的 CRUD
- Java代码实现SHA1加密
- Springmvc + FastJson
- SpringMVC 整合fastjson
- Java类的加载过程
- 出现的问题 和 错误java.io.IOException: tmpFile.renameTo(classFile) failed
- Storm Spark Scala 混合代码快速编译打包jar方式,然后java风格使用(朋友咨询)
- java 后台 Excel 文件生成后转化为字节流
- java.lang.OutOfMemoryError: PermGen space及其解决方法(转)
- 《java入门第一季》之面向对象(多态练习)
- 《java入门第一季》之面向对象(多态练习)
- 【图解JDK源码】HashMap的容量大小增长原理(JDK1.6/1.7/1.8)
- 初识Spring-MVC之DispatcherServlet详解
- 设置Myeclipse中的代码格式化、注释模板及保存时自动格式化