首页 > 其他分享 >Spring注解开发

Spring注解开发

时间:2022-11-16 20:37:56浏览次数:44  
标签:RequestMapping Spring xxx springframework 开发 注解 org import

1、使用注解需要导入的依赖

  • 1、1在application.xml文件中加入该约束

xmlns:context=http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd

 并且需要加入标签开启该注解

<context:annotation-config/>
或指定要扫描的包,包下的注解就会生效
<context:component-scan base-package="com.kuang.pojo"/>

 最终xml代码

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
</beans>
  • 1、2对应注解含义

@Autowired 自动装配,优先匹配类型,后名字
@Qualifier(value = "xxx")可指定自动装配的id
@Resource(name = "xxx") 自动装配,优先名字,后类型,也可指定名称
@Nullable 该注解后的参数可为空
@Component 组件,放在类上,说明该类被spring管理了,就是bean
    mvc三层架构:
        dao:@Repository
        service:@Service
        Controller:@Controller
功能一样,都是将该类注册到spring中,装配bean
该注解需配合<context:component-scan base-package="com.kuang.dao"/>扫包进行使用
任需ClassPathXmlApplicationContext,无法脱离配置文件
@Value("xxx")该注解用于给属性进行注入,也能够直接注入与set方法中
@Scope("xxx")该注解指定对应类的作用域,是单例模式或原型模式或其它


lombok包下的快速生成实体类对象的注解{
    @NoArgsConstructor快速创建无参构造
    @AllArgsConstructor 快速创建有参构造
    @Data 快速创建get,set
}

  spring4之后要使用注解必须导入aop包,若发现注解无效,可查看是否导入该包

使用java配置spring,完全舍弃spring的xml配置文件



@Configuration:将类指定为spring配置类
@Bean:指定该方法为xml中相当于<bean> 需返回一个实体类
@ComponentScan("xxxx"):使该配置类只扫描到指定的包下
@Import({xxx.class}):合并多个配置类

SpingMVC注解开发

@RequestMapping("/xxx"):该注解可映射一个访问路径,在单个方法上时直接访问 http://localhost:8080/xxx
						在类上时访问需加上类的访问路径 http://localhost:8080/类上的映射名/xxx
在返回单纯的数据时,它可以进行乱码解析
    @RequestMapping(value = "/sda",produces = "application/json;charset=utf-8")

RestFul风格

@PathVariable
加在参数前,可定义为路径变量

未使用前

package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RestfulTest {
    @RequestMapping("restful")
    public String restful(int a, int b, Model model){
        int c = a+b;

        model.addAttribute("msg",c);

        return "hello";
    }
}

nam

使用后

package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RestfulTest {
    @RequestMapping("/restful/{a}/{b}")
    public String restful(@PathVariable int a, @PathVariable int b, Model model){
        int c = a+b;

        model.addAttribute("msg",c);

        return "hello";
    }
}

restful是一种风格,并非规范或标准

restful指定访问方式

@RequestMapping(value

value可换成path,禁止使用name,会出问题

package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RestfulTest {
    @RequestMapping(value = "/restful/{a}/{b}",method = RequestMethod.GET)
    public String restful(@PathVariable int a, @PathVariable int b, Model model){
        int c = a+b;

        model.addAttribute("msg",c);

        return "hello";
    }
}

通过在注解中选择method可以指定通过什么方式来进行访问该路径才能得到对应的方法。

通过另外的注解也能实现对应的效果

@RequestMapping(name = "/restful/{a}/{b}",method = RequestMethod.GET)

//get方法可以用

@GetMapping("xxx")
//相同的,也有DeleteMapping等对应的注解可以实现method = RequestMethod.xxx

使用GetMapping注解接收前端参数,可直接从参数中获取,也可使用注解指定参数名

@GetMapping("/t1")
public ModelAndView he(@RequestParam("hs")String hs,User user){
    System.out.println(user);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("msg",user+"\n"+hs);
    modelAndView.setViewName("hello");
    return modelAndView;
}

@RequestParam("xxx") 指定该参数接收时的参数名必须为xxx

@Param("xxx")也可给指定参数一个别名

向前端返回数据,绕过视图解析器

在方法上写上@ResponseBody添加该注解,则绕过视图解析器,仅返回数据,不跳转视图

在类上添加@RestController注解,该类下的所有方法都只会返回数据,不跳转视图

Qualifier

@Qualifier

当bean中存在多个BookService类型对象时,搭配@Qualifier(“实现类名称”)表明注入的是哪一个具体实现类的bean(与 @Autowired配合使用加以区分)

标签:RequestMapping,Spring,xxx,springframework,开发,注解,org,import
From: https://www.cnblogs.com/nhgtx/p/16897392.html

相关文章

  • Spring--注解开发定义Bean
    注解开发先看一看之前的bean的做法:所谓注解开发,当然就要用到注解啊,就是在BookDao接口的实现类里面进行注解的定义如图所示:而在.xml文件里面,就需要进行这样一个操作:......
  • 小程序开发:用原生还是选框架(wepy/mpvue/uni-app/taro)?
    小程序开发:用原生还是选框架(wepy/mpvue/uni-app/taro)?自2017-1-9微信小程序诞生以来,历经2年多的迭代升级,已有数百万小程序上线,成为继Web、iOS、Android之后,第四大主流开发技......
  • 如何开发一款即时通讯软件?看这六个项目就够了
     即时通讯软件(IM)发展到今天功能已经越来越齐全,我们的日常生活中不管是社交、网上购物还是工作都已经离不开即时通讯软件。今天小编就为大家推荐六个即时通讯的开源项目,分别......
  • 程序员必学的项目管理知识-敏捷开发
    敏捷开发的目的敏捷开发的目的是快速响应市场需求,举个例子,如果一个产品的开发周期为一年,如果等到尽善尽美再上线,那么时间周期是非常漫长的,在这个过程中,用户需求也会发生很......
  • 12.Seata:Spring Cloud Alibaba分布式事务组件(非常详细)
    随着业务的不断发展,单体架构已经无法满足我们的需求,分布式微服务架构逐渐成为大型互联网平台的首选,但所有使用分布式微服务架构的应用都必须面临一个十分棘手的问题,那就是......
  • Spring--数据库资源管理遗留问题
    遗留问题的解决在我们要再试一试其他属性的时候,就出现了一些小问题:定义的情况下,在.xml文件里面调用:却发现输出是这样的:这完全不对等啊!之后发现是系统的值,优先级要高于......
  • Java @Data注解
    1、@Data注解是lombok.jar包下的注解,该注解通常用在实体bean上,不需要写出set和get方法,但是具备实体bean所具备的方法,简化编程提高变成速度。 2、@Data相当于@Getter@Sett......
  • Spring--案例:数据源对象管理
    案例:数据源对象管理对于已经学过数据库的我来说,这看起来就像是连接数据库的操作;就像javaweb项目里面的db.properties文件的使用一样,我们需要先导入一个包,(我用的是Maven项......
  • spring boot 使用webflux全局拦截,类似404错误
    背景要拦截类似404这种返回,添加日志返回码。所以要全局拦截404或者500返回实现1.定义拦截类packagecom.cmb.zhaohu.WebLogCollect.advice;importjava.util.LinkedH......
  • 开发访问k8s集群的几种方法(路由和kt)
    现状k8s集群内是有一套完整网络环境,我们不能直接通过IP访问到k8s集群内的pod或者service,只能通过nodeport或者ingress才能访问到服务.痛点开发人员进行微服务开发的......