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>

控制台输出

相关推荐
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ3 小时前
日志打印配置:logback-spring.xml配置;info和error完全区分了,并且按时间拆分了
xml·spring·logback
Android技术之家11 小时前
实测:Jetpack Compose 替代 XML 布局,3 步实现高性能界面迁移
xml
汐ya~12 小时前
提示词工程:AI 总误解指令?用XML标签提升3倍准确率
xml·人工智能·prompt·提示词工程·大模型llm
hahjee12 小时前
libxslt XSLT转换库:鸿蒙PC上的XML转换工具
xml·华为·harmonyos
TH_11 天前
10、xml的CDATA标签之AI模型
xml
拾忆,想起2 天前
Dubbo多协议暴露完全指南:让一个服务同时支持多种通信方式
xml·微服务·性能优化·架构·dubbo
春蕾夏荷_7282977253 天前
c++ 将xml数据写入sqlite数据库
xml·数据库
秦奈3 天前
Unity复习学习随笔(六):XML数据
xml·学习
jiayong234 天前
Spring XML解析与BeanDefinition注册详解
xml·java·spring
就叫飞六吧4 天前
Spring 框架中的 Bean 继承:`parent` 属性 (XML配置)
xml·java·spring