Understanding Spring Boot #Autowired - java

I don't understand how spring boot's annotation #Autowired correctly works. Here is a simple example:
#SpringBootApplication
public class App {
#Autowired
public Starter starter;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
public App() {
System.out.println("init App");
//starter.init();
}
}
--
#Repository
public class Starter {
public Starter() {System.out.println("init Starter");}
public void init() { System.out.println("call init"); }
}
When I execute this code, I get the logs init App and init Starter, so spring creates this objects. But when I call the init method from Starter in App, I get a NullPointerException. Is there more I need to do other than using the annotation #Autowired to init my object?
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'app': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [{package}.App$$EnhancerBySpringCGLIB$$87a7ccf]: Constructor threw exception; nested exception is java.lang.NullPointerException

When you call the init method from the constructor of class App, Spring has not yet autowired the dependencies into the App object. If you want to call this method after Spring has finished creating and autowiring the App object, then add a method with a #PostConstruct annotation to do this, for example:
#SpringBootApplication
public class App {
#Autowired
public Starter starter;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
public App() {
System.out.println("constructor of App");
}
#PostConstruct
public void init() {
System.out.println("Calling starter.init");
starter.init();
}
}

Related

coundn't get bean when running a jar packed from a springboot project

I can run my springboot project in IDEA nicely but when packed it to a jar and run with the java command, just got the java.lang.NullPointerException when getting a bean from spring context.
the first class which just got errors:
#Service
public class MdspiImpl extends CThostFtdcMdSpi {
public MdspiImpl(CThostFtdcMdApi mdapi) {
m_mdapi = mdapi;
logger.info("MdspiImpl is creating...");
***mdr = SpringContextUtil.getBean("marketDataRobot");//this is the error code***
}
}
the second class:
#Service
public class MarketDataRobot {
}
the SpringContextUtil class:
#Component("SpringContextUtil")
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static <T> T getBean(String name) {
return (T) applicationContext.getBean(name);
}
}
the gradle file:
jar {
baseName = 'programmingTrading'
version = '0.1.0'
manifest {
attributes 'Main-Class': 'com.blackHole.programmingTrading'
}
}
the running exception:
WARN main[AbstractApplicationContext.java:557 Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mdspiImpl' defined in URL [jar:file:/E:/workspace/simuPrd/programmingTrading-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/blackHole/programmingTrading/infrastructure/MdspiImpl.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.blackHole.programmingTrading.infrastructure.MdspiImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException]
[com.blackHole.programmingTrading.infrastructure.MdspiImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:184)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:300)
... 27 common frames omitted
Caused by: java.lang.NullPointerException: null
at com.blackHole.programmingTrading.SpringContextUtil.getBean(SpringContextUtil.java:35)
at com.blackHole.programmingTrading.infrastructure.MdspiImpl.<init>(MdspiImpl.java:46)
It also stem from another problem: #Autowired annotation doesn't work...
when using like this:
#Component
public class Scu{
}
in another class:
#Autowired
private Scu scu;
logger.info(String.format("MdspiImpl is creating...[%s]", scu.toString()));
will get a java.lang.NullPointerException: null
spring-boot configuration like this:
#SpringBootApplication
public class ProgrammingTrading {
public static void main(String[] args) {
SpringApplication.run(ProgrammingTrading.class, args);
}
}
that is part of reasons of using SpringContextUtil to get the bean...
thanks a lot!
SpringContextUtil shouldn't be a accessed statically like you are doing... Since you define it as a #Component do the following;
#Service
public class MdspiImpl extends CThostFtdcMdSpi {
#Autowired
private SpringContextUtil springContextUtil;
public MdspiImpl(CThostFtdcMdApi mdapi) {
m_mdapi = mdapi;
logger.info("MdspiImpl is creating...");
***mdr = springContextUtil.getBean("marketDataRobot");
}
}
Due to SpringContextUtil not being injected via Spring, but simply accessed statically, the applicationContext inside of it is ignored and is null in your case.
Also remove the static modifier;
#Component
public class SpringContextUtil implements ApplicationContextAware {
private ApplicationContext applicationContext;
// include getter/setter for applicationContext as well
public <T> T getBean(String name) {
return (T) applicationContext.getBean(name);
}
}
edit
The trouble from the latest example project;
#Service
public class ExampleService {
#Autowired
private Logger logger;
public ExampleService() {
this.logger=logger;
logger.info("Im working");
}
}
Here the Logger will be null, when the ExampleService constructor is triggered, since the constructor is called before the injection starts, but you can merge this behaviour if you incorporate the injection through the said constructor as follows;
#Service
public class ExampleService {
private final Logger logger;
public ExampleService(Logger logger) {
this.logger = logger;
logger.info("Im working");
}
}
Works perfectly without any trouble...
You should never be accessing beans programmatically like you did with this SpringContextUtil, just inject MarketDataRobot in the constructor of MdspiImpl and you’re good to go (since it’s annotated with #Service). The preferred way is to use constructor injection instead of field injection, which will make it easier for you to write unit tests. You can also get rid of #Autowired if you have only one constructor.

Spring service bean is null

I have a simple Spring boot application that initialize a CLI game.
import com.rpg.game.rpggame.client.RpgGameClient;
#SpringBootApplication
public class RpgGameApplication {
public static void main(String[] args) {
SpringApplication.run(RpgGameApplication.class, args);
RpgGameClient.runGame();
}
}
The runGame() uses some of my services (spring beans).
public class RpgGameClient {
#Autowired
private static GameService gameService;
public static void runGame() {
gameService.createNewGame();
}
}
But I have a NullPointerException when using my service, since Spring can not successfully inject it on my RpgGameClient class.
How can I solve it?
1) RpgGameClient is not declared as a bean candidate. So Spring ignores it.
You can do it by annotating the class with #Component for example (the simplest way) or by declaring a #Bean method that returns the instance of the class.
2) Even with that, it will not work still as #Autowired doesn't work for static fields. Spring injects dependencies into a bean and a bean is an instance of a class.
I think that runGame() should also be non static. Make all static for a bean spring makes no sense. It is the same thing for the Spring boot application class.
3) Constructor injection should be favored to field injection.
So it should be better :
#Component
public class RpgGameClient {
private GameService gameService;
public RpgGameClient(GameService gameService){
this.gameService = gameService;
}
public void runGame() {
gameService.createNewGame();
}
}
And change the Spring Boot app class to use injection dependency such as :
#SpringBootApplication
public class RpgGameApplication {
#Autowired
RpgGameClient rpgGameClient;
#PostConstruct
public void postConstruct(){
rpgGameClient.runGame();
}
public static void main(String[] args) {
SpringApplication.run(RpgGameApplication.class, args);
}
}

How could I get #FeignClient bean when spring boot application initializing

I need use a bean inject with #Component #FeignClient(name = "xxx") when my spring boot application initializing, but it always throws exception like this:
20180706 10:18:40,043 WARN [main]
[org.springframework.context.annotation.AnnotationConfigApplicationContext]
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'feignContract' defined in org.springframework.cloud.netflix.feign.FeignClientsConfiguration: Unsatisfied dependency expressed through method 'feignContract' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feignConversionService' defined in org.springframework.cloud.netflix.feign.FeignClientsConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'feignConversionService' threw exception; nested exception is java.lang.StackOverflowError
my feignClient code:
#Component
#FeignClient(name = "domain-account")
public interface IDomainService {
#RequestMapping(value = "/userInfos", method = RequestMethod.GET)
public String getUserInfos(#QueryMap Map<String, Object> condition);
}
ApplicationListenner code:
public class GlobalInit implements ApplicationListener<ContextRefreshedEvent> {
#Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
System.out.println("======== GlobalInit ========");
IDomainService domainService = contextRefreshedEvent.getApplicationContext().getBean(IDomainService.class);
System.out.println("*********************" + domainService);
GlobalInitManager.getInstance().doInit();
}
}
It's not entirely clear for me what you try to do with the GlobalInit but the 'standard' way of designing your Feign client in Spring Boot would the following:
#SpringBootApplication
#EnableFeignClients
#EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
#EnableCaching
public class MyHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(MyHelloWorldApplication.class, args);
}
}
#Component
public class HelloWorldServiceImpl implements HelloWorldService {
#Autowired
private IDomainService iDomainService ;
public void myMethod() {
String userinfo = iDomainService.getUserInfos(...);
}
}
Hopefully this helps.
All the best,
Wim

All my #Autowired beans are null

I can't figure out why all my beans are null in the controller. I understand that this is a common question but I am not instantiating the object with new.
Controller:
#Controller
#RequestMapping("/store")
public class MyController {
#Autowired
private MyService myService;
#GetMapping("/getOptions")
private String getOptions(HttpServletRequest request)
{
myService.doSomething(request);
....
}
}
When I request http://localhost/store/getOptions I get a NullPointerException on myService.
MyService
#Service
public class MyService
{
....
}
WebConfig
#Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
#Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping()
{
return new BeanNameUrlHandlerMapping();
}
}
Main
#SpringBootApplication
#Configuration
#ComponentScan(basePackages = { "com.mypackage.config", "com.mypackage.service", "com.mypackage.controller"})
public class MyApplication
{
public static void main(String[] args)
{
if (AuthConfigFactory.getFactory() == null)
AuthConfigFactory.setFactory(new AuthConfigFactoryImpl());
SpringApplication.run(MyApplication.class, args);
}
}
When I start the application, I can see in the logs that the myService bean is actually being Autowired into the controller:
- Processing injected element of bean 'myController': AutowiredFieldElement for private com.mypackage.service.MyService com.mypackage.controller.MyController.myService
- Returning cached instance of singleton bean 'MyService'
- Autowiring by type from bean name 'myController' to bean named 'myService'
So I don't understand why when I try to access myService within the controller, it is null. I do not instantiate the controller anywhere with new.
Clipped Exception
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause:
java.lang.NullPointerException: null
at com.mypackage.controller.MyController.getOptions(MyController.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
....
Edit:
I've removed web.xml from my project, as it is irrelevant.
My problem was that some of the methods in my controller were annotated as #Transactional. I did not provide enough information in my original post for anyone to figure that out. I found the answer here: Why we shouldn't make a Spring MVC controller #Transactional?
Spring java configuration:
#Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
#Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping(){
return new BeanNameUrlHandlerMapping();
}
}
Service class:
#Service("myService")
public class MyServiceImpl implements MyService{
#Override
public String getMessageinfo() {
// TODO Auto-generated method stub
return "Hello World!!";
}
}
Service interface:
public interface MyService {
public String getMessageinfo();
}
Controller class:
#RestController
#RequestMapping("/hello")
public class MyController {
#Autowired
private MyService myService;
#GetMapping("/getMessage")
//#ResponseBody
private String getMessage(HttpServletRequest req){
System.out.println("inside controller : - "+myService.getMessageinfo());
return myService.getMessageinfo();
}
}
SpringbootMain
#SpringBootApplication
#Configuration
#ComponentScan(basePackages={"com.example.demo.config","com.example.demo.controller","com.example.demo.service"})
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
This worked for me. without exception, but i used eclipse STS IDE and created Spring boot project.
http://localhost:8080/hello/getMessage

Nullpointer Exception when trying to access Injected Bean

I have a maven multi module project. When trying to Access a injected bean i get a Nullpointer Exception.
this is the main Function that startsthe application
public class App {
public static void main(String[] args) {
System.out.println("Startpoint");
DecisionMaker decisionMaker = new DecisionMaker();
decisionMaker.run();
}
}
this is what i do in the DecisionMaker
public class DecisionMaker {
#Inject
GameListener gm;
#Inject
BasicProductionManager basicProductionManager;
public DecisionMaker() {
System.out.println("this is the decisionmaker");
System.out.println(gm.toString());
}
so this is not an bean but a normal pojo
the gamelistener is a Applicationscoped bean which i want to inject.
#Named
#ApplicationScoped
public class GameListener extends DefaultBWListener {
#Inject
Event<OnFrameEvent> onFrameEvent;
public Mirror mirror = new Mirror();
public Game game;
public Player self;
#PostConstruct
public void init() {
System.out.println("init listener");
}
the nullpointer gets thrown in the constructor of the DecisionMaker. The #PostConscrutct init method is not called
I looked into similar question but all i found is that i needthe PostConstruct method which i already have.
you can't instantiate DecisionMaker yourself. the whole point of CDI is to give it control of creating and managing beans.
check out this article to see how to use CDI in JAVA SE.

Categories