spring6 基于xml自动装配

目录结构 

spring6 基于xml自动装配_第1张图片

代码 

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




    
    
    
    
    
    

 

控制台输出 

spring6 基于xml自动装配_第2张图片

 

你可能感兴趣的:(Javaee,xml)