spring(1) spring安装的安装与使用

一、spring Ioc简介:

        Ioc(Inversion of control):控制反转,将原本在程序中手动创建对象的控制权交由Spring框架来管理。

        正控:若要使用某个对象,需要自己去负责对象的创建。

        反控:若要使用某个对象,只需要从Spring容器中获取需要使用的对象,不关心对象的创建过程,也就是把创建对象的控制权反转给了Spring框架。

传统方式Ioc方式 
Category c = new Category(); Category c = Spring生产​​​​​​​

                                                                                                                               

二、Spring Bean: 

        定义:由Spring Ioc容器管理的对象称为Bean,Bean根据Spring配置文件中的信息创建。将Spring Ioc容器看作一个工厂,Bean相当于工厂的产品,如果希望这个工厂生产和管理Bean,就需要告诉容器需要哪些Bean,以哪种方式装配。

三、代码实例:

       1、使用idea创建项目,目录格式如下所示:

创建一个如图所示的文件目录 

        2、在pom.xml中添加项目所需依赖 

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.3.13</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.3.13</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.13</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.3.13</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
  </dependencies>

        3、新建HelloWorld.java文件,内容如下所示

public class HelloWorld {
    private String name;

    //    构造函数
    HelloWorld(String name){
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void getName() {
        System.out.println("massage:" + name);
    }
}

         4、新建bean的配置文件Beans.xml,注意:文件需存放在resource文件夹下。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--将HelloWorld注册为一个bean,bean的具体实现类是完整类名-->
    <bean id="helloWorld" class="org.caoze.HelloWorld">
        <!-- 通过构造函数将属性注入 -->
        <constructor-arg name="name" value="hello21" />
        <!-- 通过setter注入属性,优先通过setter注入 -->
        <property name="name" value="hello"></property>
    </bean>
</beans>

          5、新建MainApp.java,内容如下所示:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args){
        //使用Spring的Ioc来新建对象
        //读取Beans.xml文件的内容
        ApplicationContext context = new ClassPathXmlApplicationContext("test/Beans.xml");
        //获取bean“helloWorld”,将其赋给类型为HelloWorld的obj对象
        HelloWorld obj = context.getBean("helloWorld",HelloWorld.class);

        //之前新建对象使用的方法
        //HelloWorld obj = new HelloWorld();

        //obj对象调用getName()方法
        obj.getName();
    }
}

        6、点击运行,控制台打印出“message:hello”。

项目运行结果

四、总结

        在未使用Spring框架的时候,如果需要使用对象是通过类来新建对象的。在Spring Ioc中则不需要这么麻烦,只需要通过配置文件来新建一个bean,之后即可通过这个bean来进行对象的创建活动。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值