Spring section:
1, the statement bean notes:
@Component: component, there is no clear role
@Service: Use (service layer) Yewuluojiceng
@Repository: Use (DAO layer) in the data access layer
@Controller: the presentation layer using statement Controller (Controller)
2, injected bean notes:
@Autowired: provided by Spring, according to the type of injection, if a plurality of interfaces implemented with the use needs and @Qualifier
@Inject: JSR-330 by
@Resource: by JSR-250, injected by name
Can comment on a set of methods and properties, it recommended annotation on the property.
3, java configuration or related notes:
@Bean comment on the method, declare the current method returns a value of a bean, if you use third-party tools, you need to use @Bean assembled, otherwise use xml form
@Configuration declare the current class to class configuration, which combines internal @Component comments, indicating that this class is a bean
Component for scanning @ComponentScan
@WishlyConfiguration combination with @ComponentScan @Configuration annotations can replace the two annotations
4, section (AOP) related notes:
Spring supports AspectJ annotation style section of programming.
@Aspect Declaring an aspect
Use @ After, @ Before, @ Around defined suggestions (advice), may be blocking rules (cutting point) as parameters directly.
@After performed after performing the method (the method)
@Before performed prior to performing the method (the method)
@Around performed before the method is performed after (the method)
@PointCut sound Mingqie point
Use @EnableAspectJAutoProxy in java class configuration support for AspectJ annotation open Spring agents (the class)
5, @ Bean's property support:
How to create a new container is provided @Scope Spring Bean instance (the method, must have @Bean)
Provided types include:
Singleton (single embodiment, a container only a bean instance, the Spring default mode),
Protetype (each call to create a new bean),
Request (web project, every http request to create a new bean),
Session (web projects, each http session to create a new bean),
GlobalSession (to each create a new global http session Bean instance)
@StepScope also involved in the Spring Batch
@PostConstruct by JSR-250, executed after executing the constructor, equivalent to the xml configuration file in the bean initMethod
@PreDestory by JSR-250, before performing the destruction Bean, equivalent to the bean xml configuration file destroyMethod
6, @ Value Note: to inject property value
Support of the injection follows:
1) injected ordinary characters
@Value("Michael")
String name;
2) operating system properties injection
@Value("#{systemProperties['os.name']}")
String osName;
3) injected expression results
@Value("#{ T(java.lang.Math).random() * 100}")
String randomNumber;
4). Other injection bean properties
@Value("#{ domeClass.name}")
String name;
5).注入文件资源
@Value("classpath:com/it/test.txt")
String Resource file;
6).注入网站资源
@Value("http://www.baidu.com")
Resource url;
7).注入配置文件
@Value("${book.name} ")
String bookName;
注入配置使用方法:
① 编写配置文件(test.properties)
book.name=《三体》
② @PropertySource 加载配置文件(类上)
@PropertySource("classpath:com/it/test.properties")
③ 还需配置一个PropertySourcesPlaceholderConfigurer的bean。
7、环境切换
@Profile 通过设定Environment的ActiveProfiles来设定当前context需要使用的配置环境。(类或方法上)
@Conditional Spring4中可以使用此注解定义条件话的bean,通过实现Condition接口,并重写matches方法,从而决定该bean是否被实例化。
8、异步相关
@EnableAsync 配置类中,通过此注解开启对异步任务的支持,叙事性AsyncConfigurer接口(类上)
@Async 在实际执行的bean方法使用该注解来申明其是一个异步任务(方法上或类上所有的方法都将异步,需要@EnableAsync开启异步任务)
9、定时任务相关
@EnableScheduling 在配置类上使用,开启计划任务的支持(类上)
@Scheduled 来申明这是一个任务,包括cron,fixDelay,fixRate等类型(方法上,需先开启计划任务的支持)
10、@Enable*注解说明
这些注解主要用来开启对xxx的支持。
@EnableAspectJAutoProxy 开启对AspectJ自动代理的支持
@EnableAsync 开启异步方法的支持
@EnableScheduling 开启计划任务的支持
@EnableWebMvc 开启Web MVC的配置支持
@EnableConfigurationProperties 开启对@ConfigurationProperties注解配置Bean的支持
@EnableJpaRepositories 开启对SpringData JPA Repository的支持
@EnableTransactionManagement 开启注解式事务的支持
@EnableTransactionManagement 开启注解式事务的支持
@EnableCaching 开启注解式的缓存支持
11、测试相关注解
@RunWith 运行器,Spring中通常用于对JUnit的支持
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration 用来加载配置ApplicationContext,其中classes属性用来加载配置类
@ContextConfiguration(classes={TestConfig.class})
Spring部分:
@EnableWebMvc 在配置类中开启Web MVC的配置支持,如一些ViewResolver或者MessageConverter等,若无此句,重写WebMvcConfigurerAdapter
方法(用于对SpringMVC的配置)。
@Controller 声明该类为SpringMVC中的Controller
@RequestMapping 用于映射Web请求,包括访问路径和参数(类或方法上)
@ResponseBody 支持将返回值放在response内,而不是一个页面,通常用户返回json数据(返回值旁或方法上)
@RequestBody 允许request的参数在request体中,而不是在直接连接在地址后面。(放在参数前)
@PathVariable 用于接收路径参数,比如@RequestMapping(“/hello/{name}”)申明的路径,将注解放在参数中前,即可获取该值,通常作为
Restful的接口实现方法。
@RestController 该注解为一个组合注解,相当于@Controller和@ResponseBody的组合,注解在类上,意味着,该Controller的所有方法都默认
加上了@ResponseBody。
@ControllerAdvice 通过该注解,我们可以将对于控制器的全局配置放置在同一个位置,注解了@Controller的类的方法可使用@ExceptionHandler
、@InitBinder、@ModelAttribute注解到方法上,
这对所有注解了 @RequestMapping的控制器内的方法有效。
@ExceptionHandler 用于全局处理控制器里的异常
@InitBinder 用来设置WebDataBinder,WebDataBinder用来自动绑定前台请求参数到Model中。
@ModelAttribute 本来的作用是绑定键值对到Model里,在@ControllerAdvice中是让全局的@RequestMapping都能获得在此处设置的键值对。
总结:上面几乎都是常用的Spring相关注解,而且大部分内容,在前面Spring相关博客也都有讲过,这里转载别人的总结,方便记忆,只不过具体的
解释不够详细,可以查看我之前的内容,或者自行百度
原文链接:https://blog.csdn.net/yelvgou9995/article/details/83345267