Spring @Autowired comes as null

Wild Goat

I have @Component, A, which AutoWires a class which contains simple configurations for class A. I've created a bin for configuration file. But for some reason it comes as null. Could you please help me to find out the problem?

@Component
public class SearchEngineDriver {

    @Autowired(required = true)
    private EngineContext context;


    public SearchEngineDriver(){
         String clusterName = context.getClusterName();
    }
}


public class EngineContext {

    private String clusterName;

    public EngineContext(String clusterName){
        this.clusterName = clusterName;
    }

    public String getClusterName(){
        return this.clusterName;
    }
}

3rd class.

    @Autowired
    private SearchEngineDriver searchEngineDriver;

mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<context:component-scan base-package="org.electronsoftware" />
    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <import resource="classpath*:/application-context.xml"/>

    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">


    <context:property-placeholder location="classpath:application.properties"/>


    <bean id="searchEngineContext" class="org.electronsoftware.KGB.search.context.EngineContext" >
        <constructor-arg value="${kgb.search.engine.clustername}"/>
    </bean>

</beans>
Marko Topolnik

You are accessing the autowired field from the constructor. At the time the constructor runs, Spring has not yet got the chance to initialize the field. Instead use a @PostConstruct method to perform logic which depends on the autowired value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring Autowired null if not found

From Dev

Spring Boot Autowired null

From Dev

Spring - autowired fields are null

From Dev

Spring @Autowired field is null?

From Dev

Spring - autowired fields are null

From Dev

Spring autowired is null

From Dev

Autowired is null and not working with Jersey + Spring

From Dev

Spring MVC Autowired null in Component

From Dev

Spring @Autowired(required = true) is null

From Dev

Jersey 2 + Spring: @Autowired is null

From Dev

Spring Boot Autowired Repository null

From Dev

Spring @Autowired not working and returning null

From Dev

Spring @Autowired(required = true) is null

From Dev

spring @Autowired a repository returns null

From Dev

Spring @Autowired bean giving null

From Dev

FindBugs null issue with Spring @Autowired in Eclipse

From Dev

Spring @Autowired variable is null inside of @Component

From Java

Why is my Spring @Autowired field null?

From Dev

Spring DI - Autowired property is null in a REST service

From Dev

Spring autowired bean causes null pointer

From Dev

Spring @Autowired bean not initialising; Null Pointer Exception

From Dev

Autowired property is null - Spring Boot Configuration

From Dev

Spring Boot JavaConfig with Autowired dependencies that are null

From Dev

Autowired in CustomInterceptor getting null(Spring Boot)

From Dev

Spring bean is created, but is null when Autowired

From Dev

Why is my Spring @Autowired field returns null?

From Dev

Autowired property is null - Spring Boot Configuration

From Dev

Spring @Autowired gives null in a @Repository bean

From Dev

Spring autowired object is null in postcontruct method