Spring相关注解总结

目录

@Configuration

@Component

@ComponentScan

@Bean

@Scope

@Lazy

生命周期函数方法调用

@PostConstruct

@PreDestory

依赖注入

@Autowired

@Qualifier

@Resource

SpringMVC

@Repository

@Mapper

@MapperScan

@Service

@Controller

@Value

 @PropertySource

SpringAOP

@EnableAspectJAutoProxy

@Aspect

@Pointcut

AOP通知注解

@AfterReturning

@AfterThrowing 

@Order

SpringMVC

@RequestMapping

@ResponseBody

@PathVariable

 @RestController

@CrossOrigin

 @PostMapping

@RequestBody

lombok

@Data

@Accessors

@NoArgsConstructor

@RequiredArgsConstructor

@AllArgsConstructor

其他

@ExceptionHandler

@TableName

@TableId

@TableField

@Param


@Configuration

1.将当前类标识为配置类

2.实际上当我们查看@Configuration源码时,会发现如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {

	
	@AliasFor(annotation = Component.class)
	String value() default "";

	
	boolean proxyBeanMethods() default true;

}

也就是说,@Configuration与@Component的功能并无区别,配置类是人为做出的规定,后面的@Service和@Controller同理 

eg:

package com.jt.config;

import com.jt.demo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration //将当前类标识为配置类
public class SpringConfig {//xml
    /**
     *  1.xml形式
     *      <bean id="user" class="com.jt.demo.User"></bean>
     *  2.注解形式
     *     Map集合的结构 Map<方法名,方法的返回值>
     *     @Bean 将方法的返回值,交给Spring容器管理.
     */
    @Bean
    public User user(){

        return new User(); //相当于xml反射机制创建对象
    }
}

@Component

1.将当前的类,交给Spring容器管理, 对象的创建是由Spring通过反射机制自动创建对象.

2.@ComponentScan(“com.jt”) 指定扫描的包路径, 可以扫描它的子孙包,用在配置类中

eg:

package com.jt.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
//准备一个配置类
@Configuration
@ComponentScan("com.jt")
//当spring容器启动时,根据指定的包路径,扫描其子孙包
public class SpringConfig {


}

@ComponentScan

1.作用同@Component,后面加扫描路径,可以避免一个一个配置,通常加到配置类上

eg:

package com.jt.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.jt")
public class SpringConfig {
}

@Bean

1.将自己方法的返回值交给Spring容器管理

@Scope

1.控制对象单例/多例

eg:

package com.jt.config;

import com.jt.demo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration  //标识这是配置类
@ComponentScan("com.jt")
public class SpringConfig {

    @Bean
    @Scope("singleton")     //默认值 单例模式
    //@Scope("prototype")   //      多例模式
    public User user(){
        return new User();
    }
}

@Lazy

1.添加表示改为懒加载

2.只要对象是多例模式,则都是懒加载,在单例模式中控制懒加载才有效

3.服务器启动时,如果加载太多的资源,则必然导致服务器启动慢, 适当的将不重要的资源设置为懒加载

4.有时用户会需要一些特殊的"链接",而这些链接的创建需要很长的时间.可以使用懒加载.

eg:

package com.jt.config;

import com.jt.demo.User;
import org.springframework.context.annotation.*;

@Configuration  //标识这是配置类
@ComponentScan("com.jt")
public class SpringConfig {

    @Bean
    //@Scope("singleton")    //默认值 单例模式
    //@Scope("prototype")    //  多例模式
    @Lazy                    //懒加载
    public User user(){
        return new User();
    }
}

生命周期函数方法调用

@PostConstruct

在对象创建之后立即调用

@PreDestory

对象消亡时 进行调用

eg:

package com.jt.demo;

import org.springframework.stereotype.Component;

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

@Component  //将对象交给Spring容器管理 key=person value:反射对象
public class Person {

    public Person(){
        System.out.println("张三出生了,资质拉满");
    }

    @PostConstruct  //在对象创建之后立即调用
    public void init(){
        System.out.println("张三称为少年奇才");
    }

    //业务方法
    public void doWork(){
        System.out.println("迎娶美人鱼!!!");
    }

    @PreDestroy //对象消亡时 进行调用
    public void destory(){
        System.out.println("销毁:全世界哀悼");
    }
}

依赖注入

@Autowired

说明: 在对象中如果需要使用属性注入.一般使用@Autowired注解.
功能: 可以将Spring容器中的对象,自动注入到属性中.
注入方式:
1. 默认按照类型注入. 如果注入的属性是接口,则自动注入实现类
2. 按照名称注入(key). 一般不用

重要前提: 如果需要依赖注入.则对象必须交给Spring容器管理.
eg:

package com.jt.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component  //将user交给Spring容器管理
public class User {

    //效果: 将当前接口的实现类自动注入
    @Autowired
    private Pet pet;

    public void say(){
        //调用宠物的方法
        pet.hello();
    }
}

@Qualifier

1.该注解不能单独使用,必须配合Autowired使用,根据key进行注入

eg:

package com.jt.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component  //将user交给Spring容器管理
public class User {

    //效果: 将当前接口的实现类自动注入
    @Autowired
    @Qualifier("cat") //该注解不能单独使用,必须配合Autowired使用,根据key进行注入
    private Pet pet;  //2选1

    public void say(){
        //调用宠物的方法
        pet.hello();
    }
}

@Resource

1.此注解为根据名称注入,功能类似于@Autowired+@Qualifier("Cat")

2.@Autowired为Spring提供的注解,而@Resource为java本身自带注解,使用时推荐配套使用,即@Autowired

eg:

package com.jt.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component  //将user交给Spring容器管理
public class User {

    //效果: 将当前接口的实现类自动注入
    //@Autowired
    //@Qualifier("cat") //该注解不能单独使用,必须配合Autowired使用,根据key进行注入
    @Resource(name="dog")  //功能上说,@Autowired+@Qu
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值