博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring-framework @Autowired注入bean 为null之谜
阅读量:6888 次
发布时间:2019-06-27

本文共 4053 字,大约阅读时间需要 13 分钟。

hot3.png

环境:

JDK1.8

Spring-framework4.1.2.RELEASE

如下图所示的一个Spring javaSE工程

210724_DoIC_2338224.png

applicationContext.xml内容如下:

 

TestInterface2内容:

package com.chenjun.learnspring.annotation;public interface TestInterface2 {	public void print();}

TestInterfaceImpl2内容:

package com.chenjun.learnspring.annotation;import org.springframework.stereotype.Service;@Servicepublic class TestInterfaceImpl2 implements TestInterface2 {	public void print() {		System.out.println("TestInterfaceImpl2 is print");	}}

App.java内容: 

package com.chenjun.learnspring.annotation;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Component;@Componentpublic class App {		@Autowired	private TestInterface2 testInterface2;	public static void main(String[] args) {		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");		App app = new App();		app.proxyPrint();	}		public void proxyPrint() {		testInterface2.print();	}}

编译运行App.class主类

输出结果发现空指针异常,也就是说testInterface2的实现类并没有注入进来

Exception in thread "main" java.lang.NullPointerException

    at com.chenjun.learnspring.annotation.App.proxyPrint(App.java:24)

    at com.chenjun.learnspring.annotation.App.main(App.java:20)

 这是怎么回事呢,是因为testInterfaceImpl2这个类没有被Spring容器所管理吗? 

我打算输出一下Spring容器里面的bean一看究竟:

于是我编写如下代码:

略微修改之后的App.java

package com.chenjun.learnspring.annotation;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Component;@Componentpublic class App {		@Autowired	private TestInterface2 testInterface2;	public static void main(String[] args) {		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");				String[] beanArray = applicationContext.getBeanDefinitionNames();				for(String s : beanArray) {			System.out.println(s);		}		App app = new App();				app.proxyPrint();	}		public void proxyPrint() {		testInterface2.print();	}}

我把Spring容器里面现在的bean名称都打印一下,如下:

testInterfaceImpl2apporg.springframework.context.annotation.internalConfigurationAnnotationProcessororg.springframework.context.annotation.internalAutowiredAnnotationProcessororg.springframework.context.annotation.internalRequiredAnnotationProcessororg.springframework.context.annotation.internalCommonAnnotationProcessororg.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessororg.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessorException in thread "main" java.lang.NullPointerException	at com.chenjun.learnspring.annotation.App.proxyPrint(App.java:31)	at com.chenjun.learnspring.annotation.App.main(App.java:27)

从上面控制台输出信息可以看到,spring容器中确实已经有了testInterfaceImpl2,但是为什么上面声明这个bean的时候他没有注入呢?

原因就在于这行

App app = new App();

这行代码把App用new关键字进行创建对象,这就使得app依赖的其他bean已经脱离了spring的依赖注入管理

找到原因之后,最终我修改App.java的代码如下:

正确的注入方式

package com.chenjun.learnspring.annotation;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Component;@Componentpublic class App {		@Autowired	private TestInterface2 testInterface2;	public static void main(String[] args) {		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");		App app = applicationContext.getBean(com.chenjun.learnspring.annotation.App.class);		app.proxyPrint();	}		public void proxyPrint() {		testInterface2.print();	}}

 输出结果:

212025_XXk6_2338224.png

总结:

输出正常. 这也就说明了,spring在管理bean注入的策略是这样的:

当A组件依赖B, 要想使用spring依赖注入得到组件B的实例, 那么A本身也要是通过Spring的bean来创建的才行. 而不是直接new出A的实例;

 

转载于:https://my.oschina.net/u/2338224/blog/1822135

你可能感兴趣的文章
PHP中利用COOKIE与SESSION联合实现SESSION跨域
查看>>
error:Microsoft Visual C++ 9.0 is required. Get it
查看>>
Mininal Desktop安装CentOS 6.4后编译安装Mplayer
查看>>
linux记录 ---- 添加开机启动运行脚本
查看>>
VMware服务器虚拟化平台应急方案
查看>>
书单收藏
查看>>
好马不回头策略
查看>>
函数声明后面的const用法
查看>>
CUDA中自动初始化显卡设备宏
查看>>
application
查看>>
spring中加载xml配置文件的方式 .
查看>>
ruby参考
查看>>
斐波那契数列c语言实现
查看>>
emacs使修改的配置文件立即生效方法
查看>>
查询表中没有的字段信息
查看>>
stm32 使用 printf 串口输出 配置
查看>>
java 同步锁 synchronized 死锁 lock锁 jion 线程结束
查看>>
jsf开发心得(3)-jsf应用中css运用背景图片显示不了的问题
查看>>
IOS UIAlertController 弹出框中添加视图(例如日期选择器等等)
查看>>
ubuntu 12.04 开启root
查看>>