/*
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the “License”); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an “AS IS” BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
*/
package org.apache.dubbo.springboot.demo.provider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.springboot.demo.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Lazy;
@SpringBootApplication
@EnableDubbo
public class ProviderApplication implements ApplicationRunner {
@Autowired
private DemoService demoService1;
@Lazy
@Autowired
private DemoService demoService2;
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
@Override
public void run(final ApplicationArguments args) throws Exception {
System.out.println("(demoService1 == demoService2) = " + (demoService1 == demoService2));
//false
//原因: lazy注入是注入的一个lazy代理, 代理中的target指向spring getBean, 从而实现了在调用属性的方法时才获取bean
// 源码: lazyLookup ? buildLazyResourceProxy ()
// ps0: lazy注入不是不注入, 是不注入不就空指针了吗
// ps1: 是加了一个代理, 没有什么是加一层代理解决不了的, 比如 @RefreshScope 热更新, @lazy注入依赖
// ps2: 这里的lazy是用在 autowired依赖注入, 当用在@compont上时,bean肯定是同一个, 不用加一层, 看上去也会懒生成(最终要看源码的意思)
// ps3: api getbean肯定还是同一个
}
}