zoukankan      html  css  js  c++  java
  • spring_简介

    spring简介

     Spring简介:Spring是一个开源框架,最早由Rod Jhoson创建,Spring是为了解决企业级应用开发而创建的,但Spring不仅仅局限于服务器端的开发,任何的Java应用都能都能再简单性,可测试性,松耦合性等等方面从Spring中获益

    简化Java开发---Spring根本使命

    所谓的简化Java的开发,就是要减小Java开发的复杂性,Spring采取下面四种策略来简化Java的开发

    1:基于POJO的轻量级和最小侵入性编程

    2:通过依赖注入和面向接口实现松耦合

    3:基于切面和惯例进行声明式编程

    4:通过切面和模板减少样板式代码

    创建第一个spring程序

    1:创建一个maven项目并添加相应的依赖jar包

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.4.RELEASE</version>
    </dependency>

    2:创建一个接口

    package com.doaoao.service;
    public interface StudentService {
        void testDao();
    }

    3:创建上方接口的实现类(也就是要实现方法testDao() )

    package com.doaoao.impl;
    import com.doaoao.service.StudentService;
    public class StudentServiceImpl implements StudentService {
        @Override
        public void testDao() {
            System.out.println("Hello World");
        }
    }

    4:添加配置文件,这里命名为 applicationContxt.xml,命名可自定义

      在该配置文件中添加<bean>标签,bean标签中id可以随意命名,但必须唯一;bean中的class指向的是我们上方所创建接口的实现类(注意不能指向接口)

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- id可以随意命名,但得确保其是唯一的 -->
        <bean id="studentService" class="com.doaoao.impl.StudentServiceImpl"/>
    </beans>

    5:创建测试类

      测试类:当我们想要使用接口的实现类 StudentServiceImpl时,按照以往的知识,就必须先创建一个StudentServiceImpl的对象,再调用对象中的方法 testDao()

      当我们使用了Spring后,

      第一步读取配置文件 applicationContexy.xml 中的配置信息

      第二步获取配置文件某个id所对应的对象,当前id为studentService,读取的对象自然是实现类StudentServiceImpl了

      第三部直接调用对象中的方法

    package com.doaoao.test;
    
    import com.doaoao.impl.StudentServiceImpl;
    import com.doaoao.service.StudentService;
    import org.junit.jupiter.api.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestSpring {
    
        // 之前的写法:先创建StudentServiceImpl对象,再调用其中的方法
        @Test
        public void beforeTest(){
            StudentServiceImpl studentService = new StudentServiceImpl();
            studentService.testDao();
        }
    
    
        @Test
        public void sprintTest(){
            // 读取spring配置文件
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    
            // 从配置文件中获取对应id所对应的对象
            StudentService studentService = (StudentService) context.getBean("studentService");
    
            // 调用对象中的指定方法
            studentService.testDao();
        }
    }

     # 从以上的实现来看,似乎spring的实现过程比以往创建对象,再让对象调用方法麻烦。但随着后面的学习,你会发现,spring是多么的神奇的一个东西

    ..待补充

     本笔记参考自:小猴子老师教程 http://www.monkey1024.com

  • 相关阅读:
    nodejs 文件路径问题
    nodejs
    return , return true , return false的区别
    严防个人极端案事件 中央政法委要求健全危机干预机制
    《资本论》:什么是“原始积累”?农民的土地因何被社会剥夺
    程序员自我修炼(四)—— 代码重构
    中国各民族人口总数排名
    农村人可以去别的村买宅基地吗?这样合不合法?看完你就知道了!
    如果所在的农村拆迁了,可以在另外一个村申请宅基地吗?
    哈姆扎&#183;本&#183;拉登为什么反对美国?
  • 原文地址:https://www.cnblogs.com/Doaoao/p/10740270.html
Copyright © 2011-2022 走看看