1 Spring4 之环境搭建和HelloWorld
1 Spring 是什么?
|
Spring模块图:
Eclipse开发Spring时需要安装spring插件:springsource-tool-suite-3.6.3.RELEASE-e4.4.1-updatesite.zip(下载链接:http://spring.io/tools/sts/all 需要FQ)。
2 安装 Eclipse插件
Eclipse—->help—> install new software : 点击Add按钮 ,再点击Archive 选择你刚刚下载的zip文件,这里选择 后面带 spring IDE的四项,并把Contact all updatesite….前面的勾去掉(这个是联网更新),一直点点击 下一步, 最后重启eclipse即可
3 搭建Spring开发环境
新建Java工程 :命名spring-1;并新建Folder 命名为lib(放Spring基本的jar包)
在lib中导入:commons-logging-1.1.3.jar在struts的lib中可以找到,其余四个都在spring-framework-4.1.2.RELEASE的libs文件夹下,记得将lib下的jar包add Build Path;
4 项目结构图:
其中HelloWorld.java
package com.bai.spring.beans; public class HelloWorld { private String name; public void setName(String name){ this.name=name; } public void hello(){ System.out.println("hello:"+name); } } |
在src下新建spring配置文件,命名为applicationcontext.xml:
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.xsd"> <!-- 配置bean --> <bean id="helloworld" class="com.bai.spring.beans.HelloWorld"> <property name="name" value="Spring"></property> </bean> </beans> |
Main.java
package com.bai.spring.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String []args){ /* 不使用springde 情况 * //创建一个HelloWorld对象 HelloWorld helloworld=new HelloWorld(); //为对象赋值 helloworld.setName("baixl"); //调用hello()方法 */ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationcontext.xml"); HelloWorld helloworld=(HelloWorld) ctx.getBean("helloworld"); helloworld.hello(); } } |
运行结果:
最新文章
- div盒子垂直水平居中
- jdb - The Java Debugger
- sphinx :undefined reference to `libiconv&#39; 报错解决办法
- Cach&#233;数据库学习笔记(4)
- jquery/js实现验证聚焦,失焦
- simplify the design of the hardware forming the interface between the processor and thememory system
- mysql 字段编码该为utf8mb4
- mq_send
- Android子线程更新UI的方法总结
- DJANGO不同应用之间的用户迁移
- 盛希泰:办公室就像男人的春药——人的一生的精力是有限的,你把有限的时间分配给谁决定你的成败——你有N多选择,你人生的积累就是N多选择加起来的结果
- C#_传单小子
- hdu1443 Joseph---约瑟夫环
- MySQL-5.6.36-部署安装(编译版)
- Python调用ansible API系列(一)获取资产信息
- 杭电ACM2006--求奇数的乘积
- 重置sqlserver自增长列的种子
- 设计模式のBridgePattern(桥接模式)----结构模式
- [JDK8] Lambda
- Manjaro使用笔记-使用中国源的方法