【框架专题】管理型容器——SpringIOC——入门

SpringIoc入门——环境准备


      <!--【Spring util级别的代码封装】,asm、cligb的一些实现在这儿,还有各种处理注解与反射的工具类-->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-core</artifactId>
           <version>5.0.0.RELEASE</version>
       </dependency>
       <!--【Spring bean级别的代码jar包】,基本的IoC/DI的实现,包含访问配置文件、创建和管理bean -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-beans</artifactId>
           <version>5.0.0.RELEASE</version>
       </dependency>
       <!--【Spring 容器级别的代码jar包】,主要在整个容器层面考虑扩展性以及容器的生命周期等等-->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>5.0.0.RELEASE</version>
       </dependency>
       <!--【Spring 一些应用的扩展jar包】,比如Cache(ehcache)、JCA、JMX、 邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类-->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context-support</artifactId>
           <version>5.0.0.RELEASE</version>
       </dependency>
       <!--【Spring SPEL表达式的支持】-->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-expression</artifactId>
           <version>5.0.0.RELEASE</version>
       </dependency>

创建ioc.xml文件
在这里插入图片描述
文件模板:

<?xml version="1.0" encoding="UTF-8"?>
<!--beans里配置约束文件,用于提示代码,这里是最完整的-->
<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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <bean>
    </bean>
</beans>

SpringIoc入门——创建对象

创建想要被管理的对象

public class HelloController {
    private HelloService helloService;//注意这里我们是没有手动实例化的,我将演示利用SpringIoc注入我们的实例化对象
    public void dealRequest(String message){
        helloService.sayMessage(message);
    }

    public HelloService getHelloService() {
        return helloService;
    }
    public void setHelloService(HelloService helloService) {
        this.helloService = helloService;
    }
}
public class HelloService {
    private String username;
    private String password;
    public HellorService(){
        this.username="GodSchool";
        this.password="GodSchool";
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
   public void setPassword(String password) {
        this.password = password;
    }
    public void sayHelloWolrd(){
        System.out.println("HelloWolrd"+this.username+this.password);
    }
    public void sayMessage(String message){
        System.out.println(message);
    }
}

在这里插入图片描述
利用XML描述我们的对象

<?xml version="1.0" encoding="UTF-8"?>
<!--beans里配置约束文件,用于提示代码,这里是最完整的-->
<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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  <!--
       class就是我们类的定位,spring的原理就是找到class利用反射我们把对象创建到ioc容器中去,
      id就是取出对象的key,创建好的对象可以用id取出来供我们使用
  -->
    <bean id="HelloController" class="ioc_01.HelloController"></bean>
    <bean id="HelloService" class="ioc_01.HelloService"></bean>
</beans>

写程序入口(1)加载配置文件,自动创建对象(2)取出我们的对象

public class test01 {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("bean-ioc.xml");
        HelloController helloController = ac.getBean("HelloController", HelloController.class);
        HelloService helloService = ac.getBean("HelloService", HelloService.class);

    }
}

效果:
在这里插入图片描述
我们实现了xml文件管理对象的创建,是不是很清晰,我们创建了那些对象在xml中一目了然,这样开发的时候就可以有计划性的管理对象了,相信你已经体验到了他的好处!

SpringIoc入门——依赖注入

那么我们继续吧,现在我们只解决了独立的创建两个对象;我们想让一个对象引用另一个对象的方法,如何做到呢,这就叫依赖注入;

改造xml文件

    <bean id="HelloController" class="ioc_01.HelloController">
        <property name="helloService" ref="HelloService"></property><!--name 我们属性名 ref引用的另外一个对象的id-->
    </bean>
    <bean id="HelloService" class="ioc_01.HelloService"></bean>

测试

public class test01 {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("bean-ioc.xml");
        HelloController helloController = ac.getBean("HelloController", HelloController.class);
        helloController.dealRequest("Hello autowise");
           /**
              我们拿到了service对象,然后调用service对象的sayMessage方法
   			*  public void dealRequest(String message){
    		*      helloService.sayMessage(message);
 			*  }
 			*/
    }
}

在这里插入图片描述
Ps:实际开发过程中,mvc的tomcat整合时,dispathcerServlet就是根据路径找到Controler,其他的都跟tomcat无关,所以这里我们一样只需要拿出创建好的controller,其内部的service已经被注入好了;

至此我们入门就结束了,下一章节开始我们将会xml的所有管理方式罗列出来,以及注解管理的方法;
现在都流行使用注解!!!xml我们可以了解一下,有时候写一些配置的时候还是挺管用!!!

经典总结:利用描述文件,告诉我们的要创建的对象的类在哪里,包名+类名形式;利用描述文件,告诉我们创建的对象的初始化属性是什么,或者依赖的另外一个对象的id是什么,所有对象都被平级创建在一个ioc的大容器中,利用id可以相应取出来;

后续Spring专题:
SpringIOC——XML管理全套
SpringIOC——注解管理全套
SpringIOC——内部工具全套
SpringIOC——核心源码剖析

在这里插入图片描述
GodSchool
致力于简洁的知识工程,输出高质量的知识产出,我们一起努力

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值