• Spring环境搭建,IoC容器初体验~


    由于最近的任务是关于IoC配置文件格式的转换,所以需要从Spring的IoC容器开始学起,今天根据网上的介绍搭建了Spring环境,并对其IoC容器进行了初体验。文章中涉及到的软件以及推荐的一本关于Spring IoC容器的电子书,在结尾处会附上我的百度网盘下载地址,以防官网维护或其他浮云情况。下附说明~

    1.打开http://www.springsource.org/spring-community-download,如下图所示,点击take me to the download page。

    2.然后进入了下载页面,选择最新版本的下载

    3.下载完成后解压缩到任意文件夹,里面的目录结构一目了然。

    4.打开eclipse,新建java project,右键工程名→Build Path→Configure Build Path,在打开的窗口选择Add External JARs

    5.然后在JAR Selection窗口中找到刚才解压的路径,进入libs文件夹,选中spring-beans-3.2.1.RELEASE.jar、spring-context-3.2.1.RELEASE.jar、spring-core-3.2.1.RELEASE.jar、spring-expression-3.2.1.RELEASE.jar,打开之后点击ok添加成功。

    6.打开http://commons.apache.org/proper/commons-logging//download_logging.cgi,下载commons-logging-1.1.1-bin.zip,之后解压到任意文件夹,然后再通过第五步的方法将解压后文件夹中的commons-logging-1.1.1.jar引入工程,成功后工程目录结构如下图

    7.准备工作完成,开始IoC的初体验,创建如下目录结构

    8.各个文件的代码如下

    BeanA.java

    1 package org.beans;
    2 
    3 public class BeanA {
    4     public void say(){
    5         System.out.println("welcome");
    6     }
    7 }

    BeanB.java

     1 package org.beans;
     2 
     3 public class BeanB {
     4     private BeanA ba ;
     5 
     6     public BeanA getBa() {
     7         return ba;
     8     }
     9 
    10     public void setBa(BeanA ba) {
    11         this.ba = ba;
    12     }
    13     
    14 }

    Start.java

     1 package org.beans;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Start {
     7     public static void main(String[] args) {
     8         ApplicationContext ctx = new ClassPathXmlApplicationContext("org/beans/applicationContext.xml");
     9         BeanB bb = (BeanB) ctx.getBean("beanB");
    10         bb.getBa().say();
    11     }
    12 }

    applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans 
     3     xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xmlns:p="http://www.springframework.org/schema/p"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     7         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
     8  <bean id = "beanB" class = "org.beans.BeanB">
     9      <property name = "ba" ref = "beanA"/>
    10  </bean>
    11  <bean id = "beanA" class = "org.beans.BeanA"/>
    12 </beans>

    搞定,运行程序后,控制台输出welcome~~

    文章涉及的jar包以及Spring IoC容器的介绍电子书下载链接:

    http://pan.baidu.com/share/link?shareid=408901&uk=152821134

    有不足之处还望路过大神指出,有疑问的地方欢迎讨论~~

    PS: 我存在过,我遇见过,我失败过。 有些路,明明有坑却从没人放警示牌。有些事,明明是错的却没人去管。有些话,明明应该告诉后来人却没人去说。 既然没人做,那就我来吧。希望我曾经历过的挫折不再重现于后来人。希望传承能够不是只挂在嘴边。希望人模人样的“人”能够真正做人。
  • 相关阅读:
    MonoBehaviour.FixedUpdate 固定更新
    Gizmos 辅助线框
    Quaternion 四元数
    Object.Instantiate 实例
    c语言描述的静态查找表
    c语言描述的二叉树的基本操作(层序遍历,递归,非递归遍历)
    c语言描述的链队列的基本操作
    c语言描述的双向链表的基本操作
    c语言描述的简单选择排序
    c语言描述的二分插入排序法
  • 原文地址:https://www.cnblogs.com/FlameRen/p/2943185.html
Copyright © 2020-2023  润新知