·Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。

· Spring下载:spring的官网改版之后,就不支持直接下载了,可以在这里点击下载。

·下载当前最新版本(4.3.1.RELEASE)后,可以看到目录如下:

|-docs :文档和api

|-libs:jar包

|-schema :不懂(笑)

·spring需要依赖apache的log包(commons-logging-1.2-bin) ,可以在官网下载。

·现在就可以建立第一个java-spring程序了。

① 首先定义一个学生类

package com.fuwh.spring;

public class Student {
    
    private String name;
    private int age;     public Student() {
        System.out.println("类被初始化");
        // TODO Auto-generated constructor stub
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
    

}

②在classpath下定义一个配置文件bean.xml(名字可以随便取)。

1 <?xml version="1.0" encoding="UTF-8"?>

 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <!-- services -->
 8     
 9     <bean id="student" class="com.fuwh.spring.Student" lazy-init="default">
         <property name="name" value="fengzi"></property>
         <property name="age" value="23"></property>
     </bean>
     
 </beans>

③ 编写测试类 1 package com.fuwh.spring;

 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Spring01 {
 7 
 8     public static void main(String[] args) {
 9         
         ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
         System.out.println(ac.getBean("student"));
     }
 }

这样一个基于spring的程序就完成了,spring程序和普通程序不一样的是,不再需要new一个对象,而是将对象的创建交给了spring容器,也就是所谓的Ioc。

最新文章

  1. C#----GDI+画图的一些注意和细节
  2. Ubuntu14.04配置记录
  3. Kraken taxonomic sequence classification system
  4. 用Javascript实现回到顶部效果
  5. 定制CentOS (Redhat AS 5.1)安装盘
  6. SharePoint 2010 的企业级搜索技术文章
  7. OpenStack Havana 部署在Ubuntu 12.04 Server 【OVS+GRE】——序
  8. UOJ 218 火车管理
  9. ANTLR
  10. Can&#39;t connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39; (2)
  11. PHP通过phpmailer批量发送邮件功能
  12. IntelliJ IDEA javaDoc的使用
  13. C#操作防火墙控制电脑某些软件联网
  14. virsh命令来创建虚拟机
  15. 补习系列(10)-springboot 之配置读取
  16. 既然写CSS很容易,那为什么大家还是把CSS写的那么烂呢?
  17. centos 命令和
  18. 树莓派ssh服务
  19. 微信小程序入门实例之记事本
  20. Android利用ViewPager实现滑动广告板

热门文章

  1. 【代码笔记】iOS-获得当前硬盘空间
  2. mac maven
  3. SE Springer小组《Spring音乐播放器》软件需求说明3
  4. ESLint规则配置说明
  5. javascript的ajax
  6. ssh无密码登录The authenticity of host &#39;localhost (::1)&#39; can&#39;t be established.
  7. jmeter(九)逻辑控制器
  8. 安全测试 - 抓包工具BurpSuite
  9. Parseval&#39;s theorem 帕塞瓦尔定理
  10. CWMP开源代码研究5——CWMP程序设计思想