Spring入门之IOC(包含实例代码)

什么是Spring?

框架相当于把原来servlet的工作分成了三层:表现层(接受和响应数据)服务层 | 业务层(分配任务、控制流程)持久层(与数据库交互)

ssm框架springmvc + spring + mybatis,多用于互联网项目
ssh框架springmvc + spring + hibernet,多用于事业单位项目

pic

  • spring的核心有IOC(控制反转)AOP(面向切面开发)DI(依赖注入)

什么是Spring IOC?

IOC又称为控制反转,指的是把对象的创建权力交给spring框架,底层实际上是使用反射实现的,用来降低程序的耦合度。

如何创建一个Spring IOC项目?

1. 导入Maven项目依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2. 准备一个实体类(先定义接口再实现)

package com.qcby.service;

public interface UserService {
    public void hello();
}
package com.qcby.service.impl;

import com.qcby.service.UserService;

public class UserServiceImpl implements UserService {
    public void hello() {
        System.out.println("业务层:你好!");
    }

}

3. 准备配置文件

  • 实例化Bean的三种方式(示例代码以第一种方式为例,全部实现方式见文章最后
    1. 默认为无参数的构造方法:<bean id = "us" class="实体类的路径(包含到类名)"></bean>
    2. 静态工厂实例化方式:<bean id = "us" class="静态工厂类路径(包含到类名)" factory-method="实例化方法"/>
    3. 实例化工厂实例化方式,生成一个对象交给IOC:<bean id = "dFactory" class="实例化工厂类路径(包含到类名)"/><bean id = "us" factory-bean="工厂名" factory-method="实例化方法"/>
<?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 = "us" class="com.qcby.service.impl.UserServiceImpl"/>
</beans>

4. 在Test类中测试

  • ApplicationContext接口是个工厂接口,使用该接口可以获取到具体的Bean对象,该接口下有两个具体的实现类:
    • ClassPathXmlApplicationContext:加载类路径下的spring配置文件(常用)
    • FillSystemXmlApplicationContext:加载本地磁盘下的spring配置文件(让配置文件与项目分离管理)
import com.qcby.service.UserService;
import com.qcby.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceTest {

    //传统创建对象
    @Test
    public void run1() {
        UserService userService = new UserServiceImpl();
        userService.hello();
    }

    //spring ioc创建对象 ioc是一个map key是对象的标识 value是ioc创建的对象
    @Test
    public void run2() {
        // 创建spring ioc工厂,加载spring配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 获取bean对象 (底层通过反射来获取到对象)
        UserService userService = (UserService)ac.getBean("us");
        // 调用方法
        userService.hello();
        System.out.println(userService);
    }
}

IOC中的Bean管理

<bean id = "us" class="com.qcby.service.impl.UserServiceImpl"/>

属性解释
id属性给bean起个名字,具有唯一性。取值要求:字母开头,可以用字母、数字、连字符、下划线
class属性bean对象的全路径(包名+类名)
scope属性表示bean对象的作用范围
singleton单例(默认)最常用的方式,生命周期和配置文件一样(配置文件加载出来后就会创建实例)
prototype多例,不是加载配置文件的时候创建实例,获取实例时才创建
request多例,不常用,应用于web项目中,每次http请求时创建一个新的实例
session多例,不常用,应用于web项目中,同一个http session共享一个实例
init-method属性bean对象创建时可以配置一个指定方法并自动调用(通过反射获取到的)
destroy-method属性bean对象销毁时可以配置一个指定方法并自动调用

实例化Bean的三种方式

  1. 默认为无参构造方法
    <bean id="us" class="实体类的路径"></bean>
  1. 静态工厂实例化方法(可以自己编写业务逻辑)
package com.qcby.util;

import com.qcby.service.UserService;
import com.qcby.service.impl.UserServiceImpl;

public class StaticFactory {
    public static UserService createUs() {
        System.out.println("通过静态工厂的方式~");
        //编写业务逻辑
        //。。。
        return new UserServiceImpl();
    }
}
	<bean id = "us" class="com.qcby.util.StaticFactory" factory-method="createUs"/>
  1. 实例化工厂实例化方式(可以自己编写业务逻辑)生成一个对象交给ioc
package com.qcby.util;

import com.qcby.service.UserService;
import com.qcby.service.impl.UserServiceImpl;

public class DFactory {
    public UserService createUs() {
        System.out.println("通过实例化工厂的方式~");
        //编写业务逻辑
        //。。。
        return new UserServiceImpl();
    }
}
	<bean id = "dFactory" class="com.qcby.util.DFactory"/>
    <bean id = "us" factory-bean="dFactory" factory-method="createUs"/>
  • 23
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值