美文网首页
spring-注解

spring-注解

作者: 逢栲怭過 | 来源:发表于2019-12-15 10:48 被阅读0次

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

  1. @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

相关文章

  • spring-注解

    1.在web.xml配置启动的listener 启动容器的代码

  • spring-注解

    1.@RestControllerIn Spring’s approach to building RESTful...

  • 【Java】Spring-注解

    @RequestMapping @RequestMapping修饰类 用来修饰:方法、类当注解属性只有一个valu...

  • Spring-注解声明

    一、概要 前期说明 注解本身没有功能的,就和xml一样。注解和xml都是一种元数据,元数据即解释数据的数据,这就是...

  • Spring-基于注解配置

    今天使用一个二方库,发现对方给出的是基于xml配置的方式,因为我们团队都是新项目,大家都是使用基于注解配置的方式进...

  • Spring-注解依赖注入

    一、概要 自动装配是指Spring 在装配 Bean 的时候,根据指定的自动装配规则,将某个 Bean 所需要引用...

  • Spring-注解开发

    零、本文纲要 一、入门案例二、用于注解驱动的注解三、注入时机和设定注入条件的注解四、用于创建对象的注解五、和生命周...

  • Java Spring-声明式事务

    Spring-声明式事务

  • 12、Spring-注解注入声明

    一、概要 前面介绍了Bean的XML配置方法,从Spring 3.0开始,可以使用java代码配置Bean,替代X...

  • Java Spring-通过注解配置Bean

    组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有...

网友评论

      本文标题:spring-注解

      本文链接:https://www.haomeiwen.com/subject/qghzxctx.html