spring6 基于xml自动装配

目录结构

代码

UserContronller.java

复制代码
package bean.auto.controller;

import bean.auto.service.UserService;
import bean.auto.service.UserServiceImpl;

public class UserContronller {

    private UserService userService;

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

    public void addUser(){
        System.out.println("controller方法执行了");
//        UserServiceImpl userService = new UserServiceImpl();
        userService.addUserService();
    }
}

UserService.interface

复制代码
package bean.auto.service;

public interface UserService {
    public void addUserService();
}

UserServiceImpl.java

复制代码
package bean.auto.service;

import bean.auto.dao.UserDao;

public class UserServiceImpl implements UserService{
    private UserDao userDao;

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

    @Override
    public void addUserService() {
        System.out.println("userservice方法执行了");
        userDao.addUserDao();
    }
}

UserDao.interface

复制代码
package bean.auto.dao;

import org.junit.platform.commons.util.PackageUtils;

public interface UserDao {
    public void addUserDao();
}

UserDaoImpl.java

复制代码
package bean.auto.dao;

public class UserDaoImpl implements UserDao{


    @Override
    public void addUserDao() {
        System.out.println("userdao方法执行了");
//        UserDaoImpl userDao = new UserDaoImpl();

    }
}

UserTest.java

复制代码
package bean.auto;

import bean.auto.controller.UserContronller;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {
    @Test
    public void test(){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean-auto.xml");
        UserContronller uc = ac.getBean("userController", UserContronller.class);
        uc.addUser();
    }
}

bean-auto.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="userController" class="bean.auto.controller.UserContronller" autowire="byType">
    </bean>
    <bean id="userService" class="bean.auto.service.UserServiceImpl" autowire="byType">
    </bean>
    <bean id="userDao" class="bean.auto.dao.UserDaoImpl" autowire="byType">
    </bean>

</beans>

控制台输出

相关推荐
wpyok1683 小时前
密钥检测错误代码xml构建
xml
林开落L3 小时前
从入门到了解:Protobuf、JSON、XML 核心解析(C++ 示例)
xml·c++·json·protobuffer·结构化数据序列化机制
介一安全18 小时前
【Web安全】XML注入全手法拆解
xml·web安全·安全性测试
2301_818732064 天前
项目启动报错,错误指向xml 已解决
xml·java·数据库·后端·springboot
csdn2015_5 天前
generatorConfig.xml 配置 Controller、Service 完整教程
xml·mybatis
特立独行的猫a5 天前
从XML到Compose的UI变革:现代(2026)Android开发指南
android·xml·ui·compose·jetpack
spencer_tseng5 天前
Stream not available [SysDictDataMapper.xml]
xml·java
qq_297574676 天前
MySQL迁移到瀚高数据库 常用转换函数对照表(附XML示例,直接复用)
xml·数据库·mysql
好好研究7 天前
SpringBoot整合SpringMVC
xml·java·spring boot·后端·mvc
从此不归路8 天前
Qt5 进阶【12】JSON/XML 数据协议处理:与后端/配置文件的对接
xml·开发语言·c++·qt·json