Hi,
I am a new user of spring annotations. I have my config 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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<!-- Add your classes base package here -->
<context:component-scan base-package="com.springaction.chapter01"/>
<bean id="myBean" class="com.springaction.chapter01.InjectBean">
<!-- <property name="id" value="123"></property>
<property name="name" value="Inject Bean Class"></property> -->
</bean>
<bean id="greeting" class="com.springaction.chapter01.GreetingImpl">
<property name="greeting">
<value>Naveen Jakad</value>
</property>
<!-- <property name="bean" ref="bean"></property> -->
</bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>
and d bean which I am using for injection is :-
package com.springaction.chapter01;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
@Service
public class InjectBean {
private int id;
private
String name;
public InjectBean() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Primary
public void init(){
id = 123;
name = "Testing Class";
}
}
and the class in which I am doing injection is :-
package com.springaction.chapter01;
import javax.annotation.Resource;
public class GreetingImpl implements Greeting {
private String greeting;
@Resource
private InjectBean myBean;
public GreetingImpl() {
super();
}
public GreetingImpl(String greeting) {
super();
this.greeting = greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
@Override
public void sayGreeting() {
System.out.println(greeting + " " + myBean);
}
}
But I don't succeed to do so and I get message "Naveen Jakad null", Please help me and tell me where I am wrong and why this dependency injection by annotation is not working.......