Using Spring 3 Autowire in a Standalone Java Application

Using Spring 3 autowire in a standalone Java application

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.

Building Spring standalone application using Autowired annotation

SpringTest tester = new SpringTest();

This cannot work because by calling the constructor yourself, it is no longer a Spring managed bean and thus not eligible for dependency injection. On top of that, in no way have you marked SpringTest as being a Spring managed bean. Any class that makes use of @Autowired must live inside the Spring container.

To make this work you would need to add the @Component annotation to SpringTest, and then do something like ApplicationContext.getBean(SpringTest.class) in your main method.

How to use @Autowired correctly in spring boot standalone app

I figured out my problem, and it was a pretty dumb one. First off, i had not annotated my main class with @Component so Spring never bothered to inject the dependencies in it. Secondly, I did not realize that a method annotated with @PostContruct would run by itself after the constructor runs WITHOUT NEEDING TO EXPLICITELY BE CALLED!

I moved all my initialization code to an init method annotated with @PostConstruct and annotated my main class with @Component, everything is working now!

can we implement Spring Autowiring in Java Standalone Application (Swing)

Not sure if I understood you right. I assume, that you want to autowire your DAOs, Services etc. in UI classes. But in order do to that, these UI classes would have to be Spring Beans themselves.

What you could do, is to register each UI class in the Spring application context, when its created. To do that, you could create the following class:

public class BeanProvider {

private static ApplicationContext applicationContext;

/**
* Autowires the specified object in the spring context
*
* @param object
*/
public static void autowire(Object object) {
applicationContext.getAutowireCapableBeanFactory().autowireBean(object);
}

@Autowired
private void setApplicationContext(ApplicationContext applicationContext) {
BeanProvider.applicationContext = applicationContext;
}

}

and then in the constructor of each UI class:

public MyUiClass(){
BeanProvider.autowire(this);
}

Autowired service null in AspectJ aspect on a Spring Boot / Swing standalone app

My issue turned out to be partly multi-threading related but what I was mostly missing was this :

@Bean
public MessagingLogger messagingLogger(LogEntryService logEntryService)
{
MessagingLogger aspect = Aspects.aspectOf(MessagingLogger.class);

aspect.setLogEntryService(logEntryService);

return aspect;
}

This is how I was able to "inject" my service into my aspect. The second part of my issue turned out to be that i wasn't careful enough in my multi-threading setup (lots of background processes in this app) and i was trying to use the aspect while Spring wasn't even done creating all the services.

Standalone Java Application, no beans get created in the Spring Application Context? (IntelliJ, JavaConfig)

How would Spring be aware of your FTPWatchZip class? you should annotate it with @Component or @Service, and this is what IntelliJ is telling you. or Make it a bean as you did for the others:

    @Bean
public FTPWatchZip getFTPWatch ()
{

return new FTPWatchZip();
}

How can I @Autowire a spring bean that was created from an external jar?

You have to scan at least the package containing the class you want to inject. For example, with Spring 4 annotation:

@Configuration
@ComponentScan("com.package.where.my.class.is")
class Config {
...
}

It is the same principle for XML configuration.



Related Topics



Leave a reply



Submit