Spring的IOC入门

IOC控制反转

IOC,inversion of control :控制反转,指导开发人员如何适用对象,管理对象,把对象的创建,属性赋值,对象的声明周期都交给代码之外的容器管理

IOC的技术实现

DI(依赖注入):dependency injection ,程序只需要提供要使用的对象名称就可以了,对象如何创建,如何从容器中查找,获取都是由容器内部自己实现。

依赖名词:比如说class A类使用了class B类的属性或者方法,叫做class A依赖B;

spring框架使用的是DI实现IOC

通过spring框架,只需要提供要使用的对象名词就可以了,从容器中获取名称对应的对象。
spring底层使用的反射机制,通过反射创建对象,和属性。

DI:依赖注入,一个BookService类的功能需要依赖另外一个BookDao类配合,同时需要附上一个具体的对象,则成这种关系是依赖注入;

IOC:控制反转,以前依赖的对象自己直接new,现在不需要new,框架会自动注入对象,创建对象的权利转移给了框架;

Spring框架负责所有对象的创建、管理和依赖注入;所有的对象存储的容器叫做IOC容器;

 1、创建项目,导入spring的jar包

 2、按照如下目录结构创建接口及其实现类

servlet层

public class BookServlet {
    BookService bookService;

    @Test
    public void add(){
        System.out.println("BookServlet.......add");
        bookService.save();
    }
}

service层

//接口
public interface BookService {
    void save();
}


//实现类
public class BookServiceImpl implements BookService {
    private BookDao bookDao;


    @Override
    public void save() {
        System.out.println("BookServiceImpl.....save");
        bookDao.insert();
    }
}

dao层

//接口
public interface BookDao {
    public void insert();
}


//实现类
public class BookDaoImpl implements BookDao {
    @Override
    public void insert() {
        System.out.println("BookDaoImpl......save");
    }
}
注意:
此时项目没办法执行,因为没有做依赖对象的注入。

 3、使用spring完成IOC的配置和DI依赖注入

1)创建spring的核心配置文件

<?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">
    <!--把所有对象创建到spring的IOC容器中,并起上名字
        id:表示给对象起名字
        class:类的全类名
     -->
    <bean id="bookDao" class="com.wen.dao.impl.BookDaoImpl"></bean>
    <bean id="bookService" class="com.wen.service.impl.BookServiceImpl">
        <!--spring的依赖注入:property表示BookServiceImpl的属性,常规属性使用value赋值,
        类型属性使用ref引用-->
        <property name="name" value="小红"/>
        <property name="bookDao" ref="bookDao"/>
    </bean>
</beans>
bean标签表示把对应class的对象创建到IOC容器中,如果当前对象有依赖的其他对象,那么可以
通过property对依赖的对象使用ref引用容器中已经注册过的对象。

注意:BookServiceImpl需要给bookDao属性设置setter方法,框架才能自动注入

public class BookServiceImpl implements BookService {
    private String name;
    private BookDao bookDao;

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

    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    @Override
    public void save() {
        System.out.println("BookServiceImpl.....save"+name);
        bookDao.insert();
    }
}

2)在servlet中进行测试

1. 首先获取 IOC 的容器;
2. 从容器中获取 bookService 对象;
3. 执行方法: servlet 执行 ---service 执行,由于 spring 完成了依赖注入 ------dao 层执行
public class BookServlet {
    BookService bookService;

    @Test
    public void add(){
        System.out.println("BookServlet.......add");
        //1.获得IOC容器
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        //根据名字从容器中获取对象
        bookService = (BookService) context.getBean("bookService");
        bookService.save();
    }
}

运行结果:

4、添加一个普通的引用类对象

默认单例

<?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="bookDao" class="com.wen.dao.impl.BookDaoImpl"></bean>
</beans>

多例

<!--每次取对象时,拿到的都是一个新对象-->
<bean id="bookDao" class="com.wen.dao.impl.BookDaoImpl" scope="prototype"></bean>

5、静态工厂实例化

factory:工厂对象;工厂类就是制造对象的,一般没有功能;

class:工厂类的全类名

factory-method:具体工厂类中创建对象的方法名

配置

<bean id="bookService1" class="com.wen.factory.BookServiceFactory" ></bean>

测试

public class Test02 {
    @Test
    public void test02() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        BookServiceFactory factory = (BookServiceFactory) context.getBean("bookService1");
        System.out.println(factory);
    }
}

 运行结果:

配置工厂 

<!--配置工厂,但是获得的工厂的商品 -->
    <bean id="bookService1" class="com.wen.factory.BookServiceFactory" factory-method="getBean"></bean>

静态工厂创建对象

public class BookServiceFactory {
    public static BookService getBean() {
        return new BookServiceImpl();
    }
}

 测试

public class Test02 {
    @Test
    public void test02() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        BookService factory = (BookService) context.getBean("bookService1");
        System.out.println(factory);
    }
}

运行结果:

6、集合注入

list、set、map

<bean id="serviceVip" class="com.wen.service.impl.BookServiceVip">
        <property name="list">
            <list>
                <value>张三</value>
                <value>小明</value>
                <value>小美</value>
            </list>
        </property>
        <property name="set">
            <set>
                <value>南阳</value>
                <value>洛阳</value>
                <value>安阳</value>
                <value>信阳</value>
                <value>濮阳</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="sex" value="男"></entry>
                <entry key="age" value="19"></entry>
                <entry key="address" value="开封"></entry>
            </map>
        </property>
    </bean>

在BookServiceVip中定义引用类型属性,提供可访问的setter方法

public class BookServiceVip implements BookService {
    List list=new ArrayList();
    Set set=new HashSet();
    Map map=new HashMap();

    public void setList(List list) {
        this.list = list;
    }

    public void setSet(Set set) {
        this.set = set;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    @Override
    public void save() {
        System.out.println("BookServiceVip....save"+list+set+map);
    }
}

测试类

public class BookServlet1 {
    BookService bookService;
    @Test
    public void cc(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        bookService = (BookService) context.getBean("serviceVip");
        bookService.save();
    }
}

运行结果:

 7、注解

创建项目导入jar包

 创建接口及其实现类

 

 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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"
       xsi:schemaLocation="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.xsd">


    <!--让系统去扫描注解-->
    <context:component-scan base-package="com.wen"/>
</beans>

 BookDao接口的实现类BookDaoImpl使用注解@Component

package com.wen.dao.impl;

import com.wen.dao.BookDao;
import org.springframework.stereotype.Component;

@Component
public class BookDaoImpl implements BookDao {
    @Override
    public void insert() {
        System.out.println("BookDaoImpl....insert");
    }
}

BookService接口的实现类BookServlet使用注解@Component、@Autowired

package com.wen.service.impl;

import com.wen.dao.BookDao;
import com.wen.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BookServlet implements BookService {
    //自动注入优先按照类型注入,如果有多个对象,按照名字进行注入
    @Autowired
    BookDao bookDao;
    @Override
    public void save() {
        System.out.println("BookServlet....save");
        bookDao.insert();
    }
}

测试类

package com.wen.servlet;

import com.wen.service.BookService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {
    BookService bookService;
    @Test
    public void test01(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        bookService=context.getBean(BookService.class);
        bookService.save();
    }
}

运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值