zoukankan      html  css  js  c++  java
  • Spring-搭建

    1.下载

    ① Spring的下载

    Spring下载地址:http://repo.springsource.org/libs-release-local/org/springframework/spring

    本次下载的是:4.0.4

    目前最新版本4.3.0

    ② commons-logging的下载

    Spring依赖Apache的logging;

     

    http://commons.apache.org/proper/commons-logging/download_logging.cgi

    搭建HelloWorld;

    package com.spring.test;
    /**
     * 学生类
     */
    public class Student {
    
        public void getName(){
            System.out.println("学生的NAME");
        }
    }
    package com.spring.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    public class Main {
        public void t(){
            /**
             * ApplicationContext:接口,应用的上下文;
             * 常用的实现类:
             *         1.ClassPathXmlApplicationContext:从类加载路径下搜索配置文件,及src下;
             *         2.FileSystemXmlApplicationContext:从文件系统的相对路径或绝对路径下查找;默认在该路径下[E:workworkspaceSpring_7.3eans.xml];
             *         3.XmlWebApplicationContext:
             */
            
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            Student stu1 = (Student) applicationContext.getBean("student");//获得bean的第1种方法,需要强制转换
            Student stu =  applicationContext.getBean("student",Student.class);//获得bean的第2种方法,无需强制装换 stu.getName(); }
    public static void main(String[] args) { Main m = new Main(); m.t(); } }
    【输出】
    七月 15, 2016 10:06:44 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6c069ec: startup date [Fri Jul 15 22:06:44 CST 2016];……
    七月 15, 2016 10:06:44 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [beans.xml]
    学生的NAME
    <?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.1.xsd">
                            
                            
        <bean name="student" class="com.spring.test.Student"> </bean>
    
    
    
    </beans>

    ApplicationContext:接口,应用的上下文;继承BeanFactory;

    其有两个主要的实现类:

    1.ClassPathXmlApplicationContext:从类加载路径下搜索配置文件,即src下;

    2.FileSystemXmlApplicationContext:从文件系统的相对路径或绝对路径下查找;

     

  • 相关阅读:
    工厂对象模式简介
    (转)HelloWorld CMake CMake中构建静态库与动态库及其使用
    C和C++混合编程
    Google glog 使用
    VS2013 越来越慢
    shell 的语法
    (十二)命令模式详解(故事版)
    (十一)外观模式详解(Service第三者插足,让action与dao分手)
    (十)装饰器模式详解(与IO不解的情缘)
    (九)模板方法模式详解(包含与类加载器不得不说的故事)
  • 原文地址:https://www.cnblogs.com/devan/p/5674894.html
Copyright © 2011-2022 走看看