Hi,
I am new to Spring. I have following class where i am using autowire annotation
package com.almas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(locations = { "classpath:applicationContext-service.xml" })
public class SpringTrial {
ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext-service.xml");
@Autowired
Example example;
public static void main(String args[]) {
SpringTrial trail = new SpringTrial();
trail.example.hello();
}
}
I have follwoing example class
package com.almas;
import org.springframework.beans.factory.annotation.Autowired;
public class Example {
public Example() {
System.out.println("Hello11");
}
public void hello() {
System.out.println("Hello");
}
}
and here is my applicationContext-service.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:context="[[http://www.springframework.org/schema/context]]"
xmlns:util="[[http://www.springframework.org/schema/util]]"
xmlns:p="[[http://www.springframework.org/schema/p]]"
xsi:schemaLocation="[[http://www.springframework.org/schema/beans
[[http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ]]
[[http://www.springframework.org/schema/aop ]]
[[http://www.springframework.org/schema/aop/spring-aop-2.0.xsd ]]
[[http://www.springframework.org/schema/context ]]
[[http://www.springframework.org/schema/context/spring-context-2.5.xsd]]">
<context:annotation-config />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- bean id="context"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="applicationContext-service.xml" />
</bean-->
<bean id="example" class="com.almas.Example" autowire="byName">
<constructor-arg value="applicationContext-service.xml" />
</bean>
</beans>
I am getting a NullPointerException
if I execute the SpringTrial
class. Also is there a way I can inject ApplicationContext into my class? I am running this code from eclipse 3.3.1 and version of Spring jars are 3.0.0.M3
Thanks, Almas