aspose file tools*
The moose likes Other Application Frameworks and the fly likes injection and lookup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "injection and lookup" Watch "injection and lookup" New topic
Author

injection and lookup

Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
hi..
Any explain to me what is difference between injection and lookup?
kurt hanni
Ranch Hand

Joined: Aug 11, 2007
Posts: 140

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.

SCJA, SCJP, SCJD, SCMAD, SCWCD, SCBCD, SCDJWS, SCEA, SOA, Spring Certified, OCP MySQL 5 Developer, PMP
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

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 )

Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
kurt hanni
Ranch Hand

Joined: Aug 11, 2007
Posts: 140

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

Joined: Apr 07, 2009
Posts: 489

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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
wood burning stoves
 
subject: injection and lookup