Spring---依赖注入问题
配置类中,手动new 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | @Configuration public class TestConfig { @Bean public TestInterface testInterface(){ System. out .println( "init testInterface" ); return buildTestInterfaceImpl(); } @Bean( "testInterfaceName" ) public TestInterfaceName testInterfaceName(){ System. out .println( "init testInterfaceName" ); return new TestInterfaceNameImpl(); } private TestInterface buildTestInterfaceImpl() { TestInterfaceImpl testInterface = new TestInterfaceImpl(); -- 手动创建Bean testInterface.init(); return testInterface; } } public interface TestInterface { void test(); } public class TestInterfaceImpl implements TestInterface{ @Autowired private TestInterfaceName testInterfaceName; -- @Autowired注入依赖TestInterfaceName @Override public void test() { } public void init(){ System. out .println( "init:" + testInterfaceName); } } public interface TestInterfaceName { void test(); } public class TestInterfaceNameImpl implements TestInterfaceName{ @Override public void test() { } } @Override public void test() { } } |
初始化结果:
init testInterfaceName
init testInterface
init:null
由于手动new对象,不在spring的Bean容器管理,可能会导致手动new 对象的依赖注入失效;
解决
将手动new 对象的依赖使用构造器注入方式,使得手动创建的对象是完整的对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | @Configuration public class TestConfig { @Bean public TestInterface testInterface(TestInterfaceName testInterfaceName){ System. out .println( "init testInterface" ); return buildTestInterfaceImpl(testInterfaceName); } @Bean( "testInterfaceName" ) public TestInterfaceName testInterfaceName(){ System. out .println( "init testInterfaceName" ); return new TestInterfaceNameImpl(); } private TestInterface buildTestInterfaceImpl(TestInterfaceName testInterfaceName) { TestInterfaceImpl testInterface = new TestInterfaceImpl(testInterfaceName); testInterface.init(); return testInterface; } } public interface TestInterface { void test(); } public class TestInterfaceImpl implements TestInterface{ private TestInterfaceName testInterfaceName; public TestInterfaceImpl(TestInterfaceName testInterfaceName){ -- 构造器注入依赖 this .testInterfaceName = testInterfaceName; } @Override public void test() { } public void init(){ System. out .println( "init:" + testInterfaceName); } } public interface TestInterfaceName { void test(); } public class TestInterfaceNameImpl implements TestInterfaceName{ @Override public void test() { } } @Override public void test() { } } |
循环依赖
由于循环依赖,会注入半成品对象,如果在启动时,有大量的初始化逻辑,可能会导致注入的依赖是未完成初始化的Bean;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2019-02-26 JavaSE---关键字---return,break,continue