spring boot 启动类 添加组件

Posted zfzf1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 启动类 添加组件相关的知识,希望对你有一定的参考价值。

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@ServletComponentScan
public class SaleApp {
	
	@Bean
	@LoadBalanced
	public RestTemplate getRestTemplate() {
		return new RestTemplate();
	}
	
	public static void main(String[] args) {
		new SpringApplicationBuilder(SaleApp.class).web(true).run(args);
	}

}

  

@Service
//@DefaultProperties(defaultFallback = "getMemberFallback")
public class MemberService {

	@Autowired
	private RestTemplate restTpl;

	@HystrixCommand(fallbackMethod = "getMemberFallback", groupKey = "MemberGroup", commandKey = "MemberCommandKey", 
			commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
	}, threadPoolProperties = {
			@HystrixProperty(name = "coreSize", value = "2")
	})
	public Member getMember(Integer id) {
		try {
			Thread.sleep(2000);
		} catch (Exception e) {
			
		}
		
		Member member = restTpl.getForObject(
				"http://spring-hy-member/member/{id}", Member.class, id);
		return member;
	}

	public Member getMemberFallback(Integer id) {
		Member m = new Member();
		m.setId(1);
		m.setName("error member");
		return m;
	}
	

}

  

以上是关于spring boot 启动类 添加组件的主要内容,如果未能解决你的问题,请参考以下文章

解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)(代码片段

一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式

一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式

一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式

spring-boot整合SSM框架

自定义Spring Boot的启动器