zoukankan      html  css  js  c++  java
  • Spring框架解析

    在生活中我们会有很多依赖关系,我要写一个笔记,就要有本子,笔,两种物品,还有当事人我,一共三个对象。

    1、本子:可以再上面写字,这是他是属性,可被书画;

    2、笔:可以在很多东西上写写画画;这也是他的属性:可以写字;

    3、我:要用本子和笔完成一件事情,就是在本子上写:乐天是个sb;

    我要完成这件事就要依赖于本子,笔,就存在了依赖关系, 但是你想想你要做另一件事情是不是还要依赖其他的事物呢?依赖的东西是不是要更多呢

    再比如说,你要洗澡,要有毛巾,热水器,沐浴露,洗发膏,浴缸。。。好多

    我们把这些依赖关系写进一个spring.xml文件里,我们就可以少用很多new了,要不然你每次用他们就要创建声明对象,然后才能使用

    下面我要讲Spring环境搭建了:

    一、配置文件:

    1、asm-2.2.3.jar

    2、asm-commons-2.2.3.jar

    3、asm-util-2.2.3.jar

    4、aspectjrt.jar

    5、aspectjweaver.jar

    6、cglib-nodep-2.1_3.jar

    7、commons-logging-1.0.4.jar

    8、spring.jar

    还有一个核心文件: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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean id="m" class ="com.old.Manager"/>
    <bean id="t" class ="com.old.Computer"/>
    <bean id="f" class ="com.old.FrontStarff"/>
    <bean id="fresh" class="com.old.FreshStarff">
      <property name="manager" ref="m"/>
      <property name="tool" ref="t"/>
      <property name="fs" ref="f"/>

    </bean>
    </beans>

    配置文件的第一行是不能有空行的!!!就是因为这个原因我找了好久的错误!!!!

    下面的是实体类:

    package com.old;

    /**

    *电脑这个工具具有电脑的属性

    **/
    public class Computer implements tool {
    public String getName(){
    return "computer";
    }
    }

    某个人要使用电脑,实现使用电脑这个方法,

    package com.old;


    public class FreshStarff {
    private Manager manager;
    private tool tool;
    private FrontStarff fs;


    public Manager getManager() {
    return manager;
    }


    public void setManager(Manager manager) {
    this.manager = manager;
    }


    public tool getTool() {
    return tool;
    }


    public void setTool(tool tool) {
    this.tool = tool;
    }


    public FrontStarff getFs() {
    return fs;
    }


    public void setFs(FrontStarff fs) {
    this.fs = fs;
    }


    public void usetool(){
    if ("ok".equals(manager.allow())&&"ok".equals(fs.register())){
    System.out.println("yun許使用"+tool.getName());
    }
    }
    }

    前台同意使用电脑

    package com.old;


    public class FrontStarff {
    public String register(){
    return "ok";
    }
    }

    经理同意使用电脑

    package com.old;


    public class Manager {
    public String allow(){
    return "ok";
    }
    }

    测试类,调用封装在Spring里的内容,来实现功能,并打印到控制台

    package com.old;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Test {
    public static void main(String[] args) {
    //加載Spring的配置文件
    ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
    FreshStarff fh= (FreshStarff)ac.getBean("fresh");
    fh.usetool();
    Manager mg= (Manager)ac.getBean("m");
    System.out.println(mg.allow());

    tool to=(tool)ac.getBean("t");
    System.out.println(to.getName());
    }
    }

     我得天Spring内层太难了,等我研习一下继续给大家解读吧

  • 相关阅读:
    rails时间问题
    stringify_keys 和symbolize_keys
    thritf
    Nginx负载均衡反向代理
    CentOS 7 yum 安装 Nginx
    CentOS 7安装与配置Tomcat8
    CentOS 7安装与配置JDK8
    系统数据字典模块设计
    mysql视图
    阿里云maven中央仓库
  • 原文地址:https://www.cnblogs.com/Jack-Imane/p/6516015.html
Copyright © 2011-2022 走看看