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();
}
}
package bean.auto.service;
public interface UserService {
public void addUserService();
}
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();
}
}
package bean.auto.dao;
import org.junit.platform.commons.util.PackageUtils;
public interface UserDao {
public void addUserDao();
}
package bean.auto.dao;
public class UserDaoImpl implements UserDao{
@Override
public void addUserDao() {
System.out.println("userdao方法执行了");
// UserDaoImpl userDao = new UserDaoImpl();
}
}
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();
}
}
<?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>
?