I am kind of a newbie in spring and wondering why my spring (not springboot) program is not working. It is a simple one and that's what is bothering me the most.
I have a main class as follows:
package com.something.main;
@Configuration
@ComponentScan (
"com.something"
)
public class Main {
public static void main(String[] args) throws IOException {
String someValue = SecondClass.secondMethod("someString");
}
}
SecondClass is defined as follows:
package com.something.somethingElse;
@Component
public class SecondClass {
@Autowired
private ObjectMapper om;
private static ObjectMapper objectMapper;
@PostConstruct
public void init(){
objectMapper = this.om;
}
public static String secondMethod(String input){
String x = objectMapper.<<someOperation>>;
return x;
}
}
ObjectMapper is injected in spring ioc as follows:
package com.something.config;
@Configuration
public class ObjectMapperConfig {
@Bean
ObjectMapper getObjectMapper(){
ObjectMapper om = new ObjectMapper();
return om;
}
}
Now when I try to run the main method in dubug mode, secondMethod has objectMapper as null always. I am not sure what am i missing here. I don't even see breakpoints in ObjectMapper bean creating method hitting when I try to run the application in debug mode. My understanding is that it would standup the spring IOC on startup and then run the main method. Not happening in my case.
Also, one more thing that I don't know it works is when I create a jar of this application and include it as a dependency in another project. The base project may not have spring or even using spring for that mapper. How would that project that includes this jar as an external dependency be able to call secondMethod in secondClass if springIOC container isn't set up in the consuming application.
Can someone please help me?