浅析Spring框架的搭建

Spring是什么

Spring是一个容器框架,用于配置bean并且维护bean之间关系的框架。

Spring的结构图:

搭建Spring框架步骤

  1. 下载Spring开发包,Spring-4.3.4完整开发包下载。
  2. 在JavaProject下面建立一个lib目录,将开发包下面的libs目录里的所有以spring开头的jar文件,和commons-logging文件都拷贝到新建的lib里面。
  3. 将拷贝的文件全部选中,然后添加到Build path里。

Spring-4.3.4完整开发包下载地址

到Linux公社1号FTP服务器下载

------------------------------------------分割线------------------------------------------

在 2016年LinuxIDC.com\12月\浅析Spring框架的搭建\

------------------------------------------分割线------------------------------------------ 

简单Demo

我的结构目录:

  1. 建立一个User类

    package com.server;
    
    public class User {
      private String name;
    
    public void getName() {
        System.out.println("Hello "+name);
    }
    
    public void setName(String name) {
        this.name = name;
    }
    } 
  2. 建立一个Test类

    package com.test;
    
    /*
     *引入ApplicationContext和ClassPathXmlApplication 
     */
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.server.User;
    
    public class Test {
    
        public static void main(String[] args) {
    
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            User user = (User)ac.getBean("user");//根据Bean的id来识别
            user.getName();
        }
    
    }

    ApplicationContext是一个接口,由ClassPathXmlApplicationContext寻找applicationContext这个文件,在开发包里一个index.html的网页文件,读者可以在里面查找相应Spring版本的API文档。

    ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  3. 建立applicationContext文件

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
        xmlns:cache="http://www.springframework.org/schema/cache"  
        xsi:schemaLocation="  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx.xsd  
        http://www.springframework.org/schema/jdbc  
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
        http://www.springframework.org/schema/cache  
        http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
        http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop.xsd  
        http://www.springframework.org/schema/util  
        http://www.springframework.org/schema/util/spring-util.xsd">
        
        <!-- 在容器文件中配置bean(service/dao.domain/action/数据原); -->
        
        <!-- bean的作用是,当我们的Spring框架加载的时候,spring就会自动创建一个bean对象,并且放入内存。id是标识符,class指定路径
              本例相当于:
                 User user=new User();
                 user.setName(""chauncy);        
         -->
        <bean id="user" class="com.server.User">
          <property name="name">
            <value>chauncy</value>
          </property>
        </bean>
    </beans>

猜你喜欢

转载自www.linuxidc.com/Linux/2016-12/138821.htm
今日推荐