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>

控制台输出

相关推荐
小百菜1 天前
dom4j实现xml转map,xml转json字符串
xml·json·xml转map·xml转json
玄客)1 天前
XML标记语言
xml
 嘘 2 天前
文件操作:Xml转Excel
xml·python·excel
q567315232 天前
使用 Python 编辑 XML 文件中的文本字段
xml·java·数据库·python·sqlite
爱敲代码的小冰2 天前
java mapper 的 xml讲解
xml·java·mybatis
子非鱼9213 天前
【Ajax】原生Ajax与jQuery中的Ajax
xml·ajax·node.js·jquery
天天进步20153 天前
X2JS: XML与JSON的完美转换工具
xml·json
法迪3 天前
解读JobScheduler的jobs.xml
xml·功耗
残花月伴3 天前
配置文件格式(xml、properties、yml/yaml)
xml·java
zhangxiang_ouc3 天前
Caché/IRIS file2Xml 一个将文件转换成Studio导出的xml格式的工具
xml