Spingboot的部分注解以及作用

1,@Scope 注解

  @Scope默认是单例模式,即scope="singleton"。(全局有且仅有一个实例)

       @Scope("prototype")多例  (每次获取Bean的时候会有一个新的实例)

import com.ccservice.flight.international.crawler.entity.CrawlerTask;
import com.ccservice.flight.international.crawler.entity.HttpRequestInfo;
import com.google.gson.JsonObject;

@Component("AkWxpFetcher")  
@Scope("prototype")  //多例
public class AkWxpFetcher extends BaseFetcher {
    
}

2,@ActiveProfiles("test")

  @ActiveProfiles是Spring Boot的Test starter提供的注解,如果项目是像下面的方式依赖Test starter的话——
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

在测试类中引用

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import com.ccservice.flight.international.crawler.entity.CrawlerTask;
import com.google.gson.Gson;

import lombok.extern.slf4j.Slf4j;

/**
 * @author WanFeng
 * @version 2019年7月23日下午3:16:31
 */
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { AkWxpParser.class, Gson.class })
@Slf4j
public class AkWxpParserTest {
    @Autowired
    @Qualifier("AkWxpParser")
    private IParser parser;//是接口 
}

 

 
posted @ 2019-07-23 18:28  挽风&画笔  阅读(186)  评论(0)    收藏  举报