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


  • 相关阅读:
    这些奇葩的排序算法,你没见过动画吧?
    五分钟学会一个高难度算法:希尔排序
    看完动画你还会不懂 快速排序么
    动画演示二叉树的前序遍历
    一款基于jQuery日历插件的开发过程
    图片全部加载完成之后再显示页面ui,公司项目里用上,自己写的几行代码
    转:SQL Server附加数据库提示“版本为661,无法打开,支持655版本……”
    Android基础-TextView(文本控件)
    Android基础-UI控件(整体简单介绍)
    Android基础-相对布局(电影页面为例)
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7351086.html
Copyright © 2011-2022 走看看