• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

dependency injection by annotations

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.......
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
!) You over complicated it.
2) You have two beans of the same type, autowiring by default autowires uses by type, and works when it finds one and only one of the type to inject. You have ambiguity. Two of the same type. When you have two of the same type you have to fix the ambiquity with either putting the bean name in the @Resource annotation, or putting @Primary on the class you want injected when there is ambiguity of that type. I noticed you have @Primary on an init method. While @Primary is allowed on a method, it isn't the normal way to use it. If you look at the javadoc for @Primary it only talks about using it on a class and how it works.

Here is a simpler solution for you, All the xml is inside the >beans> tag which I left out here so I diodn't have to type all of it.



Try that.

Mark
 
Naveen Jakad
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
Thanks for reply...... I made changes according to your above code but I am still getting same output......Please help me out... and make me clear in this thing that I have give the beans to be injected path <context:component-scan base-package="com.springaction.chapter01"/> in beans .xml now I don't need to define any of the bean individually in beans.xml because system will take care of all beans defined in this package and now what is the need of "Resource" annotation infact I must use "@Autowired" ......


Mark I tried with both means "@Resource" as well as "@Annotation" but both din't work for me.........Thanks in advance...
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naveen Jakad wrote:Hi Mark,
Thanks for reply...... I made changes according to your above code but I am still getting same output......Please help me out... and make me clear in this thing that I have give the beans to be injected path <context:component-scan base-package="com.springaction.chapter01"/> in beans .xml now I don't need to define any of the bean individually in beans.xml because system will take care of all beans defined in this package and now what is the need of "Resource" annotation infact I must use "@Autowired" ......


Mark I tried with both means "@Resource" as well as "@Annotation" but both din't work for me.........Thanks in advance...



There is no @Annotation.

SPring also supports @Resource from JSR-250. As well as their own @Autowired and @Inject from JSR-330

Personally, I always just use @Autowired instead.

@Resource and @Autowired mean the exact same thing in Spring, they are interchangeable.

Here is a simpler example, just make sure the beans tag has all the namespaces needed







Spring will scan for @Component, @Service and a couple other annotations on the classes in the com.mark package. And when it sees it, it makes those classes as beans. Then later on when doing dependency injection Spring will see @Autowired and inject MyClass into OtherClass.

Good Luck

Mark
 
Naveen Jakad
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Mark,
I was about to write "@Autowired" but by mistake in hurry I wrote "@Annontation".
I did same changes as you asked to do so but still my system is not giving the desired output.
Hey tell me one thing that according to you what should be expected output? Please help me out....I have to fix it ASAP ...as I have to complete my tasks......
Thanks in advance...
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my example. Spring will instantiate the two objects and inject one object instance into the other. That is how it has always worked for me. That is how the Spring INitialization phase works. If you want more information on it, you can read the Spring documentation, but I also think in Spring In Action they talk about it too.

There is something else about what you are doing that you aren't posting here. The re-write that I posted works. There is no trick involved. So you must have something else wrong in your example, you classpath or something.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never posted the code that you use to create the Spring ApplicationContext, how you get the bean and call the method to print that stuff out. That could be where your problem is.

Remember to use the CODE tag to keep you code formatted when posting. The CODE button is right above where you are typing your reply.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic