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
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
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);
helloWorldService.setName("Spring");
message = helloWorldService.sayHello();
System.out.println(message);
}
public static void main(String[] args) {
SayHello s1 = new SayHello();
s1.test();
}
}
applicationContext.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" })