编程语言
首页 > 编程语言> > java – SprintBoot应用程序抛出NullPointer异常

java – SprintBoot应用程序抛出NullPointer异常

作者:互联网

我遇到了SpringBoot可能存在的基本问题,但我不知道如何管理它:每次我想访问一个注入的服务时,这个都是Null.

2019-03-03 18:01:34.766  INFO 6428 --- [  restartedMain] c.c.s.SpringAbstractApplication          : Starting SpringAbstractApplication on BORN2CODE with PID 6428 (D:\Datas\Workspaces\Tests\SpringAbstract\target\classes started by Loveg in D:\Datas\Workspaces\Tests\SpringAbstract)
2019-03-03 18:01:34.769  INFO 6428 --- [  restartedMain] c.c.s.SpringAbstractApplication          : No active profile set, falling back to default profiles: default
2019-03-03 18:01:34.839  INFO 6428 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-03-03 18:01:35.742  INFO 6428 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-03-03 18:01:35.782  INFO 6428 --- [  restartedMain] c.c.s.SpringAbstractApplication          : Started SpringAbstractApplication in 1.547 seconds (JVM running for 2.233)
In the Launcher Go
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
    at com.citizenweb.springabstract.classes.Launcher.go(Launcher.java:23)
    at com.citizenweb.springabstract.SpringAbstractApplication.main(SpringAbstractApplication.java:15)
    ... 5 more

这是一个有问题的基本应用程序:

由SpringBoot构建的主应用程序,我在其中添加了一个Launcher对象:

package com.citizenweb.springabstract;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.citizenweb.springabstract.classes.Launcher;

@SpringBootApplication
public class SpringAbstractApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringAbstractApplication.class, args);
        Launcher app = new Launcher();
        app.go();
    }
}

我建造的启动器(由Spring主编):

package com.citizenweb.springabstract.classes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.citizenweb.springabstract.interfaces.IFactoryService;

@Component
public class Launcher {

    @Autowired
    IFactoryService factory;

    public void go() {
        System.out.println("In the Launcher Go");
        factory.testFactory();
    }
}

我想要使​​用的服务(界面):

package com.citizenweb.springabstract.interfaces;

public interface IFactoryService {

    void testFactory();

}

服务类:

package com.citizenweb.springabstract.classes;

import org.springframework.stereotype.Service;

import com.citizenweb.springabstract.interfaces.IFactoryService;

@Service
public class FactoryService implements IFactoryService {

    @Override
    public void testFactory() {
        System.out.println("In the Factory service");
    }
}

每次我启动应用程序时,我都有一个空指针异常,因为@Autowired IFactoryService工厂变量(在Launc类中)为null.

就像Spring无法扫描接口和类所在的包那样.正如您所看到的,我尊重Spring关于包结构的规则.

有什么办法让Spring扫描我的包裹吗?通常,@ SpringBootApplication注释本身应该这样做吗?

enter image description here

解决方法:

这里的问题是你手动实例化Launcher类.

 Launcher app = new Launcher();
 app.go();

这样做意味着在Spring控制之外,在其操作环境之外.

Spring能够自动连接您的依赖项,因为它实例化了您的Bean类.上下文中的每个Bean都有一个BeanDefinition关联,它描述了它.但是,如果您手动创建一个类,则不会发生这种情况.

无法以这种方式注入IFactoryService.

你可以做的是通过#run静态方法调用获取BeanFactory

final BeanFactory factory = SpringApplication.run(SpringAbstractApplication.class, args);
final Launcher launcher = factory.getBean(Launcher.class);
launcher.go();

标签:java,nullpointerexception,spring-boot-2
来源: https://codeday.me/bug/20190722/1499823.html