1.@RestController
In Spring’s approach to building RESTful web services, HTTP requests are handled by a controller. These components are easily identified by the [@RestController
] annotation, and the GreetingController
below handles GET
requests for /greeting
by returning a new instance of the Greeting
class:
this code uses Spring 4’s new [@RestController
]annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller
and @ResponseBody
rolled together.
2.@RequestMapping
The above example does not specify GET vs. PUT, POST, and so forth, because @RequestMapping maps all HTTP operations by default. Use @RequestMapping(method=GET) to narrow this mapping.
3.@RequestParam
@RequestParam binds the value of the query string parameter name into the name parameter of the greeting() method. If the name parameter is absent in the request, the defaultValue of "World" is used.
4.@Scheduled(fixedRate = 5000)
The Scheduled
annotation defines when a particular method runs. NOTE: This example uses fixedRate
, which specifies the interval between method invocations measured from the start time of each invocation. There are [other options], like fixedDelay
, which specifies the interval between invocations measured from the completion of the task. You can also [use @Scheduled(cron=". . .")
expressions for more sophisticated task scheduling].
5.@SpringBootApplication
@SpringBootApplication is a convenience annotation that adds all of the following:
@Configuration tags the class as a source of bean definitions for the application context.
@EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
@ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.
6.@EnableScheduling
[@EnableScheduling
]ensures that a background task executor is created. Without it, nothing gets scheduled.
7@Bean
@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
相当于
<beans>
<bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>
7 @ConfigurationProperties
相当于
@Configuration
@PropertySource("classpath:config/redis.properties")
和
@Value("${redis.maxIdle}")的组合使用,他会自动找到值去一一映射
8,Spring还提供了3个功能基本和@Component等效的注解:
@Repository 用于对DAO实现类进行注解
@Service 用于对Service实现类进行注解
@Controller 用于对Controller实现类进行注解
9.Bean的作用域@Scope
10.基本类型属性注入@Value
- @Autowired,该注解默认使用按类型自动装配Bean的方式
需要在域属性上联合使用注解@Autowired与@Qualifier。@Qualifier的value属性用于指定要匹配的Bean的id值。同样类中无需setter,也可加到setter上
@Autowired还有一个属性required,默认值为true,表示当匹配失败后,会终止程序运行。若将其值设置为false,则匹配失败,将被忽略,未匹配的属性值为null。
12.@Resource注解既可以按名称匹配Bean,也可以按类型匹配Bean。使用该注解,要求JDK必须是6及以上版本。
@Resource注解若不带任何参数,则会按照类型进行Bean的匹配注入。
13.Bean的生命始末@PostConstruct与@PreDestroy
网友评论