Spring简介

    今天开个新坑(づ)づ介绍一下Spring框架???

  1. Spring是个开源框架
  2. Spring是一个IocID)和AOP容器框架

 

  1. 轻量级:Spring是得侵入性的,我们不需要使用Spring的提供接口或者继承他的类
  2. 依赖注入
  3. 面向切面编程
  4. 容器
  5. 框架
  6. 一站式框架

 

最底层核心容器分为四块:Beans,Core,Context,SpEL

 

具体细节,之后章节再一一讲解

配置Maven

    大家创建好项目之后,可以去https://mvnrepository.com/ 找我们需要的jar包,我们需要四个基础的jar包,也就是上面图上最底层核心容器的相关jar包,除此之外,我们还需要一个spring必须依赖的日志包:

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
View Code

写个HelloWorld

    Emmmm,基本流程,先写一个非常简单的类,还有方法ヾ(●)

  1. package com.figsprite.bean;  
  2.     
  3. public class HelloWorld {  
  4.     private String name;  
  5.     
  6.     public void setName(String name) {  
  7.         this.name = name;  
  8.     }  
  9.         
  10.     public void hello(){  
  11.         System.out.println("hello " + name);  
  12.     }  
  13. }  

 

  1. package com.figsprite.bean;  
  2.     
  3. public class Main {  
  4.     
  5.     public static void main(String[] args) {  
  6.         HelloWorld helloWorld = new HelloWorld();  
  7.         helloWorld.setName("world");  
  8.         helloWorld.hello();  
  9.     }  
  10.     
  11. }  

真的很简单,我们来说说Main中的代码

  1. 创建HelloWorld的一个对象

    HelloWorld helloWorld = new HelloWorld();

  2. 为name属性赋值

    helloWorld.setName("world");

  3. 调用hello方法

    helloWorld.hello();

    Spring的任务就是帮我们完成1.2两步,

通过IDEA解决spring配置文件

    先创建spring的配置文件,首先右键新建èXML Configuration FileèSpring Config

 

创建一个applicationContext.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  5.         <bean id="helloWorld" class="com.figsprite.bean.HelloWorld">  
  6.             <property name="name" value="World"></property>  
  7.         </bean>  
  8. </beans>  

看看里面各个标签,首先是<bean>,很明显,Spring需要利用反射创建新对象,所以我们需要传入类的全限定名,并用id作为唯一标识,接着里面所包含的<property>标签,name属性即为setXXX所对应的字段名,value就是赋给它的值。

 

这样在Main中的步骤就将发生变化

  1. 创建Spring的Ioc容器对象

    ApplicationContext代表的就是Spring中的Ioc容器接口,ClassPathApplicationContext是ApplicationContext的一个实现类,传入的是配置文件的名字

    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

  2. 从Ioc容器中获取Bean实例

    这里传入的是<bean>标签的id

    HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");

  3. 调用hello方法

    helloWorld.hello();

    好奇的小伙伴可以试着在HelloWorld.java中加点东西看看Spring是否真的帮我们创建了新对象,并且将它赋值,我们注释掉2.3步的代码,看看发生什么

    1. package com.figsprite.bean;  
    2.     
    3. public class HelloWorld {  
    4.     private String name;  
    5.     
    6.     public HelloWorld(){  
    7.         System.out.println("****创建对象****");  
    8.     }  
    9.     public void setName(String name) {  
    10.         System.out.println("****设置name****");  
    11.         this.name = name;  
    12.     }  
    13.         
    14.     public void hello(){  
    15.         System.out.println("hello " + name);  
    16.     }  
    17. }  

     

    是它,是它,就是它,(~ ̄▽ ̄)~就是Spring干的

转载于:https://www.cnblogs.com/figsprite/p/10750629.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值