编写第一个spring程序
程序员文章站
2024-01-03 20:58:58
1.创建一个maven工程,在DAO层创建接口和对应的实现类package com.zhu.dao;public interface UserDao { void getUser();}package com.zhu.dao;public class UserDaoImpl implements UserDao{ @Override public void getUser() { System.out.println("默认获取用户的数据");...
1.创建一个maven工程,在DAO层创建接口和对应的实现类
package com.zhu.dao;
public interface UserDao {
void getUser();
}
package com.zhu.dao;
public class UserDaoImpl implements UserDao{
@Override
public void getUser() {
System.out.println("默认获取用户的数据");
}
}
package com.zhu.dao;
public class UserDaoImpl2 implements UserDao{
@Override
public void getUser() {
System.out.println("默认获取用户的数据2");
}
}
package com.zhu.dao;
public class UserDaoImpl3 implements UserDao{
@Override
public void getUser() {
System.out.println("默认获取用户的数据3");
}
}
package com.zhu.dao;
public class UserDaoImpl4 implements UserDao{
@Override
public void getUser() {
System.out.println("默认获取用户的数据4");
}
}
2.在pom.xml文件中导入依赖包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
3.在Service层创建接口和实现类
package com.zhu.service;
public interface UserService {
void getUser();
}
package com.zhu.service;
import com.zhu.dao.UserDao;
import com.zhu.dao.UserDaoImpl;
import com.zhu.dao.UserDaoImpl2;
import com.zhu.dao.UserDaoImpl3;
public class UserServiceImpl implements UserService{
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void getUser() {
userDao.getUser();
}
}
4.创建bean配置文件,并且将实现类配置在bean文件中
<?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-3.0.xsd">
<bean id="UserDaoImpl1" class="com.zhu.dao.UserDaoImpl"></bean>
<bean id="UserDaoImpl2" class="com.zhu.dao.UserDaoImpl2"></bean>
<bean id="UserDaoImpl3" class="com.zhu.dao.UserDaoImpl3"></bean>
<bean id="UserDaoImpl4" class="com.zhu.dao.UserDaoImpl4"></bean>
<bean id="UserServiceImpl" class="com.zhu.service.UserServiceImpl">
<property name="userDao" ref="UserDaoImpl1"></property>
</bean>
</beans>
5.创建一个测试类执行程序
import com.zhu.dao.UserDao;
import com.zhu.dao.UserDaoImpl;
import com.zhu.dao.UserDaoImpl2;
import com.zhu.dao.UserDaoImpl4;
import com.zhu.service.UserService;
import com.zhu.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args){
// UserServiceImpl userService =new UserServiceImpl();
// userService.setUserDao(new UserDaoImpl());
// userService.getUser();
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userService = (UserServiceImpl) context.getBean("UserServiceImpl");
userService.getUser();
}
}
本文地址:https://blog.csdn.net/weixin_45723003/article/details/111937188