Read Spring boot property inside listener - java

My requirement is, I need to initialize some application resources when server is started in spring boot. In order to initialize those resources, I need bunch of properties. So, I have kept those properties in external property file and I am trying to read the properties in my custom listener when spring boot is started. The problem is, I could not get any property values in the listener. I am able to read them after application started without any issues. But, I need them inside listener while application is starting. I am getting below exceptions...How to resolve it. help me pls!
2015-08-20 02:58:59.585 ERROR 9376 --- [ost-startStop-1] o.a.c.c.C.[.[localhost].[/shared] : Exception sending context initialized ev
ent to listener instance of class com.org.man.api.initializer.PropertyInitializerListener
java.lang.NoSuchMethodError: com.org.man.api.beans.property.ConfigProperties.getConfigNames()Ljava/util/List;
at com.org.man.api.beans.property.PropertyBeanParser.initializeConfigProperties(PropertyBeanParser.java:33)
at com.org.man.api.initializer.J2eeInitializer.getJ2eePresets(J2eeInitializer.java:79)
at com.org.man.api.initializer.J2eeInitializer.initialize(J2eeInitializer.java:36)
at com.org.man.api.initializer.PropertyInitializerListener.contextInitialized(PropertyInitializerListener.java:81)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
2015-08-20 02:58:59.592 ERROR 9376 --- [ost-startStop-1] o.apache.catalina.core.StandardContext : One or more listeners failed to start. F
ull details will be found in the appropriate container log file
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Fac
tory method 'viewControllerHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: The resources may not be ac
cessed if they are not currently started
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 23 common frames omitted
Caused by: java.lang.IllegalStateException: The resources may not be accessed if they are not currently started
at org.apache.catalina.webresources.StandardRoot.validate(StandardRoot.java:245)
at org.apache.catalina.webresources.StandardRoot.getResource(StandardRoot.java:212)
Listener code
public class PropertyInitializerListener implements ServletContextListener {
private static final String INITIALIZED = PropertyInitializerListener.class.getName() + ".INITIALIZED";
private J2eeInitializer initializer;
#Autowired
PropertyBeanParser parser;
public void contextDestroyed(ServletContextEvent event) {
if (initializer != null) {
initializer.terminate();
}
ServletContext context = event.getServletContext();
context.removeAttribute(FileSearcher.CONFIG_FILE_PROP);
}
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
if (context.getAttribute(INITIALIZED) != null) {
throw new IllegalStateException(
"Already initialized - " +
"check whether you have multiple <listener> definitions in your web.xml!");
}
ConfigBean presets = super.getPresets();
presets = parser.initializeConfigProperties();
SmapiDebug.setSaveMode(true);
SmapiDebug.info("contextInitialized");
PropertyBeanparser code
#Configuration
#EnableConfigurationProperties({ConfigProperties.class,LoggingProperties.class,
InstrumentationProperties.class,KeyeventProperties.class})
public class PropertyBeanParser {
#Autowired
private ConfigProperties configProperties;
#Autowired
private LoggingProperties loggingProperties;
#Autowired
private InstrumentationProperties instrumentationProperties;
#Autowired
private KeyeventProperties keyeventProperties;
public ConfigBean initializeConfigProperties(){
ConfigBean configBean = new ConfigBean();
try{
if(configProperties.getConfigNames()!=null && configProperties.getConfigValues()!=null) {
if(configProperties.getConfigNames().size()==configProperties.getConfigValues().size()){
for(int i=0;i<=configProperties.getConfigNames().size();i++){
ConfigVarDefinitionBean var = new ConfigVarDefinitionBean(configProperties.getConfigNames().get(i),
configProperties.getConfigValues().get(i));
configBean.addConfigVarDefinition(var);
}
}
else{
throw new Exception("number of names and values are not matching");
}
}
}
catch(Exception e){
e.getMessage();
}
return configBean;
}
}
ConfigProperties class
#Configuration
#ConfigurationProperties(locations = "file:config.properties", prefix = "config")
public class ConfigProperties {
private List<String> configNames = new ArrayList<String>();
private List<String> configValues = new ArrayList<String>();
public List<String> getConfigNames() {
return configNames;
}
public void setConfigNames(List<String> configNames) {
this.configNames = configNames;
}
public List<String> getConfigValues() {
return configValues;
}
public void setConfigValues(List<String> configValues) {
this.configValues = configValues;
}
}
Config.Properties
config.configNames[0]=test1
config.configNames[1]=Testserver
config.configNames[2]=ResourceId
config.configNames[3]=AdaptorName
config.configNames[4]=runLevel
config.configValues[0]=ServiceComp
config.configValues[1]=Test
config.configValues[2]=instance2
config.configValues[3]=test
config.configValues[4]=localhost

The issue is, properties can't be retrieved inside listener during spring boot start up. So, in order to do some initialization in the start up, we can add run method in the class where #SpringBootApplication annotation is set by implementing CommandLineRunner . If you do so, that run method will be executed just before finishing the SpringApplication's run method.This is how I tried.
#SpringBootApplication
public class SpringResource implements CommandLineRunner {
/**
* #param args
*/
#Autowired
PropertyTest test;
public void run(String... args){
test.print();
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringResource.class, args);
}
}
PropertyTest Class
#Configuration
#EnableConfigurationProperties({ConfigProperties.class})
#Controller
public class PropertyTest {
#Autowired
ConfigProperties config;
#RequestMapping(value = "/dummy", method = RequestMethod.GET)
#ResponseBody
public void print() {
// TODO Auto-generated method stub
for(int i=0;i<config.getConfigNames().size();i++)
System.out.println("Im in Property test method. :)" +config.getConfigNames().get(i)+" "+config.getConfigValues().get(i));
}
}

I'm answer from my mobile, but may the problem it's in the listener, you not autowire PropertyBeanparser you create, and you broke spring magic... I think in main app class springboot to declare a listener and make inside spring "flow" hope that help

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.

Sharing an instance of a class across a spring boot application

I have a particular class used to interface with a service that requires initialization. In the application lifecycle, the only place this makes sense is in the start of the application because the rest of the spring application cannot run without it. I had the idea to do this:
#SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
try {
MyRequiredService mrs = new MyRequiredService();
mrs.connect(); // This will throw if it fails
run(MyApplication.class, args);
} catch(MyException e) {
System.out.println("Failed to connect to MyRequiredService!");
}
}
}
This will launch the service and attempt to connect but I have one big problem. How do I pass this class around the application? I need it's functions in the service endpoints I am writing.
I didn't see anything obvious and searching "passing class instance in spring boot application" turns up a bunch of unrelated topics.
Is there a smart, clean way to do this in spring boot? I apologize for a contrived example. The names of the service are unique enough I didn't want to violate any agreements.
You can make Spring do this for you. First, you need to annotate your class with #Service, so Spring will pick it up when scanning for classes.
Then, define an init() method and annotate it with #PostConstruct. Spring will instantiate your MyRequiredService class and call init()
#Service
public class MyRequiredService {
#PostConstruct
public void init() {
connect();
}
public void connect() {
// ...
}
}
You could call connect() from the constructor, but I don't like to define objects that may throw exceptions out of the constructor.
And then, you can use MyRequiredService in some other class by injecting it via the #Autowired annotation:
#Component
public class MyOtherClass {
private final MyRequiredService service;
public MyOtherClass(final MyRequiredService service) {
this.service = service;
}
// Other methods here.
}
This has the same overall effect as what you're trying to do above. If MyRequiredService fails, the application will not start up.
Make it a bean. Then it will be in the ApplicationContext which then you can pass to your desired other classes through the constructor
#Configuration
public class ApplicationConfiguration
{
#Bean
public MyRequiredService myRequiredService()
{
MyRequiredService mrs = new MyRequiredService();
try {
mrs.connect(); // This will throw if it fails
return mrs;
} catch(MyException e) {
log.error("Failed to connect to MyRequiredService!");
throw new IllegalStateException("MyRequiredService failed connection. Stopping startup");
}
}
#Bean
public SomeOtherService someOtherService(MyRequiredService mrs) {
return new SomeOtherService(mrs);
}
}
IMHO Instead of catching the error and logging it. I would throw it and stop the application from starting, but to keep with your example I added the throw IllegalStateException after the log.
Doing it this way Spring will create your MyRequiredService bean in the ApplicationContext then you can see I added as a parameter needed by the bean below that. Spring will grab that bean out of the ApplicationContext and supply it to the bean. If Spring doesn't find the bean in the ApplicationContext it will throw an error and stop the application from startup.
a class implements BeanFactoryPostProcessor which is init before normal bean
#Configuration
public class MyRequiredService implements BeanFactoryPostProcessor,
PriorityOrdered, InitializingBean {
#Override
public int getOrder() {
return Integer.MIN_VALUE;
}
public void connect() {
// ...
}
#Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
#Override
public void afterPropertiesSet() throws Exception {
connect();
}
}

PostConstruct in inner Spring configuration not getting invoked

Im trying to setup multiple filter chains as follows in Spring Security and running into a weird issue.
#EnableWebSecurity
#EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration {
#Configuration("apiWebSecurityConfigurationAdapter")
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
....
}
#Configuration("ssoWebSecurityConfigurerAdapter")
#Order(1)
public static class SSOWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
private Timer backgroundTaskTimer;
private MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager;
#PostConstruct
public void init() {
this.backgroundTaskTimer = new Timer(true);
this.multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
}
#PreDestroy
public void destroy() {
this.backgroundTaskTimer.purge();
this.backgroundTaskTimer.cancel();
this.multiThreadedHttpConnectionManager.shutdown();
}
#Bean
#Qualifier("idp-test")
public ExtendedMetadataDelegate openiamExtendedMetadataProvider()
throws MetadataProviderException, ResourceException {
DefaultResourceLoader loader = new DefaultResourceLoader();
String metadataLocation = "classpath:/sso/idp-test.xml";
ResourceBackedMetadataProvider resourceBackedMetadataProvider =
new ResourceBackedMetadataProvider(backgroundTaskTimer,
new SpringResourceWrapperOpenSAMLResource(loader.getResource(metadataLocation.trim()))); // backgroundTaskTimer is null
resourceBackedMetadataProvider.setParserPool(parserPool());
ExtendedMetadataDelegate extendedMetadataDelegate =
new ExtendedMetadataDelegate(resourceBackedMetadataProvider, extendedMetadata());
extendedMetadataDelegate.setMetadataTrustCheck(true);
extendedMetadataDelegate.setMetadataRequireSignature(false);
backgroundTaskTimer.purge();
return extendedMetadataDelegate;
}
.....
}
}
It causes the error,
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.saml.metadata.ExtendedMetadataDelegate]: Circular reference involving containing bean 'ssoWebSecurityConfigurerAdapter' - consider declaring the factory method as static for independence from its containing instance. Factory method 'testExtendedMetadataProvider' threw exception; nested exception is java.lang.IllegalArgumentException: Task timer may not be null
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 114 common frames omitted
Caused by: java.lang.IllegalArgumentException: Task timer may not be null
at org.opensaml.saml2.metadata.provider.AbstractReloadingMetadataProvider.<init>(AbstractReloadingMetadataProvider.java:105)
at org.opensaml.saml2.metadata.provider.ResourceBackedMetadataProvider.<init>(ResourceBackedMetadataProvider.java:76)
Why doesnt backgroundTaskTimer in the above case get set inside the PostConstruct ?

numberformatexception when parsing TestPropertySource in #PostConstruct

I'm trying to test my LoginCacheServiceImpl but having problems. I followed this tutorial http://crunchify.com/how-to-create-a-simple-in-memory-cache-in-java-lightweight-cache/ and called it InMemoryCache with my own model object LoginEvent.
#Service
#PropertySource("classpath:app.properties")
public class LoginCacheServiceImpl {
#Value("$app{timeToLive}")
private long timeToLive;
#Value("$app{timerInterval}")
private long timerInterval;
private InMemoryCache<String, LoginEvent> cache;
#Override
public InMemoryCache<String, LoginEvent> getCache() {
return cache;
}
#PostConstruct
public void init() {
cache = new InMemoryCache<>(timeToLive, timerInterval, 10000);
}
}
The code runs fine but I'm trying to write unit tests for it. In the test class:
#TestPropertySource("classpath:app-test.properties")
#ContextConfiguration(classes = { LoginCacheServiceImplTest.class, LoginCacheServiceImpl.class })
#RunWith(SpringJUnit4ClassRunner.class)
public class LoginCacheServiceImplTest
#Autowired
private Environment environment;
private LoginCacheServiceImpl cacheService;
#Before
public void setUp() {
String test = environment.getProperty("timeToLive"); // this value gets parsed correctly
cacheService = new LoginCacheServiceImpl();
}
#Test
public void doNothing() {
System.out.println("test");
}
}
I get the error:
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: "$app{timeToLive}"
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:77)
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:54)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:968)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 42 more
Caused by: java.lang.NumberFormatException: For input string: "$app{timeToLive}"
I'm not sure how I should either rewrite my LoginCacheServiceImpl to make it testable, or preferably, to set up my test class so PostConstruct parses the timeToLive and timerInterval properly.
app-test.properties:
timeToLive=1000
timerInterval=1000
As an aside, I'm using Mockito 2.X. Can you use both MockitoJUnitRunner and SpringJUnit4ClassRunner? If not, how do you choose which one to use (sorry, new to Java).
Edit:
If I have this line
#ContextConfiguration(classes = { LoginCacheServiceImplTest.class, LoginCacheServiceImpl.class })
the unit test gives me that numbeformatexception error when I try to run the test as it is trying to create LoginCacheServiceImpl. But if I change it to
#ContextConfiguration(classes = { LoginCacheServiceImplTest.class })
and just do
String prop = environment.getProperty("timeToLive");
where in my app-test.properties file has
timeToLive=1000
the unit test can read that properties file. The timeToLive=1000 is a different number than I have in the original app.properties file so I do not think it's an issue of finding the app-test.properties file.
Please check the below updated class:
#Service
#PropertySource("classpath:app.properties")
public class LoginCacheServiceImpl {
#Value("${timeToLive}")
private long timeToLive;
#Value("${timerInterval}")
private long timerInterval;
private InMemoryCache<String, LoginEvent> cache;
#Override
public InMemoryCache<String, LoginEvent> getCache() {
return cache;
}
#PostConstruct
public void init() {
cache = new InMemoryCache<>(timeToLive, timerInterval, 10000);
}
}
if your propertySource able to find app.properties on classpath correctly, you can use ${keyname} to access the property.
Environment object already have properties configured by spring container as it extend PropertyResolver. check the docs for the same.Environment spring
or instead of using #Value, you can autowire the #Environment and use it in your service class.

Programmatically adding Beans to Spring application Context

I am trying to add a simple String to my Spring Application Context, and then autowire this to a different existing bean (A) within the application context. I know this is not the usual way to go, yet I need to add many beans programmatically, which would otherwise make my xml configuration huge.
public class MyPostProcessor implements BeanFactoryPostProcessor, Ordered {
#Override
public int getOrder() {
return 0;
}
#Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerSingleton("myString", "this is the String");
A a = beanFactory.getBean(A.class);
beanFactory.autowireBean(a);
}
}
public class A {
#Autowired
public transient String message;
}
When running this, the property message of the instance of A is null. What am I missing?
EDIT: this is my application context:
#Configuration
class TestConfig {
#Bean
public A a() {
return new A();
}
#Bean
public MyPostProcessor postProcessor() {
return new MyPostProcessor();
}
}
And this is my test:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = TestConfig.class)
public class MyTest {
#Autowired
private transient A a;
#Test
public void test() throws Exception {
System.err.println("Running");
System.err.println("This is the autowired String: " + a.message);
Thread.sleep(1000);
}
}
Thanks
You should not instantiate beans from BeanFactoryPostprocessors.
From BeanFactoryPostProcessor JavaDoc:
A BeanFactoryPostProcessor may interact with and modify bean
definitions, but never bean instances. Doing so may cause premature
bean instantiation, violating the container and causing unintended
side-effects.
In your case, the A bean is instantiated before BeanPostProcessors and therefore not autowired.
Remove the lines:
A a = beanFactory.getBean(A.class);
beanFactory.autowireBean(a);
And will work.
Try using the #Qualifier to specific which bean you want to Auto wire.
public class A {
#Autowired
#Qualifier("myString")
public transient String message;
}

Categories