Spring(四)基于xml的自动装配

news/2024/5/20 9:58:58

自动装配:根据指定的策略,在IOC容器中匹配某一个bean,自动为指定的bean中所依赖的类类型或接口类型属性赋值。

首先我们来熟悉三层架构的创建过程:

三层架构为controller层,service层,dao层。

在service层里面创建serviceImpl,创建实现类,实现类要继承service层的接口。

dao层里面的实现类要继承

我们首先进行创建controller层

之后我们进行创建service层:

 同时创建service层接口的实现类,实现类同时继承该接口。

最后我们进行创建dao层:

 之后我们在controller层的类里面进行创建对service层的对象:

UserController:

package com.rgf.controller;

import com.rgf.service.Impl.UserServiceImpl;
import com.rgf.service.UserService;

public class UserController {
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

UserServiceImpl:

package com.rgf.service.Impl;

import com.rgf.dao.UserDao;
import com.rgf.service.UserService;

public class UserServiceImpl implements UserService {
  private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}

如果我们在UserController层里面进行创建方法:

package com.rgf.controller;

import com.rgf.service.Impl.UserServiceImpl;
import com.rgf.service.UserService;

public class UserController {
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public  void saveUser(){
        userService.saveUser();
    }
}

则我们需要在service接口里面创建该方法:

package com.rgf.service;

public interface UserService {
    /**
     * 保存用户信息
     */
    void saveUser();
}

我们在该实现类里面对该方法进行重写:

package com.rgf.service.Impl;

import com.rgf.dao.UserDao;
import com.rgf.service.UserService;

public class UserServiceImpl implements UserService {
  private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void saveUser() {
      userDao.saveUser();
    }
}

我们在该接口实现该方法:

package com.rgf.dao;

public interface UserDao {
    /**
     * 保存用户信息
     */
    void saveUser();
}

之后我们在该实现类对该方法进行重写:

package com.rgf.dao.Impl;

import com.rgf.dao.UserDao;

public class UserDaoImpl implements UserDao {

    @Override
    public void saveUser() {
        System.out.println("保存成功");
    }
}

我们可以看到,我们在controller里面写方法,需要调用service层实现类里面的该方法,故service层的接口需要实现该方法,而service层的实现类需要重写该方法的同时调用dao层的该方法,dao层接口需要实现该方法,dao层的实现类进行构建该方法。

我们进行创建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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userController" class="com.rgf.spring.controller.UserController">
        <property name="userService" ref="userService"></property>

    </bean>
    <bean id="userService" class="com.rgf.spring.service.Impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
    <bean id="userDao" class="com.rgf.spring.dao.Impl.UserDaoImpl"></bean>
</beans>

我们进行测试如下所示:

package com.rgf.spring.test;

import com.rgf.spring.controller.UserController;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AutowireByXMLTest {
    @Test
    public  void  testAutowire(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-autowire.xml");
        UserController userController = ioc.getBean(UserController.class);
        userController.saveUser();
    }
}

运行之后如下所示:

 二、基于XML的自动装配之byType:

其中no为不自动装配

default为不设置,也是不装配

我们利用自动装配如下所示:

<?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="userController" class="com.rgf.spring.controller.UserController" autowire="byType">
    </bean>
    <bean id="userService" class="com.rgf.spring.service.Impl.UserServiceImpl" autowire="byType">
    </bean>
    <bean id="userDao" class="com.rgf.spring.dao.Impl.UserDaoImpl"></bean>
</beans>

 我们运行之后如下所示:

<?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="userController" class="com.rgf.spring.controller.UserController" autowire="byType">
    </bean>
    <bean id="userService" class="com.rgf.spring.service.Impl.UserServiceImpl" autowire="byType">
    </bean>
    <bean id="service" class="com.rgf.spring.service.Impl.UserServiceImpl" autowire="byType">
    </bean>
    <bean id="userDao" class="com.rgf.spring.dao.Impl.UserDaoImpl"></bean>
    <bean id="dao" class="com.rgf.spring.dao.Impl.UserDaoImpl"></bean>
</beans>

此时出现报错。

我们进行运行之后如下所示:

 我们将中间的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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userController" class="com.rgf.spring.controller.UserController" autowire="byType">
    </bean>
    <!--<bean id="userService" class="com.rgf.spring.service.Impl.UserServiceImpl" autowire="byType">
    </bean>-->
   
    <bean id="userDao" class="com.rgf.spring.dao.Impl.UserDaoImpl"></bean>

</beans>

我们运行之后如下所示:

出现空指针异常。

我们的测试类如下:

package com.rgf.spring.test;

import com.rgf.spring.controller.UserController;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AutowireByXMLTest {
    /**
     * 自动装配:
     * 根据指定的策略,在IOC容器中匹配某个bean,自动为bean中的类类型的属性或接口类型的属性赋值
     * 可以通过bean标签中的autowire属性设置自动装配的策略
     * 自动装配的策略:
     * no,default:表示不装配,即bean中的属性不会自动匹配某个bean为属性赋值,此时属性使用默认值
     * byType:根据要赋值的属性的类型,在IOC容器中匹配某个bean,为属性赋值
     * 注意:
     * a>若通过类型没有找到任何一个类型匹配的bean,此时不装配,属性使用默认值
     * b>通过类型找到了多个类型匹配的bean,此时会抛出异常:NoUniqueBeanDefinitionException
     * 总结:当使用byType实现自动装配时,IOC容器中有且只有一个类型匹配的bean能够为属性赋值。
     */
    @Test
    public  void  testAutowire(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-autowire.xml");
        UserController userController = ioc.getBean(UserController.class);
        userController.saveUser();
    }
}

  三、基于XML的自动装配之byName:

byName:把我们当前要赋值的属性的属性名作为bean的id,在IOC容器中匹配到某一个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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userController" class="com.rgf.spring.controller.UserController" autowire="byName">
    </bean>
    <bean id="userService" class="com.rgf.spring.service.Impl.UserServiceImpl" autowire="byName">
    </bean>
    <bean id="service" class="com.rgf.spring.service.Impl.UserServiceImpl " autowire="byName">
    </bean>
    <bean id="userDao" class="com.rgf.spring.dao.Impl.UserDaoImpl"></bean>
    <bean id="dao" class="com.rgf.spring.dao.Impl.UserDaoImpl"></bean>
</beans>

我们运行之后如下所示:

 总结如下所示:


     * 自动装配:
     * 根据指定的策略,在IOC容器中匹配某个bean,自动为bean中的类类型的属性或接口类型的属性赋值
     * 可以通过bean标签中的autowire属性设置自动装配的策略
     * 自动装配的策略:
     * 1.no,default:表示不装配,即bean中的属性不会自动匹配某个bean为属性赋值,此时属性使用默认值
     * 2.byType:根据要赋值的属性的类型,在IOC容器中匹配某个bean,为属性赋值
     * 注意:
     * a>若通过类型没有找到任何一个类型匹配的bean,此时不装配,属性使用默认值
     * b>通过类型找到了多个类型匹配的bean,此时会抛出异常:NoUniqueBeanDefinitionException
     * 总结:当使用byType实现自动装配时,IOC容器中有且只有一个类型匹配的bean能够为属性赋值。
     *3.byName:将要赋值的属性的属性名作为bean的id,在IOC容器中匹配某个bean,为属性赋值
     * 总结:当类型匹配的bean有多个时,此时可以使用byName实现自动装配
     */


http://www.niftyadmin.cn/n/393973.html

相关文章

​LeetCode解法汇总2465. 不同的平均值数目

目录链接&#xff1a; 力扣编程题-解法汇总_分享记录-CSDN博客 GitHub同步刷题项目&#xff1a; https://github.com/September26/java-algorithms 原题链接&#xff1a;力扣 描述&#xff1a; 给你一个下标从 0 开始长度为 偶数 的整数数组 nums 。 只要 nums 不是 空数组…

【深度学习-第1篇】深度学习是什么、能干什么、要怎样学?

大概4年前本专栏发布了一篇关于神经网络的入门科普文章&#xff0c;到现在大概有小5k赞了&#xff08;Mr.看海&#xff1a;神经网络15分钟入门&#xff01;足够通俗易懂了吧&#xff09;。 这几年人工智能发生了突飞猛进的发展&#xff0c;时至今日甚至发展出了GPT-4、Midjourn…

make xxx_deconfig过程

在uboot中&#xff0c;所写的shell脚本&#xff1a;mx6ull_alientek_emmc.sh的内容如下&#xff1a; #!/bin/bash2 make ARCHarm CROSS_COMPILEarm-linux-gnueabihf- distclean3 make ARCHarm CROSS_COMPILEarm-linux-gnueabihf- mx6ull_14x14_ddr512_emmc_defconfig4 make V1…

TextBlob怎么用的?有哪些具体使用场景呢?

TextBlob是一个Python库&#xff0c;提供了简单而直观的API&#xff0c;用于进行文本处理任务&#xff0c;如文本情感分析、词性标注、文本翻译等。 要使用TextBlob&#xff0c;首先需要安装该库。可以使用pip命令进行安装&#xff1a; pip install textblob 安装完成后&…

[数据结构习题]队列——用栈实现队列

[数据结构习题]队列——用栈实现队列 &#x1f449;知识点导航&#x1f48e;&#xff1a;【数据结构】栈和队列 &#x1f449;[王道数据结构]习题导航&#x1f48e;&#xff1a; p a g e 85.3 page85.3 page85.3 本节为栈和队列的综合练习题 题目描述&#xff1a; &#x1f…

Linux 实操篇-RPM 与YUM

Linux 实操篇-RPM 与YUM rpm 包的管理 介绍 rpm 用于互联网下载包的打包及安装工具&#xff0c;它包含在某些Linux 分发版中。它生成具有.RPM 扩展名的文件。RPM是RedHat Package Manager&#xff08;RedHat 软件包管理工具&#xff09;的缩写&#xff0c;类似windows 的set…

INTJ型人格适合选择哪些专业?

INTJ型人格是一种理性、独立、逻辑性强的人格类型&#xff0c;他们通常在思考问题时会非常深入&#xff0c;而且对于目标的追求非常明确。 INTJ型人格的人通常在职业发展中追求自我提升和成长&#xff0c;他们善于分析问题、制定计划和实现目标&#xff0c;具有很强的自我意识…

【JUC基础】14. ThreadLocal

目录 1、前言 2、什么是ThreadLocal 3、ThreadLocal作用 4、ThradLocal基本使用 4.1、创建和初始化 4.2、存储和获取线程变量 4.3、清理和释放线程变量 4.4、小结 4.5、示例代码 5、ThreadLocal原理 5.1、set() 5.2、get() 5.3、变量清理 5.4、ThreadLocalMap 6、…