简体   繁体   English

Springboot自动接线的空值

[英]Springboot autowired null value

I'm fairly new to Spring & Spring boot, trying to wrap my head around with the concepts. 我是Spring&Spring引导的新手,试图用这些概念来包裹我的头。 I have this sample class, this is just a typed up code to show what i'm trying to do. 我有这个示例类,这只是一个键入的代码,以显示我要执行的操作。 There are no compilation errors. 没有编译错误。 When I start the server, the MQConnection class code gets executed, the mq properties from the appplication.properties are read and printing. 当我启动服务器时,将执行MQConnection类代码,并读取并打印来自appplication.properties的mq属性。 But when another class tries to call the send message to MQ, i'm seeing NUllPointerException 但是当另一个类尝试将发送消息调用到MQ时,我看到了NUllPointerException

@Component
public class MQConnection {

    private String username;
    private String password;
    private String host;

    private Connection connection;

    @Autowired
    public MQConnection(@value("${username}") String username, 
            @value("${password}") String password, @value("${host}") String host) {
        this.username = username;
        this.password = password;
        this.host = host;
        init();
    }

    public getConnection() {
        return connection 
    }

    private init() {
        connection = mqconnection;
        System.out.println(username, password, host); // I see the value when the server is started
        //code to connect to mq

    }

}

What am I missing, these autowired & beans is really confusing for me as i'm new to Spring world. 我缺少的是,这些自动接线的&bean对我来说真的很困惑,因为我是Spring World的新手。 Am I using right flow or completely absurd, I don't know 我是在使用正确的流程还是完全荒唐,我不知道

@Component
public class MQSendMessage {

    @Autowired
    MQConnection mqConnection;

    public void sendMessage(String message) {
        connection = mqConnection.getConnection(); //NULL value
        //send messageCode throws nullpointerexception
    }
}

public class AnotherClass {

    @Autowired
    MQSendMessage messageq;

    public doSomething() {

        messageq.sendMessage("hello world"); 
    }
}

Any help to fix the connection that throws nullpointer 修复抛出nullpointer的连接的任何帮助

It looks like AnotherClass is not instantiated by Spring container. 看起来AnotherClass没有被Spring容器实例化。 If you want to use Spring-annotation like convention then you have to annotate your class with eg @Component annotation. 如果您想使用Spring注释之类的约定,则必须使用@Component注释对类进行注释。 Otherwise Spring wont instantiate this object for you. 否则,Spring不会为您实例化此对象。

Useful tip 有用的提示

Try using constructor injection instead of a field injection. 尝试使用构造函数注入而不是字段注入。 Just like in your MQConnection class. 就像在您的MQConnection类中一样。 You can even mark all your class fields instantiated in the construct with final keyword so you will be sure that these values wont change (if they are immutable of course) during bean life cycle. 您甚至可以使用final关键字标记在构造中实例化的所有类字段,这样您可以确保在bean生命周期中这些值不会更改(如果它们当然是不变的)。 Then AnotherClass could look like this: 然后AnotherClass可能看起来像这样:

public class AnotherClass {

    private final MQSendMessage messageq;

    @Autowired
    public AnotherClass(MQSendMessage messageq) {
        this.messageq = messageq
    }

    public doSomething() {
        messageq.sendMessage("hello world");
    }
}

Spring Boot documentation Spring Boot文档

Also please read carefully Spring Boot documentation on Spring Beans and dependency injection . 另外,请仔细阅读有关Spring Beans和依赖项注入的 Spring Boot文档。 It is very well written and describes basic concepts in details. 它写得很好,并详细描述了基本概念。 It will make your learning much easier and faster. 这将使您的学习变得轻松快捷。

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM