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>

控制台输出

相关推荐
那个失眠的夜2 天前
Spring 的纯注解配置
xml·java·数据库·后端·spring·junit
mobai72 天前
使用pyang将yang模型转换为xml
xml·运维·服务器
我不是8神3 天前
xml配置文件知识点总结
xml
一叶龙洲4 天前
Java中使用模板引擎(FreeMarker / Velocity) + Word XML导出复杂Word
xml·java·word
Mike_6665 天前
txt_json和xml_json
xml·python·json
20YC编程社区5 天前
一分钟了解XML语言格式,使用场景,和它的优缺点
xml
Kay.Wen5 天前
LabVIEW 转换 XML文本
xml·labview
练习时长一年5 天前
浅谈assembly插件打包分发机制
xml
历程里程碑7 天前
Protobuf vs JSON vs XML:小白该怎么选?
xml·大数据·数据结构·elasticsearch·链表·搜索引擎·json
那个失眠的夜8 天前
Mybatis延迟加载策略
xml·java·数据库·maven·mybatis