当前位置: 首页 > 知识库问答 >
问题:

@自动连线依赖项具有空成员

怀经赋
2023-03-14

我的配置类:

@Configuration
public class TimeOutConfig {

    @Value("${value1}")
    private int value1;

    @Value("${value2}")
    private int value2;

    @Bean()
    User boTimeOut() {
        System.out.println("In BOTimeOutConfig");
        BeanTest b1 = new BeanTest(value1, value2);
        User u1 = new User(b1);
        return u1;
    }


    @Value("${value3}")
    private int value3;

    @Value("${value4}")
    private int value4;

    @Bean(name = "BSETimeout")
    User getBSEUser() {
        System.out.println("In BSETimeOutConfig");
        User u2 = new User( new BeanTest(value3, value4));
        return u2;
    }

}

BeanTest等级:

@Component
public class BeanTest {

    private int v1;
    private int v2;

   BeanWebClient b1;

    public BeanTest(){}

    public BeanTest(int v1, int v2){
        this.v1=v1;
        this.v2=v2;
        beanWebClient();
        
    }
    public BeanWebClient beanWebClient() {
        //System.out.println("In BeanTest class beanWebClient() method execution v1 and v2: "+v1+v2);
        b1 = new BeanWebClient(v1,v2);
        System.out.println("In BeanTest class beanWebClient() method execution v1 and v2: "+b1.getV1()+" "+b1.getV2());
        return b1;
    }

    public void message()
    {
        b1.webClientMessage();
        //System.out.println("V1= "+v1+" V2= "+v2);
    }
}

BeanWebClient类:

@Component
public class BeanWebClient {

    private int v1;
    private int v2;
    public BeanWebClient(int v1, int v2){

        this.v1=v1;
        this.v2=v2;
        System.out.println("Received from BeanTest "+v1+" "+v2);
    }

    public BeanWebClient(){

    }

    public int getV1() {
        return v1;
    }
    public int getV2() {
        return v2;
    }

    public void webClientMessage(){

        System.out.println("In BeanWebClient class, printing from autowired dependency "+v1+" "+v2);
    }

}

我的用户类别:

@Component
public class User {

    @Autowired
    private BeanTest beanTest;



    public User(BeanTest beanTest){

        this.beanTest=beanTest;
    }

    public void message()
    {

        beanTest.message();
    }


    public User() {

        //System.out.println("user create... hashCode :" + this.hashCode());
    }
}

最后,我的服务类:

@Service
public class BeanService {

    @Autowired
    @Qualifier("boTimeOut")
    private User boUser;

    @Autowired
    @Qualifier("BSETimeout")
    private User bseUser;

    public void printBO()
    {
        boUser.message();

    }
    public void printBSE()
    {
        bseUser.message();
    }

}

我试图了解组件和自动布线依赖项在spring boot中是如何工作的。在我的User类中,autowired BeanTest类在配置过程中被初始化-执行所有必要的打印语句。但是,当我调用服务类的函数时,自动连线用户依赖项对于beanTest及其成员为null。与此相反,当我在我的用户类中创建BeanTest对象时,一切都按预期工作。这就是我的意思:

@Component
public class User {

    BeanTest beanTest;



    public User(BeanTest beanTest){

        this.beanTest=beanTest;
    }

    public void message()
    {

        beanTest.message();
    }


    public User() {

        //System.out.println("user create... hashCode :" + this.hashCode());
    }
}

我有点理解User类中自动装配的bean是从容器中获取的是null。由于我在我的服务类中自动装配特定的User依赖项,我不明白为什么它不起作用。有人能解释一下如果和如何正确地将BeanTest类自动装配到User类中吗?抱歉有这么多代码文件。

输出这是我在没有@Autowire的情况下手动创建BeanTest对象时得到的输出

In BOTimeOutConfig
Received from BeanTest 10 20
In BeanTest class beanWebClient() method execution v1 and v2: 10 20
In BSETimeOutConfig
Received from BeanTest 30 40
In BeanTest class beanWebClient() method execution v1 and v2: 30 40
2021-11-13 14:37:58.161  WARN 404383 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-11-13 14:37:58.613  INFO 404383 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-13 14:37:58.622  INFO 404383 --- [           main] c.paytmmoney.errordb.ErrordbApplication  : Started ErrordbApplication in 4.04 seconds (JVM running for 4.379)
2021-11-13 14:38:07.768  INFO 404383 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-11-13 14:38:07.768  INFO 404383 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-11-13 14:38:07.769  INFO 404383 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
In /test method
In BeanWebClient class, printing from autowired dependency 10 20
In /test method
In BeanWebClient class, printing from autowired dependency 30 40

当我autowire BeanTest类时,这是输出:

In BOTimeOutConfig
Received from BeanTest 10 20
In BeanTest class beanWebClient() method execution v1 and v2: 10 20
In BSETimeOutConfig
Received from BeanTest 30 40
In BeanTest class beanWebClient() method execution v1 and v2: 30 40
2021-11-13 14:40:36.072  WARN 404724 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-11-13 14:40:36.585  INFO 404724 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-13 14:40:36.596  INFO 404724 --- [           main] c.paytmmoney.errordb.ErrordbApplication  : Started ErrordbApplication in 4.353 seconds (JVM running for 4.75)
2021-11-13 14:40:45.081  INFO 404724 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-11-13 14:40:45.081  INFO 404724 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-11-13 14:40:45.082  INFO 404724 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
In /test method
2021-11-13 14:40:45.204 ERROR 404724 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.paytmmoney.errordb.repository.BeanTest.message(BeanTest.java:42) ~[main/:na]
    at com.paytmmoney.errordb.repository.User.message(User.java:22) ~[main/:na]
    at com.paytmmoney.errordb.service.BeanService.printBO(BeanService.java:21) ~[main/:na]

我正在使用rest控制器调用Service类中的函数。
谢谢!!!

共有1个答案

有骏奇
2023-03-14

鉴于您正在TimeOutConfig中自己创建BeanTest和User实例,请按如下方式从BeanTest、BeanWebClient和User中删除所有组件和自动连线注释:

public class BeanTest {

    private int v1;
    private int v2;
    BeanWebClient b1;

    public BeanTest(){}

    public BeanTest(int v1, int v2){
        this.v1=v1;
        this.v2=v2;
        beanWebClient();
    }
    
    public BeanWebClient beanWebClient() {
        //System.out.println("In BeanTest class beanWebClient() method execution v1 and v2: "+v1+v2);
        b1 = new BeanWebClient(v1,v2);
        System.out.println("In BeanTest class beanWebClient() method execution v1 and v2: "+b1.getV1()+" "+b1.getV2());
        return b1;
    }

    public void message() {
        b1.webClientMessage();
        //System.out.println("V1= "+v1+" V2= "+v2);
    }
}
public class BeanWebClient {

    private int v1;
    private int v2;
    
    public BeanWebClient(int v1, int v2) {}
        this.v1=v1;
        this.v2=v2;
        System.out.println("Received from BeanTest "+v1+" "+v2);
    }

    public BeanWebClient(){ }

    public int getV1() {
        return v1;
    }
    
    public int getV2() {
        return v2;
    }

    public void webClientMessage(){
        System.out.println("In BeanWebClient class, printing from autowired dependency "+v1+" "+v2);
    }
}
public class User {

    private BeanTest beanTest;

    public User(BeanTest beanTest) {
        this.beanTest=beanTest;
    }
    
    public User() {
        //System.out.println("user create... hashCode :" + this.hashCode());
    }

    public void message() {
        beanTest.message();
    }
}
 类似资料:
  • 我想测试一些包含其他autowired服务的服务。但这些“外部”服务对于测试本身并不是必需的。 我如何创建一个测试设置,例如下面的例子? ApplicationContext.xml: 问题:所有包含来自未在测试中扫描的包(如)的自动连线依赖项的服务都将引发异常: 原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:

  • 我在Spring+SpringMVC+Hibernate+MySQL web应用程序中的Spring配置有一个问题。Spring无法创建我在Service类中宣布的bean。 下面是Controller类 应用程序-上下文 最后是我的StackTrace 原因:org.SpringFramework.Beans.Factory.BeanCreationException:无法自动连接字段:priv

  • 问题内容: 我正在开发一个小型Java EE Hibernate Spring应用程序,出现错误: 这是我的控制器ArticleControleur,其中包含用于恢复文章列表的功能: 这是我的articleService: 这是我的应用程序上下文: 问题答案: 该错误表明不是注册的Bean。添加其中包含将在你的应用程序上下文中自动装配的bean的软件包: 或者,如果你想将所有子包包括在com.bd

  • 被异常卡住,下面是日志: org.springframework.beans.factory.BeanCreation异常:创建名为扬声器的bean时出错:注入自动生成的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreation异常:无法自动连接方法:公共最终无效org.mybatis.spring.support.SqlSessionDao

  • 大家好,我收到下一个错误,我是使用Hibernate的新手

  • 我正在开发一个具有可重用逻辑的公共java库,以与一些AWS服务交互,这些服务将依次被多个消费者应用程序使用。出于这里概述的原因,以及Spring Boot似乎为SQS集成之类的东西提供了大量无模板代码的事实,我决定将此公共库实现为具有自动配置的定制Spring Boot启动器。 我也是Spring框架的新手,因此遇到了一个问题,即我的自动配置类的实例变量没有通过AutoWired注释进行初始化。