views:

376

answers:

2

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]]"&gt;   

 <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

+1  A: 

@ContextConfiguration is part of Spring's JUnit support. It won't work if you just instantiate the class manually via a main method.

I suggest reading up on this part of the Spring docs.

skaffman
+1  A: 

There are several odd things about your code:

  • you are creating your class via a constructor in your main method. This circumvents Spring's management of your class and thus nothing will be injected
  • your injected dependencies are package private; that is considered bad practice; I would create a public setter method and make the var private or inject via the constructor
  • Your configuration looks odd; you are injecting what seems to be a Spring configuration file name into the constructor of your class (however that class doesn't have such a constructor).
  • You also don't have SpringTrial configured at all (another reason it won't get injected with dependencies)

I would suggest starting with things wired explicitly via XML configuration to make sure you have your classes are designed correctly and things are generally working. Explicit configuration can be painful on large projects, but it's much more predictable when you are just starting out.

Then, add annotation-based configurations a step at a time; this way if something blows up, you'll know why and be more easily able to diagnose it.

davetron5000