• Spring常用注解汇总


    使用注解之前要开启自动扫描功能

    其中base-package为需要扫描的包(含子包)。

    1
    <context:component-scan base-package="cn.test"/>
    @Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。
    @Scope注解 作用域
    @Lazy(true) 表示延迟初始化
    @Service用于标注业务层组件、
    @Controller用于标注控制层组件(如struts中的action)
    @Repository用于标注数据访问组件,即DAO组件。
    @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
    @Scope用于指定scope作用域的(用在类上)

    ---------------jsr250----
    @PostConstruct用于指定初始化方法(用在方法上)
    @PreDestory用于指定销毁方法(用在方法上)

    @Resource 默认按名称装配,当找不到与名称匹配的bean才会按类型装配。


    @DependsOn:定义Bean初始化及销毁时的顺序
    @Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
    @Autowired 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
    @Autowired @Qualifier("personDaoBean") 存在多个实例配合使用

    @PostConstruct 初始化注解
    @PreDestroy 摧毁注解 默认 单例 启动就加载??
    @Async异步方法调用,需要添加以下代码:
    1
    2
    3
    4

    5 task:annotation-driven/
    为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsync

    在Spring中,基于@Async标注的方法,称之为异步方法;

    这些方法将在执行的时候,将会在独立的线程中被执行,

    调用者无需等待它的完成,

    即可继续其他的操作。

    @Async所修饰的函数不要定义为static类型,这样异步调用不会生效

    @ComponentScan(basePackages = "com.xzc.")
    @EnableAutoConfiguration
    @SpringBootApplication
    @PropertySource({"classpath:application.properties", "classpath:xzc.properties"})
    @ImportResource("classpath:ws-client.xml")
    @EnableRedisHttpSession
    @EnableAspectJAutoProxy
    @EnableCaching
    @EnableAsync
    @Configuration
    @EnableScheduling 启动定时任务

    @Entity注释指名这是一个实体Bean
    @SuppressWarnings注解?

    1、 @PathVariable
    当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上

    @Controller
    @RequestMapping("/owners/{ownerId}")
    public class RelativePathUriTemplateController {

    @RequestMapping("/pets/{petId}")
    public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
    // implementation omitted
    }
    }
    上面代码把URI template 中变量 ownerId的值和petId的值,绑定到方法的参数上。

    若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称。

  • 相关阅读:
    Python Django开发遇到的坑(版本不匹配)
    Mysql安装与问题合集
    git branch -r查看不了远程所有分支
    angularJS使用$http请求下载excel表格
    遍历formData对象数据
    按需使用CryptoJS之AES加密(CFB)模式
    git之创建、删除分支
    git pull时报错:Access Denied (拒绝访问)
    angularJS监听数据变化
    Angular-ui-router入门
  • 原文地址:https://www.cnblogs.com/glb79809-glb/p/14089900.html
Copyright © 2020-2023  润新知