spring6 基于xml自动装配
2023-12-16 22:30:46
目录结构?
代码?
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>
?
控制台输出?
?
文章来源:https://blog.csdn.net/qq_64005599/article/details/135038271
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!