【Spring框架】Spring核心知识点剖析(一)之IOC

Spring核心知识点剖析(一)之IOC

最近刚开始接触Spring框架部分,写此博客记录学习内容。

Spring的基本概念

Spring:不管学过没学过,大家对Spring都有所了解。Spring是一款开放源码的Java框架,它致力于代码之间的松耦合。S
pring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。

Spring的核心知识点是IOC(inverse of control)反转控制,DI(dependency injection)依赖注入和AOP(Aspect Oriented Programming)面向切面编程。

IOC控制反转

何为控制反转?举个例子来说,正常来讲如果你要谈恋爱,你得相中一个合适的女孩子,问她索要联系方式,然后聊天,谈恋爱再到结婚生孩子。控制反转就像是你把你意向的女孩信息列下来交给婚介或者媒婆,让一个中介来帮你找符合你条件的女孩,然后你们再聊天结婚生孩子。

看了上面这个例子,理解IOC相对就要简单了。IOC的思想就是反转资源获取的方向,就是Spring容器主动地将资源(如JavaBean)推送给它所管理的组件, 组件所要做的仅是选择一种合适的方式来接受资源。

IOC的三种实现方式

1、通过Spring的容器,即创建一个名为applicationContext的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"
      		   xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="studentdao" class="com.imp.StudentImp">
    		   
    </bean>
 </beans>

其中,beans标签代表此xml是Spring的容器,而其中的bean标签是你需要的资源,id属性即为你为这个资源所起的名字,class属性即此资源所在的全路径(包名+类名)。
注意
(1)按照如上的配置文件的写法,你的资源即JavaBean中,必须有无参构造方法。带参构造方法及对属性的赋值会在之后的DI中讲解。
(2)如果在如上的配置文件中写入两个不同的id对应同一个的bean,则会抛出NoUniqueBeanDefinitionException异常。

对象获取完毕后获取资源的方法如下代码所示:

//创建容器上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取资源(需要强制类型转换)
StudentDao studendao = (StudentImp) context.getBean("studentdao");
//调用资源的方法
studendao.study();

如上getBean方法的参数即对应xml容器中bean的id值。

2、通过注解的方式配置
代码如下:

//开启注解的扫描(此段代码在applicationContext配置文件中存放)
<context:component-scan base-package="com.bean"/>

其中base-package为你要扫描的包,即你的资源所在的位置。

package com.bean;
import org.springframework.stereotype.Component;

@Component
public class DogBean {
    private String name;
    private String color;

    public DogBean() {
    }

    public DogBean(String name, String color) {
        this.name = name;
        this.color = color;
    }

    @Override
    public String toString() {
        return "DogBean{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

在导包代码之下,类代码开始之上进行注解即@Component。
然后在注解扫描的作用下,指定包内被@Component注解的类会在Spring容器被创建时完成创建,即如下代码:

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

获取方式同第一种方法,在此不多做赘述。
注意:
(1)使用此种方法扫描,getBean获取时参数为类名(首字母小写),如类名为StudentDao,参数即为"studentDao"。
3、全注释(零配置)
全注释,即不需要配置文件,全部用注解来完成。此时需要创建一个类当作容器,并用上注解:

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.ArrayList;

@Configuration
public class SpringConfig {
    public SpringConfig() {
        System.out.println("容器创建成功");
    }
}

此时的@Configuration就代替了之前在配置文件中beans,即如下代码的作用:

<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">
</beans>

然后在资源中配置注解

package com.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

@Repository(value = "studentBean")
public class StudentBean {
    private int id;
    private String name;
    private String major;

    public StudentBean() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "StudentBean{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", major='" + major + '\'' +
                '}';
    }

    public StudentBean(int id, String name, String major) {
        this.id = id;
        this.name = name;
        this.major = major;
    }

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

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }
}

这里的@Repository(value = “studentBean”)的作用就相当于之前的@Component
(@Repository和@Component的作用相同,@Component多用于业务逻辑层,@Repository多用于持久层),
value就相当于之前bean中的id属性,不写的话默认为类名(首字母小写)。
同时也要开启资源扫描,代码如下:

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.ArrayList;

@Configuration
@ComponentScan(basePackages = "com.bean")
public class SpringConfig {
    public SpringConfig() {
        System.out.println("容器创建成功");
    }
}

在@Configuration下面加上@ComponentScan(basePackages = “com.bean”)
这里的@ComponentScan(basePackages = “com.bean”)的作用相当于如下代码:

//开启注解的扫描(此段代码在applicationContext配置文件中存放)
<context:component-scan base-package="com.bean"/>

获取资源的方式同上面两种方法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值