Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had been using spring MVC and web application. but now i am using simple application using spring. I am trying to replace all that component scan concept using @Resource annotation. I am pasting my code below. Please help to correct it

HelloWorldService.java

Java
package com.gide.iis.bls;

public class HelloWorldService {
 
	private String name;
 
	public void setName(String name) {
		this.name = name;
	}
 
	public String sayHello() {
		return "Hello! " + name;
	}
}


SayHello.java
Java
package com.gide.iis.bls_main;

import javax.annotation.Resource;

import org.springframework.test.context.ContextConfiguration;

import com.gide.iis.bls.HelloWorldService;

@ContextConfiguration(locations ={ "/META-INF/spring/applicationContext.xml" })
public class SayHello {
	 @Resource(name="helloWorldService")
	 private HelloWorldService helloWorldService;
	 
	 public void test(){
		 String message = helloWorldService.sayHello();
			System.out.println(message);
			 
			//set a new name
			helloWorldService.setName("Spring");
			message = helloWorldService.sayHello();
			System.out.println(message);
	 }
	 
	 public static void main(String[] args) {
		SayHello s1 = new SayHello();
		s1.test();

	}

}



applicationContext.xml
XML
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
 
	<context:component-scan base-package="com.gide.iis.bls" />
	 <context:annotation-config/>
 
	<bean id="helloWorldService"
		class="com.gide.iis.bls.HelloWorldService">
		<property name="name" value="Program Creek Readers" />
	</bean>
</beans>



i have applicationContext in src/main/resource/META-INF/spring/. The above code working fine with ApplicationContext in SayHello.java but when trying to replace with @Resource and @ContextConfiguration. it showing following error
Exception in thread "main" java.lang.NullPointerException
at com.gide.iis.bls_main.SayHello.test(SayHello.java:15)
at com.gide.iis.bls_main.SayHello.main(SayHello.java:30)

i know its not able to read that bean. How to make run

What I have tried:

I have tried giving also @ContextConfiguration(locations ={ "classpath:/META-INF/spring/applicationContext.xml" })
Posted
Updated 16-May-16 17:34pm
Comments
Nandlalaji Singh 16-May-16 3:25am    
Can some one give comment over it

1 solution

 
Share this answer
 
Comments
Patrice T 18-May-16 15:00pm    
Since you found a solution,
Use Accept answer to close the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900