ClientService 接口
public interface ClientService { String client(); }
这里同时有2个类实现了该接口
ClientServiceImpl
@Service("ClientServiceImpl") public class ClientServiceImpl implements ClientService { @Autowired private LoadBalancerClient loadBalancerClient; @Override public String client() { ServiceInstance si = loadBalancerClient.choose("eureka-client"); // 获取Application Service IP。192.168.1.1 System.out.println(si.getHost()); // 获取Ip及端口。192.168.1.1:8081 System.out.println(si.getInstanceId()); // 获取额外声明信息.{management.port=8081} System.out.println(si.getmetadata()); // 端口 8081 System.out.println(si.getPort()); // 模式 null System.out.println(si.getScheme()); // 应用程序名 eureka-client System.out.println(si.getServiceId()); // URI http://192.168.1.1:8081 System.out.println(si.getUri().toString()); return null; } }
ClientServiceImpl2
@Service("ClientServiceImpl2") public class ClientServiceImpl2 implements ClientService { @Autowired private RestTemplate restTemplate; @Override public String client() { return restTemplate.getForObject("http://eureka-client/demo", String.class); } }
Controller类
@RestController public class ClientController { @Resource(name="ClientServiceImpl2") private ClientService clientService; @RequestMapping("/client") public String client(){ return clientService.client(); } }
这里先解释一下这些注解(这些注解可以相互替换)
@Controller 定义控制层Bean
@Service 定义业务层Bean
@Repository 定义DAO层Bean
@Component 定义Bean,不确定类型时
这里看一下这些注解的源码
这里解释一下什么时类型什么时名字
一般@Autowired和@Qualifier一起使用,@Resource单独用。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { @AliasFor(annotation = EnableAutoConfiguration.class) Class>[] exclude() default {}; @AliasFor(annotation = EnableAutoConfiguration.class) String[] excludeName() default {}; @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") String[] scanbasePackages() default {}; @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses") Class>[] scanbasePackageClasses() default {}; @AliasFor(annotation = ComponentScan.class, attribute = "nameGenerator") Class extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; @AliasFor(annotation = Configuration.class) boolean proxyBeanMethods() default true; }
@SpringBootApplication并没有定义新的属性而是复用其他注解已有的注解属性并对其进行组合
形成新的注解从而到达到便捷的目的。这样的注解我们可以称之为复合注解。
所以在使用SpringBoot 时我们只需要@SpringBootApplication一个注解就能开启
自动配置,自动扫描的功能。
而不再需要使下面三个注解来达到同样的目的。
@Configuration
@ComponentSan
@EnnableAutoConfiguration
@Scope(“prototype”)
public class People { private Integer id; private String name; public Integer getId() { return id; } public String getName() { return name; } public void setid(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public People() { } public People(Integer id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "People{" + "id=" + id + ", name='" + name + ''' + '}'; } public void initPeople(){ System.out.println("PeopleBean init"); } public void destroyPeople(){ System.out.println("PeopleBean destroy"); } }
@Configuration public class RibbonConfig { @Scope @Bean(value="People1",name="People1",initMethod="initPeople",destroyMethod="destroyPeople") public People getUser(){ System.out.println("创建People实例"); return new People(26,"lisi"); } public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RibbonConfig.class); People people1 = (People)context.getBean("People1"); System.out.println(people1); People people2 = (People)context.getBean("People1"); System.out.println(people2); context.close(); } }
结果
@PostConstruct
相当于init-method,使用在方法上,当Bean初始化时执行。
@PreDestroy
相当于destory-method,使用在方法上,当Bean销毁时执行。
修改
@PostConstruct public void initPeople(){ System.out.println("PeopleBean init"); } @PreDestroy public void destroyPeople(){ System.out.println("PeopleBean destroy"); }
修改Bean
@Scope @Bean(value="People1",name="People1") public People getUser(){ System.out.println("创建People实例"); return new People(26,"lisi"); }
结果使一样的
@Controller 定义控制层Bean
@Service 定义业务层Bean
@Repository 定义DAO层Bean
@Component 定义Bean,不确定类型时
其他三个注解继承Component 注解,其功能一样
一般@Autowired和@Qualifier一起使用,@Resource单独用。
@Autowired//默认按type注入
@Qualifier(“ClientServiceImpl2”)//一般作为@Autowired()的修饰用
@Resource(name=“ClientServiceImpl2”)//默认按name注入,可以通过name和type属性进行选择性注入
补充一个
@Inject 是JSR-330提供的 按类型装配,功能比@Autowired少,没有使用的必要。
@Scope(“prototype”)
值有:singleton,prototype,session,request,session,globalSession
@PostConstruct
相当于init-method,使用在方法上,当Bean初始化时执行。
@PreDestroy
相当于destory-method,使用在方法上,当Bean销毁时执行。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)