简体   繁体   中英

Springboot autowired null value

I'm fairly new to Spring & Spring boot, trying to wrap my head around with the concepts. 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. But when another class tries to call the send message to MQ, i'm seeing 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. 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

It looks like AnotherClass is not instantiated by Spring container. If you want to use Spring-annotation like convention then you have to annotate your class with eg @Component annotation. Otherwise Spring wont instantiate this object for you.

Useful tip

Try using constructor injection instead of a field injection. Just like in your MQConnection class. 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. Then AnotherClass could look like this:

public class AnotherClass {

    private final MQSendMessage messageq;

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

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

Spring Boot documentation

Also please read carefully Spring Boot documentation on Spring Beans and dependency injection . It is very well written and describes basic concepts in details. It will make your learning much easier and faster.

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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