Spring 入门

  1. Spring全家桶
    •SpringFramework:基石
    •SpringBoot:构建项目
    •SpringCloud:微服务,拆分成若干个项目,便于维护,开发难度高
    •SpringCloudDataFlow:数据集成,多个客户端

2.Spring
SpringFramework
•SpringCore-IoC、AOP
•SpringDataAccess
Transactions、SpringMyBatis
•WebServlet
SpringMVC
•Integration
Email、Scheduling、AMQP、Security
2.1 IOC容器
管理bean,需要哪些Bean,这些bean的依赖关系SpringApplication.run(CommunityApplication.class, args)自动启动服务器,扫描自动配置类并加载,但不是所有配置类都生效,只要有相应的starter才会生效
扫描项目包下所有的bean
@PostConstruct
@PreDestroy
@scope,作用范围,一个还是多个实例
@Configration 配置类
@SpringBootApplication 程序入口
@Bean 第三方类

可以主动获取容器

public class CommunityApplicationTests implements ApplicationContextAware {
	private ApplicationContext applicationContext;
# ServerProperties
server.port=8080
server.servlet.context-path=/community  # 项目路径

然后使用applicationContext.getBean(AlphaService.class)获取Bean,管理Bean,使用依赖注入DI,但不推荐,直接注入
@Autowired
@Qualifier(“alphaHibernate”)

2.2 Spring MVC
Contoller 控制层
Model 模型层
View 视图层
核心组件:DispatcherServlet

2.3 Thymleaf
模板文件+Model ------->模板引擎----->Html
在开发期间模板引擎页面修改内容后,页面刷新不会有效果
需要进行以下设置

# ThymeleafProperties
spring.thymeleaf.cache=false
@RequestMapping("/http")
    public void http(HttpServletRequest request, HttpServletResponse response) {
        // 获取请求数据
        System.out.println(request.getMethod());
        System.out.println(request.getServletPath());
        Enumeration<String> enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            String value = request.getHeader(name);
            System.out.println(name + ": " + value);
        }
        System.out.println(request.getParameter("code"));
        // 返回响应数据
        response.setContentType("text/html;charset=utf-8");
        try (
                PrintWriter writer = response.getWriter();
        ) {
            writer.write("<h1>牛客网</h1>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

GET请求,向服务器请求获取数据

@RequestMapping(path = "/students", method = RequestMethod.GET)
@ResponseBody
    public String getStudents(
            @RequestParam(name = "current", required = false, defaultValue = "1") int current,
            @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
        System.out.println(current);
        System.out.println(limit);
        return "some students";
    }

required = false代表可以不传参数

// /student/123
    @RequestMapping(path = "/student/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String getStudent(@PathVariable("id") int id) {
        System.out.println(id);
        return "a student";
    }
// POST请求
    @RequestMapping(path = "/student", method = RequestMethod.POST)
    @ResponseBody
    public String saveStudent(String name, int age) {
        System.out.println(name);
        System.out.println(age);
        return "success";
    }
// 响应HTML数据
    @RequestMapping(path = "/teacher", method = RequestMethod.GET)
    public ModelAndView getTeacher() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", "张三");
        mav.addObject("age", 30);
        mav.setViewName("/demo/view");
        return mav;
    }
@RequestMapping(path = "/school", method = RequestMethod.GET)
    public String getSchool(Model model) {
        model.addAttribute("name", "北京大学");
        model.addAttribute("age", 80);
        return "/demo/view";
    }
  // 响应JSON数据(异步请求)
    // Java对象 -> JSON字符串 -> JS对象
    @RequestMapping(path = "/emp", method = RequestMethod.GET)
    @ResponseBody  //必须
    public Map<String, Object> getEmp() {
        Map<String, Object> emp = new HashMap<>();
        emp.put("name", "张三");
        emp.put("age", 23);
        emp.put("salary", 8000.00);
        return emp;
    }

//浏览器接收不能接受java对象,但可以接收json对象

  @RequestMapping(path = "/emps", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String, Object>> getEmps() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> emp = new HashMap<>();
        emp.put("name", "张三");
        emp.put("age", 23);
        emp.put("salary", 8000.00);
        list.add(emp);
        emp = new HashMap<>();
        emp.put("name", "李四");
        emp.put("age", 24);
        emp.put("salary", 9000.00);
        list.add(emp);
        emp = new HashMap<>();
        emp.put("name", "王五");
        emp.put("age", 25);
        emp.put("salary", 10000.00);
        list.add(emp);
        return list;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值