【JavaEE】Spring小练习——存储和获取对象

一、题目:

在 Spring 项目中,通过 main 方法获取到 Controller 类,调用 Controller 里面通过注入的方式调用Service 类,Service 再通过注入的方式获取到 Repository 类,Repository 类里面有一个方法构建⼀个 User 对象,返回给 main 方法。Repository 无需连接数据库,使用伪代码即可。

二、实现:

1、前置工作

(1)创建一个Maven项目

在这里插入图片描述

(2)添加Spring框架到pom.xml

在这里插入图片描述

(3)在resources下新建spring-config.xml,配置Spring

在这里插入图片描述
(3)创建好所需要的类
在这里插入图片描述

2、具体实现

(1)User类

package com.java.Ethan.Model;

import org.springframework.stereotype.Component;

public class User {
    private int id;
    private String name;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

(2)UserRepository类

package com.java.Ethan.Repository;

import com.java.Ethan.Model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {
    public User getUser() {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        return user;
    }
}

(3)UserService类

package com.java.Ethan.Service;

import com.java.Ethan.Model.User;
import com.java.Ethan.Repository.UserRepository;
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();
    }
}

(4)UserController类

package com.java.Ethan.Controller;

import com.java.Ethan.Model.User;
import com.java.Ethan.Service.UserService;
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();
    }
}

(5)启动类

import com.java.Ethan.Controller.UserController;
import com.java.Ethan.Model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        UserController userController = context.getBean("userController", UserController.class);
        System.out.println(userController.getUser().toString());
    }
}

(6)运行结果

在这里插入图片描述

3、分析

(1)为什么main方法不能通过注入的方式获得UserController?

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值