Spring IOC环境搭建及依赖注入(基于xml配置文件)

基于xml配置文件的IOC环境搭建

1、pom.xml导入依赖

首先创建一个maven工程,然后在pom.xml导入spring依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>freak.com.demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>

2、创建相关接口及实现类,配置bean文件,将bean添加到ioc容器中,并获取到ioc容器中的bean

这里我们创建两个很简单的接口及实现类,来展现相关内容

IbookService

package freak.item.service;

public interface IbookService {
    public void save();
}

IbookServiceImp

package freak.item.serviceImp;

import freak.item.service.IbookService;

public class IbookServiceImp implements IbookService {
    @Override
    public void save() {
        System.out.println("IbookServiceImp保存成功");
    }
}

IuserService

package freak.item.service;

public interface IuserService {
    public void save();
}

IuserServiceImp

package freak.item.serviceImp;

import freak.item.service.IuserService;



public class IuserServiceImp implements IuserService {
    public void save() {
      System.out.println("IuserServiceImp保存成功");
    }

}

这里我们就创建完成两个接口接实现类,接下来如何将实现类添加到ioc容器呢
让我们一起来配置xml文件,在resources下创建,这里我起名了一个bean.xml的文件,创建之后需要加上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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
   
</beans>

ioc容器是以key value 的方式储存bean的,添加代码完成对象的添加操作

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="userService" class="freak.item.serviceImp.IuserServiceImp"> </bean>
    <bean id="bookService" class="freak.item.serviceImp.IbookServiceImp"> </bean>
</beans>

接下来我们只有获取到bean,就可以调用里面我们定义好的方法
创建一个单元测试类

import freak.item.serviceImp.IbookServiceImp;
import freak.item.serviceImp.IuserServiceImp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class uiTest {
    /**
     * 获取spring的IOC容器
     * @param args
     */
    public static void main (String[] args){
        //获取bean中的核心容器对象,获取对象有三种方式这里我们采用了常用的ClassPathXmlApplicationContext方法获取对象
        ApplicationContext as = new ClassPathXmlApplicationContext("bean.xml");
        //以下两种方法都可以获取到bean对象(通过ID获取)
        IuserServiceImp userServiceImp = as.getBean("userService",IuserServiceImp.class);
        IbookServiceImp  bookServiceImp = (IbookServiceImp)as.getBean("bookService");
        //打印出两个对象,及调用该对象的方法
        System.out.println(userServiceImp);
        System.out.println(bookServiceImp);
        userServiceImp.save();
        bookServiceImp.save();

    }
}

输出结果:

十一月 24, 2020 10:15:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
freak.item.serviceImp.IuserServiceImp@6b4a4e18
freak.item.serviceImp.IbookServiceImp@27c86f2d
IuserServiceImp保存成功
IbookServiceImp保存成功
Process finished with exit code 0

3、spring的依赖注入

那么我们如何实现依赖注入呢,此处展示构造方法注入和set方法注入
首先修改实现类内容

IbookServiceImp

package freak.item.serviceImp;

import freak.item.service.IbookService;

public class IbookServiceImp implements IbookService {
    private String  bookName;
    private  int price;
    private  String author;

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public void save() {
        System.out.println("IbookServiceImp保存成功信息为:"+"bookName="+bookName+",price="+price+",author="+author);
    }
}

IuserServiceImp


package freak.item.serviceImp;

import freak.item.service.IuserService;

import java.util.Date;

public class IuserServiceImp implements IuserService {
    private String username;
    private int age;
    private Date birthday;
    public void setUsername(String username) {
        this.username = username;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public IuserServiceImp(String username, int age, Date birthday){
        this.username=username;
        this.age=age;
        this.birthday=birthday;
    };
    public void save() {
        System.out.println("IuserServiceImp保存成功信息为:"+"username="+username+",age="+age+",birthday="+birthday);
    }

}


由代码可以看出IuserServiceImp采用了构造函数,IbookServiceImp单纯应用了setter方法,这样我们修改bean文件来对两个分别进行依赖注入

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 第一种采用构造函数注入  -->
    <bean id="userService" class="freak.item.serviceImp.IuserServiceImp">
        <constructor-arg name="age" value="38"></constructor-arg>
        <constructor-arg name="username" value="德玛西亚"></constructor-arg>
        <constructor-arg name="birthday" ref="date"></constructor-arg>
    </bean>
    <!-- set方法注入-->
    <bean id="bookService" class="freak.item.serviceImp.IbookServiceImp">
        <property name="bookName" value="LOL超鬼大法"/>
        <property name="price" value="6"/>
        <property name="author" value="马保国"/>
    </bean>
    <bean id="date" class="java.util.Date"></bean>
</beans>

在这里其实可以看出,age类型我在实现类中定义的是int类型,而此处我value给的是String类型的值

在这里插入图片描述在这里呢,spring可以自己完成简单的类型转换,但是birthday字段是date类型,这里我们就不能赋予字符串:1999-02-13这样类型的值
从上面代码可以看出,添加了一个id 为 date的bean,并用ref获取

在这里插入图片描述
运行结果
在这里插入图片描述

这里只涉及到了简单类型的注入,没有涉及到 list map set props 等发杂类型的注入方法,后续笔者会详细讲解复杂类型的注入方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值