题目
在Spring项目中,通过main方法获取到Controller类,Controller通过注入的方式调用Service类,Service再通过注入的方式获取到Repository类,Repository类里面有一个方法构建一个User对象,返回给main方法。Repository无需连接数据库,使用伪代码即可。
思路
两种实现方法:①从上层到下层;②从下层到上层。
分层设计:
代码
pom.xml中添加Spring框架支持
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-3</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
</project>
在resource下添加Spring配置文件spring-config.xml并设置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"
xmlns:content="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--设置需要存储到spring中的bean根目录-->
<content:component-scan base-package="com.beans"></content:component-scan>
</beans>
分层实现代码
package com.beans.controller;
import com.beans.service.UserService;
import com.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
@Autowired
private UserService userService;
public User getUser() {
return userService.getUser();
}
}
package com.beans.repository;
import com.model.User;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository {
public User getUser() {
//伪代码
User user = new User();
user.setId(1);
user.setName("张三");
user.setPassword("123");
return user;
}
}
package com.beans.service;
import com.beans.repository.UserRepository;
import com.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User getUser() {
return userRepository.getUser();
}
}
package com.model;
/**
* 普通实体类
*/
public class User {
private int id;
private String name;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
import com.beans.controller.UserController;
import com.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
//1.获取Spring上下文
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
//2.得到bean
UserController userController = context.getBean(UserController.class);
//3.使用bean
User user = userController.getUser();
System.out.println(user);
}
}