@lazy注入

/*

  • 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肯定还是同一个
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@Lazy是一个Spring注解,用于控制Bean的初始化方式。当一个Bean被标记为@Lazy时,它的初始化将会被延迟到第一次使用时。这样可以提高系统的性能,因为只有在需要使用该Bean时才会进行初始化。 @PostConstruct是一个Java注解,用于标记一个方法在Bean初始化之后立即执行。当Bean被创建并完成属性注入后,@PostConstruct注解的方法会被自动调用。这个方法可以用来执行一些初始化操作,比如加载配置文件、连接数据库等。 总结起来,@Lazy注解用于控制Bean的延迟初始化,而@PostConstruct注解用于标记一个方法在Bean初始化之后执行。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Spring注解 - 52注解 - 原稿笔记](https://download.csdn.net/download/qq_46878411/20929624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [关于懒加载导致@PostConstruct失效](https://blog.csdn.net/Leo_Hth/article/details/121267670)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值