Spring Annotations

一.@SpringBootApplication

之前用户使用的是3个注解注解他们的main类。分别是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@SpringBootApplication。

@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。


分开解释@Configuration,@EnableAutoConfiguration,@ComponentScan。

1、@Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件。

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <beans>  
  2.     <bean id = "car" class="com.test.Car">  
  3.         <property name="wheel" ref = "wheel"></property>  
  4.     </bean>  
  5.     <bean id = "wheel" class="com.test.Wheel"></bean>  
  6. </beans>  

相当于:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. @Configuration  
  2. public class Conf {  
  3.     @Bean  
  4.     public Car car() {  
  5.         Car car = new Car();  
  6.         car.setWheel(wheel());  
  7.         return car;  
  8.     }  
  9.     @Bean   
  10.     public Wheel wheel() {  
  11.         return new Wheel();  
  12.     }  
  13. }  
@Configuration的注解类标识这个类可以使用Spring IoC容器作为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。

2、@EnableAutoConfiguration:能够自动配置spring的上下文. 根据你加入的依赖关系dependencies, Spring试图猜测你想要的配置和你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。例如你加入了spring-boot-starter-web(内置有Tomcat与SpringMVC),自动装配会猜测你想建立一个web 项目,然后给你相应的配置。

3、@ComponentScan:会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service,@Repository,@Controller。


The @RestController and @RequestMapping annotations

@RestController  注解该类是controller,
@RequestMapping 注解 提供了路由信息(routing), 路由匹配后将执行下面的类代码。

Spring 文档:http://docs.spring.io/spring-boot/docs/current/reference/html/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值