zoukankan      html  css  js  c++  java
  • Spring常用注解介绍【经典总结】

    Spring常用注解介绍【经典总结】

    Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式。
    Spring注解方式减少了配置文件内容,更加便于管理,并且使用注解可以大大提高了开发效率!

    下面讲解Spring中常用的一些注解。

    注解类介绍

    @Component :标准一个普通的spring Bean类。
    @Repository:标注一个DAO组件类。
    @Service:标注一个业务逻辑组件类。
    @Controller:标注一个控制器组件类。

    这些都是注解在平时的开发过程中出镜率极高,@Component、@Repository、@Service、@Controller实质上属于同一类注解,用法相同,功能相同,区别在于标识组件的类型。@Component可以代替@Repository、@Service、@Controller,因为这三个注解是被@Component标注的。如下代码

    1 @Target({ElementType.TYPE})
    2 @Retention(RetentionPolicy.RUNTIME)
    3 @Documented
    4 @Component
    5 public @interface Controller {
    6     String value() default "";
    7 }

    (1)当一个组件代表数据访问层(DAO)的时候,我们使用@Repository进行注解,如下

    1 @Repository
    2 public class HappyDaoImpl implements HappyDao{
    3 private final static Logger LOGGER = LoggerFactory.getLogger(HappyDaoImpl .class);
    4 public void  club(){
    5         //do something ,like drinking and singing
    6     }
    7 }

    (2)当一个组件代表业务层时,我们使用@Service进行注解,如下

    项目实际应用:

     1 @Service
     2 @Transactional(readOnly = true)
     3 public class ChartChineseOceanService extends CrudService<ChartChineseOceanDao, ChartChineseOcean> {
     4 
     5     public ChartChineseOcean get(String id) {
     6         return super.get(id);
     7     }
     8     
     9     public List<ChartChineseOcean> findList(ChartChineseOcean chartChineseOcean) {
    10         return super.findList(chartChineseOcean);
    11     }
    12     
    13     public Page<ChartChineseOcean> findPage(Page<ChartChineseOcean> page, ChartChineseOcean chartChineseOcean) {
    14         return super.findPage(page, chartChineseOcean);
    15     }
    16     
    17     @Transactional(readOnly = false)
    18     public void save(ChartChineseOcean chartChineseOcean) {
    19         super.save(chartChineseOcean);
    20     }
    21     
    22     @Transactional(readOnly = false)
    23     public void delete(ChartChineseOcean chartChineseOcean) {
    24         super.delete(chartChineseOcean);
    25     }
    26     
    27 }

    (3)当一个组件作为前端交互的控制层,使用@Controller进行注解,如下

    1 @Controller
    2 public class HappyController {
    3     @Autowired //下面进行讲解
    4     private ClubService clubService;
    5 
    6     // Control the people entering the Club
    7     // do something
    8 }
    9 /*Controller相关的注解下面进行详细讲解,这里简单引入@Controller*/

    实际应用:

     1 /**
     2  * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
     3  */
     4 package com.thinkgem.jeesite.modules.tpydg.chart.web;
     5 
     6 import javax.servlet.http.HttpServletRequest;
     7 import javax.servlet.http.HttpServletResponse;
     8 
     9 import org.apache.shiro.authz.annotation.RequiresPermissions;
    10 import org.springframework.beans.factory.annotation.Autowired;
    11 import org.springframework.stereotype.Controller;
    12 import org.springframework.ui.Model;
    13 import org.springframework.web.bind.annotation.ModelAttribute;
    14 import org.springframework.web.bind.annotation.RequestMapping;
    15 import org.springframework.web.bind.annotation.RequestParam;
    16 import org.springframework.web.servlet.mvc.support.RedirectAttributes;
    17 
    18 import com.thinkgem.jeesite.common.config.Global;
    19 import com.thinkgem.jeesite.common.persistence.Page;
    20 import com.thinkgem.jeesite.common.web.BaseController;
    21 import com.thinkgem.jeesite.common.utils.StringUtils;
    22 import com.thinkgem.jeesite.modules.tpydg.chart.entity.ChartSchoolEnrolment;
    23 import com.thinkgem.jeesite.modules.tpydg.chart.service.ChartSchoolEnrolmentService;
    24 
    25 import java.util.List;
    26 
    27 /**
    28  * 岛国入学率Controller
    29  * @author zkx
    30  * @version 2018-05-15
    31  */
    32 @Controller
    33 @RequestMapping(value = "${adminPath}/chart/chartSchoolEnrolment")
    34 public class ChartSchoolEnrolmentController extends BaseController {
    35 
    36     @Autowired
    37     private ChartSchoolEnrolmentService chartSchoolEnrolmentService;
    38     
    39     @ModelAttribute
    40     public ChartSchoolEnrolment get(@RequestParam(required=false) String id) {
    41         ChartSchoolEnrolment entity = null;
    42         if (StringUtils.isNotBlank(id)){
    43             entity = chartSchoolEnrolmentService.get(id);
    44         }
    45         if (entity == null){
    46             entity = new ChartSchoolEnrolment();
    47         }
    48         return entity;
    49     }
    50     
    51     @RequiresPermissions("chart:chartSchoolEnrolment:view")
    52     @RequestMapping(value = {"list", ""})
    53     public String list(ChartSchoolEnrolment chartSchoolEnrolment, HttpServletRequest request, HttpServletResponse response, Model model) {
    54         Page<ChartSchoolEnrolment> enrolmentPage = new Page<ChartSchoolEnrolment>(request, response);
    55         enrolmentPage.setOrderBy("year DESC");
    56         Page<ChartSchoolEnrolment> page = chartSchoolEnrolmentService.findPage(enrolmentPage, chartSchoolEnrolment);
    57         model.addAttribute("page", page);
    58         return "tpydg/chart/chartSchoolEnrolmentList";
    59     }
    60 
    61     @RequiresPermissions("chart:chartSchoolEnrolment:view")
    62     @RequestMapping(value = "form")
    63     public String form(ChartSchoolEnrolment chartSchoolEnrolment, Model model) {
    64         model.addAttribute("chartSchoolEnrolment", chartSchoolEnrolment);
    65         return "tpydg/chart/chartSchoolEnrolmentForm";
    66     }
    67 
    68     @RequiresPermissions("chart:chartSchoolEnrolment:edit")
    69     @RequestMapping(value = "save")
    70     public String save(ChartSchoolEnrolment chartSchoolEnrolment, Model model, RedirectAttributes redirectAttributes) {
    71         if (!beanValidator(model, chartSchoolEnrolment)){
    72             return form(chartSchoolEnrolment, model);
    73         }
    74         ChartSchoolEnrolment schoolEnrolment = new ChartSchoolEnrolment();
    75         schoolEnrolment.setYear(chartSchoolEnrolment.getYear());
    76         List<ChartSchoolEnrolment> list = chartSchoolEnrolmentService.findList(schoolEnrolment);
    77         if(StringUtils.isBlank(chartSchoolEnrolment.getId())&&! list.isEmpty()){
    78             addMessage(redirectAttributes, "添加失败,"+chartSchoolEnrolment.getYear()+"年数据已经存在!");
    79         } else{
    80             chartSchoolEnrolmentService.save(chartSchoolEnrolment);
    81             addMessage(redirectAttributes, "添加成功");
    82         }
    83         return "redirect:"+Global.getAdminPath()+"/chart/chartSchoolEnrolment/?repage";
    84     }
    85     
    86     @RequiresPermissions("chart:chartSchoolEnrolment:edit")
    87     @RequestMapping(value = "delete")
    88     public String delete(ChartSchoolEnrolment chartSchoolEnrolment, RedirectAttributes redirectAttributes) {
    89         chartSchoolEnrolmentService.delete(chartSchoolEnrolment);
    90         addMessage(redirectAttributes, "删除岛国入学率成功");
    91         return "redirect:"+Global.getAdminPath()+"/chart/chartSchoolEnrolment/?repage";
    92     }
    93 
    94 }

    4.@Autowired   @Autowired:属于Spring 的org.springframework.beans.factory.annotation包下,可用于为类的属性、构造器、方法进行注值

    实际应用:

     1    @Autowired
     2     private ChartPopulationGrowthService chartPopulationGrowthService;
     3     @Autowired
     4     private ChartSchoolEnrolmentService chartSchoolEnrolmentService;
     5     @Autowired
     6     private ChartExportRateService chartExportRateService;
     7     @Autowired
     8     private ChartForeignInvestmentService chartForeignInvestmentService;
     9     @Autowired
    10     private  ChartGdpCompositionService chartGdpCompositionService;
    11     @Autowired
    12     private ChartGdpGrowthRateService chartGdpGrowthRateService;
    13     @Autowired
    14     private ChartGdpPurchaseDiscountService chartGdpPurchaseDiscountService;

    5.@RequestMapping : 这个注解用于将url映射到整个处理类或者特定的处理请求的方法。可以只用通配符!

    @RequestMapping 既可以作用在类级别,也可以作用在方法级别。当它定义在类级别时,标明该控制器处理所有的请求都被映射到 /favsoft 路径下。@RequestMapping中可以使用 method 属性标记其所接受的方法类型,如果不指定方法类型的话,可以使用 HTTP GET/POST 方法请求数据,但是一旦指定方法类型,就只能使用该类型获取数据。

    实际应用:

     1 @RequestMapping(value = {"list", ""})
     2     public String list(ChartPopulationGrowth chartPopulationGrowth, HttpServletRequest request, HttpServletResponse response, Model model) {
     3         Page<ChartPopulationGrowth> growthPage = new Page<ChartPopulationGrowth>(request, response);
     4         growthPage.setOrderBy("year DESC");
     5         Page<ChartPopulationGrowth> page = chartPopulationGrowthService.findPage(growthPage, chartPopulationGrowth);
     6         model.addAttribute("page", page);
     7         return "tpydg/chart/chartPopulationGrowthList";
     8     }
     9 
    10     @RequiresPermissions("chart:chartPopulationGrowth:view")
    11     @RequestMapping(value = "form")
    12     public String form(ChartPopulationGrowth chartPopulationGrowth, Model model) {
    13         model.addAttribute("chartPopulationGrowth", chartPopulationGrowth);
    14         return "tpydg/chart/chartPopulationGrowthForm";
    15     }
    16 
    17     @RequiresPermissions("chart:chartPopulationGrowth:edit")
    18     @RequestMapping(value = "save")
    19     public String save(ChartPopulationGrowth chartPopulationGrowth, Model model, RedirectAttributes redirectAttributes) {
    20         if (!beanValidator(model, chartPopulationGrowth)){
    21             return form(chartPopulationGrowth, model);
    22         }
    23 
    24         ChartPopulationGrowth search = new ChartPopulationGrowth();
    25         search.setYear(chartPopulationGrowth.getYear());
    26         List<ChartPopulationGrowth> list = chartPopulationGrowthService.findList(search);
    27         if(StringUtils.isBlank(chartPopulationGrowth.getId())&&!list.isEmpty()){
    28             addMessage(redirectAttributes,"添加失败,"+chartPopulationGrowth.getYear()+"年数据已经存在!");
    29         }else{
    30             chartPopulationGrowthService.save(chartPopulationGrowth);
    31             addMessage(redirectAttributes, "添加成功");
    32         }
    33         return "redirect:"+Global.getAdminPath()+"/chart/chartPopulationGrowth/?repage";
    34     }
    35     
    36     @RequiresPermissions("chart:chartPopulationGrowth:edit")
    37     @RequestMapping(value = "delete")
    38     public String delete(ChartPopulationGrowth chartPopulationGrowth, RedirectAttributes redirectAttributes) {
    39         chartPopulationGrowthService.delete(chartPopulationGrowth);
    40         addMessage(redirectAttributes, "删除岛国 人口增长成功");
    41         return "redirect:"+Global.getAdminPath()+"/chart/chartPopulationGrowth/?repage";
    42     }
    43 
    44     @RequiresPermissions("chart:chartPopulationGrowth:edit")
    45     @RequestMapping(value = "import")

    6.@RequestParam :将请求的参数绑定到方法中的参数上,有required参数,默认情况下,required=true,也就是改参数必须要传。如果改参数可以传可不传,可以配置required=false。

    1  @RequestMapping("/happy")
    2   public String sayHappy(
    3   @RequestParam(value = "name", required = false) String name,
    4   @RequestParam(value = "age", required = true) String age) {
    5   //age参数必须传 ,name可传可不传
    6   ...
    7   }

    以上就是开发中用的最多的注解啦 

    参照博客:

    https://blog.csdn.net/u010648555/article/details/76299467

  • 相关阅读:
    用GDB调试程序(一)
    关于“鸡脚神”的看法
    Oracle 经典SQL 专为笔试准备
    怎样设计接口?
    myeclipse6.0下载及注冊码
    VB连接Mysql数据库
    开源html5_kiwijs_helloworld
    server宕机监控、检測、报警程序(139绑定手机短信报警)monitor_down.sh
    js实现自己定义鼠标右键-------Day45
    C/C++程序猿必须熟练应用的开源项目
  • 原文地址:https://www.cnblogs.com/zhukaixin/p/9172996.html
Copyright © 2011-2022 走看看