面试官:为什么要用 Spring?它到底解决了什么问题?
java1234
共 2441字,需浏览 5分钟
·
2022-01-06 18:37
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
/**
* @author :Prannt
* @description :Service层
* @program : Test
* @date :2021/1/30 9:16
*/
public class UserService {
public void add(){
System.out.println("service add...");
UserDao dao = new UserDaoImpl();
dao.query();
}
}
/**
* @description :接口中只有一个抽象方法
*/
public interface UserDao {
void query();
}
/**
* @description :接口的实现类,重写 query方法
*/
public class UserDaoImpl implements UserDao{
@Override
public void query() {
System.out.println("dao query...");
}
}
//测试
public class test {
@Test
public void test1(){
UserService service = new UserService();
service.add();
}
}
"1.0" encoding="UTF-8"?>
"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">
"userService" class="Service.UserService">
"dao" ref="Dao">
"Dao" class="Dao.UserDaoImpl">
import Dao.UserDao;
/**
* @author :Prannt
* @description :
* @program : Demo01
* @date :2021/1/30 8:40
*/
public class UserService {
public void add(){
System.out.println("service add......");
dao.update();
}
//创建dao类型的属性,生成对应的set方法
private UserDao dao;
public void setDao(UserDao dao) {
this.dao = dao;
}
}
public interface UserDao {
void update();
}
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("Dao update......");
}
}
import Service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
@Test
public void testBean(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
UserService userService = context.getBean("userService",UserService.class);
userService.add();
}
}
作者:Prannt
来源:csdn.net/qq_38050259/article/details/113414419
加锋哥微信: java1239 围观锋哥朋友圈,每天推送Java干货!
评论