日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Spring Boot之Dao、Service、Controller通過注解委托給Spring容器

作者:LeBron Le 更新時間: 2022-07-16 編程語言

1. 概述

定義數據訪問組件AlphaDao、業務組件AlphaService、控制器AlphaController,并通過注解的方式將它們委托給Spring容器。

2. 數據訪問組件AlphaDao

package com.nowcoder.community.dao;

public interface AlphaDao {

    String select();

}

2.1 Dao接口的兩個實現類

AlphaDao接口有兩個實現類:AlphaDaoHibernateImplAlphaDaoMyBatisImpl
其中,由于添加了@Primary注解,所以AlphaDaoMyBatisImpl的優先級更高。

2.1.1 類AlphaDaoHibernateImpl

package com.nowcoder.community.dao;

import org.springframework.stereotype.Repository;

@Repository("alphaHibernate")
public class AlphaDaoHibernateImpl implements AlphaDao {

    @Override
    public String select() {
        return "Hibernate";
    }

}

2.1.2 類AlphaDaoMyBatisImpl

package com.nowcoder.community.dao;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

@Repository("alphaBatis")
@Primary
public class AlphaDaoMyBatisImpl implements AlphaDao {

    @Override
    public String select() {
        return "MyBatis";
    }
}

3. 業務組件AlphaService

通過注解@Autowired注入AlphaDao,然后調用該接口的select()方法。

package com.nowcoder.community.service;

import com.nowcoder.community.dao.AlphaDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Service
//@Scope("prototype")
public class AlphaService {

    @Autowired
    private AlphaDao alphaDao;

    public AlphaService() {
        System.out.println("實例化AlphaService");
    }

    @PostConstruct
    public void init() {
        System.out.println("初始化AlphaService");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("銷毀AlphaService");
    }

    public String find() {
        return alphaDao.select();
    }

}

4. 控制器AlphaController

通過注解@Autowired注入AlphaService,然后調用該接口的find()方法。

package com.nowcoder.community.controller;

import com.nowcoder.community.service.AlphaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/alpha")
public class AlphaController {

    @Autowired
    private AlphaService alphaService;

    @RequestMapping("/hello")
    @ResponseBody
    public String sayHello() {
        return "Hello Spring Boot.";
    }

    @RequestMapping("/data")
    @ResponseBody
    public String getData() {
        return alphaService.find();
    }
}

5. 開發過程思想

  1. controller處理瀏覽器請求,處理請求的過程中調用service層業務組件,業務組件會調用dao層去訪問數據庫,彼此相互依賴,就可以調用依賴注入的方式實現。
  2. controller在處理請求的過程中會先注入service,再寫一個getData()方法,即調用了service業務組件的find()方法,而service的find()方法是通過調用dao的select()方法實現的。

原文鏈接:https://blog.csdn.net/hutianle/article/details/125804362

欄目分類
最近更新