Spring 注解以及用途
最近在Spring-MVC的项目,把一些自己在项目中使用到的注解整理一下。
1、@Controller
2、@Component
把普通 POJO 实例化到 Spring 容器中;
3、@Scope
声明范围类型,值有:singleton、prototype、request、session、global session;
4、@Service
对应业务层的Bean;
5、@Repository
对应数据访问层Bean;
6、@Recource
JDK注解;按照name或者type方式注入对象,默认按照name方式;
7、@Autowired
Spring注解;按照type方式注入对象;
8、@Qualifier
9、@Configuration
<beans>
,作用为:配置spring容器(应用上下文)10、@Configuration + @Bean
<bean>
,作用为:注册bean对象11、@Configuration + @Component
注册Bean对象
12、@PropertySource
@PropertySource注解将properties配置文件中的值存储到Spring的 Environment中,例如代码:
@PropertySource(value = "classpath:storage.properties")
13、@RequestMapping
控制器指定可以处理哪些 URL 请求;
14、@PathVariable、
@RequestHeader、@CookieValue、
@RequestParam、@RequestBody、
@SessionAttributes, @ModelAttribute
15、@PostConstruct、@PreDestroy
分别为:初始化注解(用在方法上)、销毁注解(默认单例,启动就加载,用在方法上)