首页  

spring常用注解     所属分类 spring 浏览量 1216
组件声明
@Component 
@Service 
@Repository  dao层
@Controller  web层


bean注入

@Autowired  Spring提供
@Inject JSR-330提供
@Resource JSR-250提供

注入注解可以用在方法和属性上,建议直接用在属性上

配置类

@Configuration 声明类为配置类 
@Bean   声明当前方法的返回值为一个bean 
@ComponentScan  组件扫描
@WishlyConfiguration    @Configuration 与 @ComponentScan 的组合


AOP注解

AspectJ注解式切面编程

@Aspect 声明一个切面 (类上)
使用@After、@Before、@Around  定义advice,可直接将拦截规则(切点)作为参数。

@After 在方法执行之后执行( 
@Before 在方法执行之前执行( 
@Around 在方法执行之前与之后执行 

@PointCut 声明切点

使用@EnableAspectJAutoProxy注解开启Spring对AspectJ代理的支持


Bean属性相关注解


@Scope
Singleton  单例 
Protetype  每次都新建一个bean 
Request  web request
Session  web session
GlobalSession    global http session 

@PostConstruct 由JSR-250提供,在构造函数执行完之后执行,类似 xml配置 bean的initMethod

@PreDestory 由JSR-250提供,在Bean销毁之前执行,类似 xml配置 bean的destroyMethod




@Value注解

注入普通字符
@Value("mini tiger")
String name;

注入系统属性

@Value("#{systemProperties['os.name']}")
String osName;

注入表达式结果
@Value("#{ T(java.lang.Math).random() * 100 }")
String randomNumber;

注入其它bean属性
@Value("#{domeClass.name}")
String name;

注入文件资源

@Value("classpath:dyyx/hello.txt")
String Resource file;


注入配置文件
@Value("${book.name}")
String bookName;


加载配置文件
@PropertySource("classpath:dyyx/hello.propertie")

PropertySourcesPlaceholderConfigurer


环境切换

@Profile  设定Environment的ActiveProfiles 

@Conditional 
Spring4  条件化配置
实现Condition接口,并重写matches方法,从而决定bean是否被实例化。

异步相关
@EnableAsync

@Async 

定时任务相关

@EnableScheduling 
@Scheduled   定时任务 cron,fixDelay,fixRate等类型 

 SpringBoot定时任务 schedule  

Enable系列注解


@EnableAspectJAutoProxy 开启AspectJ自动代理
@EnableAsync 开启异步方法
@EnableScheduling 开启定时任务
@EnableWebMvc 开启Web MVC
@EnableConfigurationProperties  
@EnableJpaRepositories  
@EnableTransactionManagement 开启注解式事务
@EnableCaching 开启注解式缓存


测试相关注解

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes={TestConfig.class})


SpringMVC相关注解

@EnableWebMvc

@Controller 

@RequestMapping

@ResponseBody

@RequestBody

@PathVariable    

@RestController  相当于@Controller和@ResponseBody的组合

@ControllerAdvice  

@ExceptionHandler    全局处理控制器异常
@InitBinder   设置WebDataBinder  自动绑定请求参数到Model中
@ModelAttribute  绑定键值对到Model里


springboot常用注解和配置整理 spring web 注解

上一篇     下一篇
log4j日志格式配置

Class.forName()和ClassLoader.loadClass()的区别

getResource和getSystemResource的区别

proc diskstats 字段说明

FileInputStream无法读取完整数据问题解决

Java GC种类及配置说明