spring安装 使用 教程
来源:互联网 发布:淘宝货到付款先发货 编辑:程序博客网 时间:2024/05/21 15:39
一、下载 Spring 下载地址:http://repo.spring.io/libs-release-local/org/springframework/spring/4.0.6.RELEASE/ 下载zip压缩包: spring-framework-4.0.6.RELEASE-dist.zip 并解压。
二、在 Eclipse 呀 myEclipse 中开发 Spring 应用
1. 新建 java project 项目命名为 myspring
2. 为该项目增加 Spring 支持。添加用户库 spring4.0.6 和 common-logging 添加方法如下图:
commons-logging-1.1.3.jar
3. 定义一个 Spring 管理容器中的 Bean (POJO) src\hsl\service\PersonService.java 代码如下:
- package hsl.service;
- public class PersonService {
- private String name;
- // name属性的setter方法
- public void setName(String name) {
- this.name = name;
- }
- // 测试Person类的info方法
- public void info() {
- System.out.println("此人名为:" + name);
- }
- }
注:Spring 可以管理任意的 POJO,并不要求 java 类是一个标准的 JavaBean.
4. 编写主程序,该程序初始化 Spring 容器 src\hsl\SpringTest.java 代码如下:
- package hsl;
- import hsl.service.PersonService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class SpringTest {
- public static void main(String[] args) {
- // 创建Spring的ApplicationContext
- ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
- System.out.println(ctx); // 输出Spring容器
- // 通过 Spring 容器获取 Person 实例,并为 Person 实例设置属性值(这种方式称为控制反转,IoC)
- PersonService p = ctx.getBean("personService", PersonService.class);
- p.info();
- }
- }
5. 将 PersionService 类部署在 Spring 配置文件中, src\bean.xml 代码如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <!-- 将PersonService类部署成Spring容器中的Bean -->
- <bean id="personService" class="hsl.service.PersonService">
- <property name="name" value="wawa"/>
- </bean>
- </beans>
6. 运行主程序,结果如下:
阅读全文
0 0
- spring安装 使用 教程
- ActiveMQ安装使用与spring整合配置教程
- Spring Shiro使用教程
- spring应用程序使用教程
- Pligg安装使用教程
- Vmare安装使用教程
- VMware6安装使用教程
- Yii安装使用教程
- JMEditor安装使用教程
- qwt安装使用教程
- Openshift 安装使用教程
- Jmeter安装使用教程
- QCustomPlot安装使用教程
- HHVM安装使用教程
- mongoDB 安装 使用教程
- Kivy安装使用教程
- winpcap安装使用教程
- Thrift安装使用教程
- Spring JDBC 示例
- 大理石在哪儿 where is the Marble?
- 38. Count and Say
- CSS中的content属性
- 卷积神经网络——ResNet
- spring安装 使用 教程
- Linux中select IO复用机制
- Maximum Subarray (算法分析week2)
- #正念奇迹#ZQ0008《人类简史:从动物到上帝》
- 如何向整个 Git 仓库补提交一个文件
- linux svn使用实例(待完善)
- jetty启动项目时,BUILD FAILURE,程序包com.sun.org.apache.xml.internal.security.utils不存在
- #正念奇迹#ZQ0007《人类简史:从远古到二十一世纪》
- PHP下使用多线程