Spring框架注解大全(@各种注解详细介绍)

博主也在一点一点学习,把目前掌握的注解一一给大家介绍,注解的使用场景和作用分析!

博主再次谢谢各位,本章会不断更新和补充,我有什么重要的注解遗漏的话,希望大家私信指导和补充!

下面进入正题---------↓

- 创建一个DefaultCache类然后交给spring 管理

package jwdlh;
@Component
@Scope("singleton")
@Lazy
public class DefaultCache {
	public Cache() {
	   System.out.println("cache()");
	}
	@PostConstruct
	public void init() {
		System.out.println("init()");
	}
	@PreDestroy
	public void destory() {
		System.out.println("destory");
	}
}
  1. @Component 是Spring中用于描述Bean类的一个注解。
  2. @Scope 是Spring中用于定义Bean对象作用域的一个注解,其常用的值有singleton(整个内存有一份Bean实例),prototype(每次获取都会创建新实例)等。
  3. @Lazy 注解用于描述类,其目的是告诉spring框架此类支持延迟加载。
  4. @PostConstruct 注解用于描述bean对象生命周期方法中的初始化方法,此方法会在对象的构造方法之后执行。
  5. @PreDestroy 注解用于描述Bean对象生命周期方法中的销毁方法,此方法会在对象销毁之前执行。

添加sringboot 测试类,进行bean的获取及测试

@SpringBootTest
public class DefaultCacheTests {
	@Autowired
	private DefaultCache defaultCache;
	@Test
	public void testCache() {
		System.out.println(defaultCache);
	}
}
  1. @SpringBootTest 注解用于告诉spring框架,此测试类交给spring管理。
  2. @Autowired 注解用于描述属性或相关方法(例如set方法,构造方法等),用于告诉spring容器为指定属性赋值。

定义商品业务数据层接口及业务方法

package com.cy.pj.goods.dao;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
@Mapper 
public interface GoodsDao {
	 @Delete("delete from goods where id=#{id}")
	 int deleteById(Integer id);
}
  1. @Mapper 是由MyBatis框架中定义的一个描述Mapper接口的的注解,用于告诉Spring框架此接口由底层(mybatis)创建其实现类对象,并存储到spring容器.

---------------------------以下是借鉴搜索引擎,部分我还未用到-------------------
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

发布了10 篇原创文章 · 获赞 0 · 访问量 345

猜你喜欢

转载自blog.csdn.net/weixin_45925109/article/details/105621033
今日推荐