Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
We have developed an application using following technologies stack:
Struts 2.1
Springs 3.1
Groovy 1.8
Tomcat 6.0.26 - For deployment
Our requirement is to migrate a specific layer of application to Groovy. We have migrated that layer to groovy and application is working fine. After the migration groovy beans are initialized properly by spring container but we are not able to utilize the 'dynamic language' feature of Groovy. That is when we modify Groovy script, updated changes are not reflected back to application dynamically.
For reference I am attaching a method of groovy script (GroovyConnection.groovy):
#Component
class GroovyConnection implements EndPointManager, Serializable{
private static final long serialVersionUID = 1481681752777429674L;
private final static Logger log = Logger.getLogger(GroovyConnection.class);
private static final String DESTINATION_DIR = ARTIFACT_DIR_PATH.toString()+"/";
private static final String JIVE_USER = ""; //"jivescpt";
/**
* #param default
*/
public GroovyConnection() {
super();
jschInstance = new JSch();
}
public String executeCommands(Session session, String inputCommand, Device device) {
log.info("Entering executeCommand() device:" + inputCommand);
Channel channel = null;
PipedOutputStream commandIO = null;
InputStream sessionInput = null;
InputStream sessionOutput = null;
InputStream sessionError = null;
String commandResponse = null;
try {
channel = session.openChannel("shell");
commandIO = new PipedOutputStream();
sessionInput = new PipedInputStream(commandIO);
channel.setInputStream(sessionInput);
sessionOutput = channel.getInputStream();
sessionError = channel.getExtInputStream();
channel.connect();
return fireCommandsOnTerminal(session, commandIO, sessionOutput, sessionError, inputCommand, device);
}
catch (JSchException e) {
log.error(AutomationConstants.ERROR_MSG.toString(), e);
}
catch (IOException e) {
log.error(AutomationConstants.ERROR_MSG.toString(), e);
}
catch (InterruptedException e) {
log.error(AutomationConstants.ERROR_MSG.toString(), e);
}
finally {
boolean isClosed = GroovyConnectionHelper.closeAll(session, channel, commandIO, sessionInput, sessionOutput, sessionError);
if (!isClosed) {
return AutomationUtils.getPropertyValue("jive.vpn.error.msg");
}
}
log.info("Exiting executeCommand() device:" + inputCommand);
return commandResponse;
}
}
Groovy-context file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
<lang:defaults refresh-check-delay="2000"/>
<lang:groovy id="endPointManager"
script-source="classpath:GroovyConnection.groovy" refresh-check-delay="1000">
</lang:groovy>
</beans>
Even after given refresh-check-delay time for 1 second groovy beans are not behaving accordingly at runtime. I have to restart Tomcat to redeploy updated groovy changes.
Looks like application treating groovy beans as java beans.
Apart from this if I execute a groovy script without using Springs, it executes as expected. Modified groovy script changes are reflected back dynamically. Here is code for reference:
groovy-script:
package com
public class test1 {
def hello_world() {
println "Connectddded"
}
}
Main Class:
public static void main(String arg[]) {
try {
new GroovyShell().parse(newFile("test1.groovy")).invokeMethod("hello_world", null);
} catch (CompilationFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
According to my understanding either we are missing something in Spring-Groovy Integration configuration that allows us to refresh back the Groovy beans modification or Tomcat is not allowing us refresh back the updated groovy script.
If someone can provide any suggesting on this would be helpful. Thanks in advance.
Regards,
Divya Garg
Thanks all, who have spent their time to read my post. Issue is resolved.
Actually we were using #component and groovy-context.xml file simultaneously because of that we were facing this issue. I have removed annotation and put my groovy file in classpath. Now everything is working file as expected. Groovy beans are getting refresh properly. But I coluld not achieve the same using spring annotations.
Now I am facing another issue and I tried a lot for it. I have another groovy bean where I have my all static methods called (GroovyConnectionHelper.groovy). I am not able to call these static methods in GroovyConnection.script. When application executes applcation get hangs on the place where execution reaches to GroovyConnectionHelper static method.
To resolve the issue I had tried to inject GroovyConnectionHelper bean in GroovyConnection as follows(Which is not good approach):
groovy-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
<lang:defaults refresh-check-delay="1000"/>
<lang:groovy name="endPointManager" script-source="classpath:GroovyConnection.groovy">
<lang:property name="groovyConnectionHelper" ref="groovyConnectionHelper"/>
</lang:groovy>
<lang:groovy id="groovyConnectionHelper" script-source="classpath:GroovyConnectionHelper.groovy"/>
</beans>
And i put the setter for the same in GroovyConnection.groovy:
public void setGroovyConnectionHelper(GroovyConnectionHelper groovyConnectionHelper) {
System.out.println( "Setting ----------------------------------------- groovyConnectionHelper : " + groovyConnectionHelper );
if ( null != groovyConnectionHelper ){
System.out.println( "Object groovyConnectionHelper: ::: " + groovyConnectionHelper.dump() );
}
this.groovyConnectionHelper = groovyConnectionHelper;
}
But this is not working out. Facing this error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scripting.groovy.GroovyScriptFactory#0': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scriptedObject.org.springframework.scripting.groovy.GroovyScriptFactory#0': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'groovyConnectionHelper' of bean class [GroovyConnection]: Bean property 'groovyConnectionHelper' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:452)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
... 71 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scriptedObject.org.springframework.scripting.groovy.GroovyScriptFactory#0': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'groovyConnectionHelper' of bean class [GroovyConnection]: Bean property 'groovyConnectionHelper' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1396)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.aop.target.dynamic.BeanFactoryRefreshableTargetSource.obtainFreshBean(BeanFactoryRefreshableTargetSource.java:77)
at org.springframework.scripting.support.RefreshableScriptTargetSource.obtainFreshBean(RefreshableScriptTargetSource.java:79)
at org.springframework.aop.target.dynamic.BeanFactoryRefreshableTargetSource.freshTarget(BeanFactoryRefreshableTargetSource.java:66)
at org.springframework.aop.target.dynamic.AbstractRefreshableTargetSource.refresh(AbstractRefreshableTargetSource.java:97)
at org.springframework.aop.target.dynamic.AbstractRefreshableTargetSource.getTargetClass(AbstractRefreshableTargetSource.java:68)
at org.springframework.scripting.support.ScriptFactoryPostProcessor.createRefreshableProxy(ScriptFactoryPostProcessor.java:553)
at org.springframework.scripting.support.ScriptFactoryPostProcessor.postProcessBeforeInstantiation(ScriptFactoryPostProcessor.java:322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:880)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:852)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:446)
... 79 more
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'groovyConnectionHelper' of bean class [GroovyConnection]: Bean property 'groovyConnectionHelper' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:924)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
... 94 more
Please suggest should I use Groovy Bean Injection for this or we can directely call groovy static methods direcly. Thanks in advance
Regards,
Divya Garg
Related
I'm posting my query after visiting numerous question posted before on same context, unfortunately none of them resolved my problem. I'm trying a complete xml based configuration in spring without using any annotation. But as soon as i launch the application, i'm getting the following exception.
Cannot find class [com.kashyap.springboot.xml.JdbcConnectionXML.java]
for bean with name 'connection' defined in class path resource
[applicationContext.xml]; nested exception is
java.lang.ClassNotFoundException:
com.kashyap.springboot.xml.JdbcConnectionXML.java
Things i've already taken care of :-
Namespace for component scan re-examined.
Tag for component scan entered.
Bean tag with proper classes' qualified name re-examined.
Bean property and references re-visited.
applicationContext.xml is located on path src/main/resources so no
absolute path required.
Context initialized with ClassPathXmlApplicationContext class.
Classes are already defined with default public constructors for instantiation required by reflection.(though it is not mandatory, used it as precaution). Alas!!! none of the these things worked. Please find the classes and xml below.
package com.kashyap.springboot.xml;
public class JdbcConnectionXML {
public JdbcConnectionXML(){
System.out.println("JDBC XML Constructor Called");
}
}
package com.kashyap.springboot.xml;
public class PrototypeXMLDAO {
public PrototypeXMLDAO(){
System.out.println("PrototypeXMLDAO Constructor Called");
}
private JdbcConnectionXML connection;
public JdbcConnectionXML getConnection() {
return connection;
}
public void setConnection(JdbcConnectionXML connection) {
this.connection = connection;
}
}
package com.kashyap.springboot.xml;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringbootdevApplication {
public static void main(String[] args) {
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");) {
PrototypeXMLDAO pdCDIOne = context.getBean(PrototypeXMLDAO.class);
PrototypeXMLDAO pdCDITwo = context.getBean(PrototypeXMLDAO.class);
System.out.println(pdCDIOne);
System.out.println(pdCDIOne.getConnection());
System.out.println(pdCDITwo);
System.out.println(pdCDITwo.getConnection());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.kashyap.springboot.xml" />
<bean id="connection" class="com.kashyap.springboot.xml.JdbcConnectionXML">
</bean>
<bean id="prototype" class="com.kashyap.springboot.xml.PrototypeXMLDAO">
<property name="connection" ref="connection"></property>
</bean>
</beans>
The stack-trace on console as follows:-
Exception in thread "main"
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot
find class [com.kashyap.springboot.xml.JdbcConnectionXML.java] for
bean with name 'connection' defined in class path resource
[applicationContext.xml]; nested exception is
java.lang.ClassNotFoundException:
com.kashyap.springboot.xml.JdbcConnectionXML.java at
org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1380)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:670)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:637)
at
org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1489)
at
org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1007)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:739)
at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at
org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:144)
at
org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:85)
at
com.kashyap.springboot.xml.SpringbootdevApplication.main(SpringbootdevApplication.java:13)
Caused by: java.lang.ClassNotFoundException:
com.kashyap.springboot.xml.JdbcConnectionXML.java at
java.net.URLClassLoader.findClass(URLClassLoader.java:381) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) at
org.springframework.util.ClassUtils.forName(ClassUtils.java:255) at
org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:418)
at
org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1428)
at
org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1372)
... 10 more
If required i'll post the pom.xml too. Any sort of help will be highly appreciated.
I have implemented a simple file upload routine using Spring Boot ConfigurationProperties annotation so I can load the directory from my YAML configuration file:
Service:
#Service
public class FileSystemStorageService implements StorageService {
private final Logger log = LoggerFactory.getLogger(FileSystemStorageService.class);
private final Path pictureLocation;
#Autowired
public FileSystemStorageService(StorageProperties storageProperties) {
pictureLocation = Paths.get(storageProperties.getUpload());
}
And the StorageProperties:
#Component
#ConfigurationProperties("nutrilife.meals")
public class StorageProperties {
private String upload;
public String getUpload() {
return upload;
}
public void setUpload(String upload) {
this.upload= upload;
}
}
In the yaml file I have:
nutrilife:
meals:
upload: /$full_path_to_my_upload_dir
This works perfectly in normal Spring Boot runtime but the problem starts when I try to run my integration tests, it throws the error:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileSystemStorageService' defined in file [/home/mmaia/git/nutrilife/build/classes/main/com/getnutrilife/service/upload/FileSystemStorageService.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.getnutrilife.service.upload.FileSystemStorageService]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.getnutrilife.service.upload.FileSystemStorageService]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
So basically during tests the YAML file configuration looks like it's not being properly picked up. I am new to Spring. How can I fix it?
try adding the #value annotation :
#Value("upload")
private String upload;
The above answer is valid for application.properties config.
it also works with yaml as well.
You may find your correct correct yaml config here :
24.6 Using YAML instead of Properties
I am trying to inject a bean into an application using an XML file. The main function has
try(ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring/application.xml")) {
context.registerShutdownHook();
app.setResourceLoader(context);
app.run(args);
} catch (final Exception ex) {
ex.printStackTrace();
}
I also have a Person POJO and is set in the xml file.
The xml defination is as follows:
<context:annotation-config/>
<bean id="person" class="hello.service.Person" p:name="Ben" p:age="25" />
<bean class="hello.HelloBeanPostProcessor"/>
The link to my repo is:
https://bitbucket.org/rkc2015/gs-scheduling-tasks-complete
It is the default guide from Spring boot that does a scheduled task.
I'm trying to inject the Person POJO defined in the xml file into a scheduled task.
I am currently getting this error:
Error creating bean with name 'scheduledTasks': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private hello.service.Person
hello.service.ScheduledTasks.person; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [hello.service.Person] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for
this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
Can anyone please help? I am new to Spring.
You can use #ImportResource annotation to import xml configurations.
Documentation link
#SpringBootApplication
#EnableScheduling
#ImportResource("/spring/application.xml")
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(Application.class);
app.run();
}
}
If this is through spring bean you should have used #component annotation for you bean definition or else i application.xml you should have defined scheduledTasks bean also and with it member variable of person so that both beans are created and can be autowired.
I am new to spring. I am developing one sample project using spring. I am getting the following exception when I provide a value in Beans.xml. Please provide solution.
Beans.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.dhr.spring.beans.HelloWorld">
<property name="message" value="d" />
</bean>
</beans>
HelloWorld.java:
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
MainApp:
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
Exception:
Caused by: java.lang.NoSuchFieldError: NULL
at org.springframework.expression.TypedValue.<clinit>(TypedValue.java:31)
at org.springframework.expression.spel.support.StandardEvaluationContext.setRootObject(StandardEvaluationContext.java:85)
at org.springframework.expression.spel.support.StandardEvaluationContext.<init>(StandardEvaluationContext.java:74)
at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:124)
at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1299)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.evaluate(BeanDefinitionValueResolver.java:210)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:182)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
... 11 more
Welcome! To the ambiguous world of Spring! Where stack traces are long and documentation is short convoluted.
I have seen this error several times and, at least for me, it usually has to do with dependency inconsistencies.
I see that you just posted you are not using Maven. I can almost assure you that that is the problem. You really SHOULD be using Maven. The amount of work it will save you is monumental.
Edit: To make this post more answer-like: You are getting the error due to dependency inconsistencies. You are manually putting Spring JAR files into your dependency tree which is not recommended.
Either 1) Use Maven to handle dependency management (recommended) or 2) Ensure that all your JAR files utilize non-conflicting and coherent dependency trees.
For instance, you are using org.springframework.expression-3.0.3.RELEASE which is not part of the 3.1.x group. So, replace the jar file with org.springframework.expression-3.1.0-RELEASE
I've a weird problem of unable to instantiate a bean which is injected to another bean.
The PropertiesUtil is the bean in question. It's injected to the LoginController class as follows in my sn-servlet.xml
<bean name="/Login.html" class="org.sn.auth.LoginController">
<property name="dbUtil" ref="dbUtil"/>
<property name="propertiesUtil" ref="propertiesUtil"/>
</bean>
and my PropertiesUtil.java is
public class PropertiesUtil {
private Properties properties;
public PropertiesUtil() {
properties = new Properties();
try {
properties.load(ClassLoader.getSystemResourceAsStream(
"/resources/messages.properties"));
}
catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
And the NullPointerException occurs at the line where I try to use the properties to load a resource. I'm really confused why it's null when I'm clearly instanting it in the previous line.
I've also tried injecting the properties instance as a constructor-arg and also as a property from the sn-servlet.xml, but all in vain.
Is there something like I'm not supposed to do any operations in a constructor when that bean is spring-injected to some other class?
Thanks for any ideas!
Check out the javadoc for ClassLoader.getResourceAsStream(). It returns null if the resource cannot be found. So it would seem that messages.properties cannot be found and the Properties.load() method will throw a NullPointerException when trying to read from a null InputStream.