zoukankan      html  css  js  c++  java
  • 【spring教程之中的一个】创建一个最简单的spring样例

    1、首先spring的主要思想,就是依赖注入。简单来说。就是不须要手动new对象,而这些对象由spring容器统一进行管理。


    2、样例结构


    如上图所看到的,採用的是mavenproject。


    2、pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>SpringExample001</groupId>
      <artifactId>SpringExample001</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <dependencies> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-context</artifactId> 
            <version>3.2.5.RELEASE</version> 
        </dependency> 
    </dependencies>
    </project>

    3、spring.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="do" class="com.test.pro.Do"/> </beans>


    4、Do.java

    package com.test.pro;
    
    public class Do {
    	public void speaking()
    	{
    		System.out.println("speaking.......");
    	}
    }
    

    5、測试类

    package com.test.pro;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
    		Do did=(Do)ctx.getBean("do");
    		did.speaking();
    		
    	}
    
    }
    


    6、输出



    7、分析

    我们能够看到,在核心的spring配置文件里的spring.xml中仅仅有一句话:<bean id="do" class="com.test.pro.Do"/>,这句话指明的是有一个bean文件。名称为do,其类的地址是com.test.pro.Do。

    然后就是我们的測试类里面的一段话:

    ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
    Do did=(Do)ctx.getBean("do");
    did.speaking();

    这里表示声明一个上下文类,这个上下文类装载了配置文件,注意,假设这里不是採用mavenproject的话,一定要注意spring.xml的相对地址。假设实在不确定相对地址是什么。能够採用绝对地址的方式。比如:

    ApplicationContext ctx=new ClassPathXmlApplicationContext("file:H:/spring.xml");

    然后就是利用上下文对象来获得在配置文件里声明过的bean的演示样例,而且能够直接调用。

    那么这个bean文件是什么时候实例化的,假设bean的scope是prototype的,则该Bean的实例化是在第一次使用该Bean的时候进行实例化 ,能够參考这篇文章:http://www.iteye.com/problems/93479


  • 相关阅读:
    (转)视频编码标准汇总及比较
    (转)live555从RTSP服务器读取数据到使用接收到的数据流程分析
    (转)MPEG4码流简单分析
    H264裸流分析中,能获取哪些信息?
    (转)基于live555的流媒体代理转发服务器
    测试x264编码器的低延时编码和非延时编码
    ELK 日志分析系统
    Dubbox:来自当当网的SOA服务框架
    CHMOD命令怎么用?
    linux显示文件列表命令ls,使用ls --help列出所有命令参数
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7351086.html
Copyright © 2011-2022 走看看