Here is my code:
public class Main { public static void main(String[] args) { Main p = new Main(); p.start(args); } @Autowired private MyBean myBean; private void start(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/config.xml"); System.out.println("my beans method: " + myBean.getStr()); } } @Service public class MyBean { public String getStr() { return "string"; } }
<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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="mypackage"/> </beans>
Why doesn't this work? I get NullPointerException
. Is it possible to use autowiring in a standalone application?
Sure you can use @Autowired but as your JobMain isn't a a Spring bean it will ofcourse not be autowired. Basically that is duplicate of this (more reasons here. So to fix make your JobMain an @Component and don't construct a new instance yourself.
The @Autowired annotation in spring automatically injects the dependent beans into the associated references of a POJO class. This annotation will inject the dependent beans by matching the data-type (i.e. Works internally as Autowiring byType).
So the answer is: No, @Autowired does not necessarily mean you must also use @Component . It may be registered with applicationContext. xml or @Configuration+@Bean .
Yes you can , autowiree just ensures that autowired component is loaded when that class is loaded , if autowired component cant be wired it raises compile time exception .
Spring works in standalone application. You are using the wrong way to create a spring bean. The correct way to do it like this:
@Component public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/config.xml"); Main p = context.getBean(Main.class); p.start(args); } @Autowired private MyBean myBean; private void start(String[] args) { System.out.println("my beans method: " + myBean.getStr()); } } @Service public class MyBean { public String getStr() { return "string"; } }
In the first case (the one in the question), you are creating the object by yourself, rather than getting it from the Spring context. So Spring does not get a chance to Autowire
the dependencies (which causes the NullPointerException
).
In the second case (the one in this answer), you get the bean from the Spring context and hence it is Spring managed and Spring takes care of autowiring
.
Spring is moving away from XML files and uses annotations heavily. The following example is a simple standalone Spring application which uses annotation instead of XML files.
package com.zetcode.bean; import org.springframework.stereotype.Component; @Component public class Message { private String message = "Hello there!"; public void setMessage(String message){ this.message = message; } public String getMessage(){ return message; } }
This is a simple bean. It is decorated with the @Component
annotation for auto-detection by Spring container.
package com.zetcode.main; import com.zetcode.bean.Message; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; @ComponentScan(basePackages = "com.zetcode") public class Application { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(Application.class); Application p = context.getBean(Application.class); p.start(); } @Autowired private Message message; private void start() { System.out.println("Message: " + message.getMessage()); } }
This is the main Application
class. The @ComponentScan
annotation searches for components. The @Autowired
annotation injects the bean into the message
variable. The AnnotationConfigApplicationContext
is used to create the Spring application context.
My Standalone Spring tutorial shows how to create a standalone Spring application with both XML and annotations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With