Why Is My Spring @Autowired Field Null

Java Spring Boot @Autowired values are null

They exist in different packages; With AppConfig living in a config
folder and DAOMethods in client->dynamodb->util folder. Config and
Client are folders under the main->java folder

The added @Configuration annotation scans for the beans in the current and its subpackages. You need to explicitly tell the application to scan the required packages. So you can do:

@SpringBootApplication (scanBasePackages = {"config", "client"})

OR,

You need to keep the config and other classes which uses that config in same root package. You can put the config folder and client folder under same package, say com.application and then add the following in your main class:

@SpringBootApplication(scanBasePackages = "com.application")

Now run the application.

Null Pointer Exception in @Autowired Annotation

You are getting Null Pointer because your field annotated with @Autowired is a static field. Spring does not support autowiring static fields.

Try adding @Autowired to your setter method instead to the field.

private static ProducerMain producerMain; 

public ProducerMain getProducerMain() {
return producerMain;
}
@Autowired
public void setProducerMain(ProducerMain producerMain) {
DemoApplication.producerMain = producerMain;
}

Why does my direct autowire property injection turns into null?

Are you really placing the @Autowired on the field of the main class or its just an illustration? If you do - it won't work because the class on which @Autowired can happen must be by itself managed by Spring. And in this case its obviously not, because its a special class - an entry point of the application...

I suggest to use something like this instead:

@Component 
public class EntryPoint {


@Autowired
Interfacex interfacex;

public void foo() {
// interfacex shouldn't be null
// because everything is managed by spring now
interfacex.doA();
}

}

public class Main {

public static void main(..) {
ApplicationContext ctx = ...
EntryPoint ep = ctx.getBean(EntryPoint.class);
ep.foo();
}
}

Why is my Spring @Autowired field returns null?

I've checked your code and i got answer for your problem.

There is nothing wrong with your code.

But when you want to use both spring mvc and jersey restfull webservices you have to do something more than that you are doing here. Bcoz jersey is not aware of spring container and it's implementation. So, whenever you are requesting for a webservice jersey fullfills your request processing instead of the spring framework.

So, here the playerDao is not injected into the LoginRestService.java and hence it (playerDao) resolve to null.

So do the following steps,

Step-1:-- Add the below maven dependency to your pom.xml

    <dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.19.1</version>
<dependency>

This dependency is used to integrate Spring and Jersey

Step-2:- Now create a WebApplicationContext to read all the configuration information from your mvc-dispatcher-servlet.xml. For this copy the following code to your web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Step-3:-- Now integrate spring and jersey by using jersey framework provided class com.sun.jersey.spi.spring.container.servlet.SpringServlet (This class is available in jersey-spring.jar ). For this copy the below code to your web.xml

<servlet>
<servlet-name>jersey-serlvet</servlet-name>

<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>

<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>ngdemo.rest</param-value>
</init-param>

<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Thats it. Everything is completed.

Just update your maven project and run again in the server.

Suggestion
One more thing when you are adding dependency ( jersey-spring-1.19.1.jar ) some spring related jar files (like spring-aop.3.0.0CR.jar, spring-bean.jar etc.. ) are included into your maven dependencies and the included jars are old version. So sometime you may get ClassNotFoundException.So you have to exclude those spring dependencies.
If you get such kind of Exception copy this below code to element of your jersey-spring.jar

<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.19.1</version>
<exclusions>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-web</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>

I hope this helps you. Let me know whether you got correct solution or not.

Spring Repository is null despite being autowired

The problem in your code is here:

private UserService userService = new UserService();

You expect that the object will be created by the container through dependency injection, but instead, you create it manually. It does not work like that.

Try something like this:

@Service
public class LoginInterface implements ActionListener {
//
@Autowired
private UserService userService
//
}


Related Topics



Leave a reply



Submit