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 小时前
pugixml XML配置文件 的增删改查
xml
小狗蛋ing1 天前
反编译APK获取xml资源
xml·反编译
yqcoder1 天前
同源策略详解
xml·前端·javascript
油丶酸萝卜别吃2 天前
MyBatis中XML文件的模板
xml·数据库·mybatis
武子康2 天前
大数据-259 离线数仓 - Griffin架构 修改配置 pom.xml sparkProperties 编译启动
xml·java·大数据·hive·hadoop·架构
seasugar2 天前
Maven怎么会出现一个dependency-reduced-pom.xml的文件
xml·java·maven
SEO-狼术3 天前
Oxygen XML Developer Crack
xml
北辰浮光4 天前
[spring]XML配置文件标签
xml·spring
GoodStudyAndDayDayUp5 天前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
见欢.5 天前
XXE靶场
xml