Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

injection and lookup

 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..
Any explain to me what is difference between injection and lookup?
 
kurt hanni
Ranch Hand
Posts: 140
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dependency Lookup is when your class is responsible for getting its dependencies from some lookup factory, (this is similar to a JNDI lookup).
for example ClassA has a ClassB,
in ClassA you will have a code something like this

while a Dependency Injection is when your class is given a dependency by some factory or framework
example ClassA has a ClassB,

you can see in Dependency Injection that the framework was responsible for injecting the instance of ClassB.
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i have any question about "DI/IoC"..

if iam using Spring..
is it DI make our application to use Singleton Object?..

and how to i make a Singleton Object in spring?..

example i have class : com.leonardo.action.SomeAction

how to configure ir in a spring context to be an singleton object?..
and how to use it in my application?..

CMIIW.
Thank's

Regards,
W
(Sorry my english isn't too good )
 
kurt hanni
Ranch Hand
Posts: 140
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leonardo, by default all objects created by spring are singleton, so you don't have do anything in your configuration.

If you want to see the configuration, you can specify it in the "scope" attribute of <bean> tag.

<bean id="hello" class="Hello" scope="singleton" />

you also specify in scope attribute if you want nonsingleton beans.

 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kurt hanni wrote:Hi Leonardo, by default all objects created by spring are singleton, so you don't have do anything in your configuration.

If you want to see the configuration, you can specify it in the "scope" attribute of <bean> tag.

<bean id="hello" class="Hello" scope="singleton" />

you also specify in scope attribute if you want nonsingleton beans.



Alright, thank's for your reply ..
now, how to get (use) bean which named "hello" in our application?..

is it just like this?

public class MyClass {
private Hello helo;

public void setHello(Hello hello){
this.hello=hello;
}

public doWork{
hello.print("-X-"); //example method
//any code
}
}

Thank's before
(sorry my english isn't too good)
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and no, I like the code of MyClass, the next part is how you tell Spring there is the dependency between MyClass and the Hello class.

You can define another bean of type MyClass in your configuration and use a <property> tag in that <bean> tag that points or "ref"s references your "hello" bean. That is if you want it defined in the xml.

The other choice is to use Annotations. the @Autowired (Spring annotation) or the @Resource (JSR-250 annotation) does the trick. But to use annotations you have to put in the xml file that you are using annotations.

Now, if MyClass is not going to be a Spring Bean, then in MyClass you need to get a reference to the ApplicationContext object and call the getBean(String beanName) method.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic