0

I've started to read a book about Spring and I keep asking myself one question:

What is the difference between defining beans using Spring Expression Language in spring's XML file and the normal way in Java class. For example we have two equivalent implementations:

<bean id="carl" class="springinaction.springidol.Instrumentalist" >
<property name="song" value="#{kenny.song}" /> 
</bean>

and

Instrumentalist carl = new Instrumentalist();
carl.setSong(kenny.getSong());

When and why should I use one way instead of the other one?

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
Piotr Sagalara
  • 2,247
  • 3
  • 22
  • 25
  • possible duplicate of [What is dependency injection?](http://stackoverflow.com/questions/130794/what-is-dependency-injection) – andyb Feb 19 '14 at 20:02
  • You haven't provided any context. That `` element is defining a new bean called `carl` of type `Instrumentalist` with a property name `song` that is referring to a property of another bean called `kenny`. This bean only has any meaning with an `ApplicationContext`. The Java code you showed out of context doesn't explain anything. You're just creating an object and invoking one of its methods, passing in the value of the return value from invoking another method on another object. – Sotirios Delimanolis Feb 19 '14 at 20:32

2 Answers2

1
<bean id="carl" class="springinaction.springidol.Instrumentalist" >
<property name="song" value="#{kenny.song}" /> 
</bean>

This code, included on the proper Spring configuration file, will produce a Spring managed object. As by default the scope of a Spring bean is a singleton, there'll be only one instance, that may be accessed using the method ApplicationContext#getBean.

Instrumentalist carl = new Instrumentalist();
carl.setSong(kenny.getSong());

This code, included on the proper Java method, will create an instance on the JVM heap, that may be accessed through the variable carl. The instance will exist as long as there are variables that reference it. Then it'll be electable for garbage collection.

Andres
  • 10,561
  • 4
  • 45
  • 63
0

If you're asking about Dependency Injection, you should read this

To summarize Dependency injection, it is a pattern for shifting around type dependencies. Instead of having your dependencies directly in your application classes, you move them to a component that manages them. This component is usually known as an Inversion of Control container, and that's what Spring provides primarily. You can do this with XML or you can do it with Java. In other words, though you are shifting the dependencies, they can be defined at run-time (with XML) or at compile-time (with Java).

If you're asking about why to configure your context with XML vs Java, then one of the answers is that you don't have to recompile your source code to change configurations if you're using XML. chrylis also brings up some good points. XML is not type safe (but I've never had a problem with that).

However, there are some configurations that are hard to do with XML. The XML schemas that Spring declares are restrictive and force you to use some coding conventions. With Java configuration, you don't have those restrictions. You can create your objects any way you want.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • But you still do generally have to rebuild your packaging, and it's not type-safe. – chrylis -cautiouslyoptimistic- Feb 19 '14 at 18:59
  • @chrylis I meant in the sense of choosing your bean class. In XML, you would simply change the bean `class` property and any `` fields. In Java, you'd have to recompile the whole configuration class(es). – Sotirios Delimanolis Feb 19 '14 at 19:02
  • You consider giving the address of a tutorial a proper answer? – Andres Feb 19 '14 at 19:16
  • Right, but recompiling really isn't a big deal with modern development practices, especially when the distinction is between repackaging with or without recompiling a few classes. – chrylis -cautiouslyoptimistic- Feb 19 '14 at 19:43
  • @Andres You're right. I've summarized my understanding of Dependency Injection in an edit. Please take a look. – Sotirios Delimanolis Feb 19 '14 at 20:27
  • @chrylis Does it then become a matter of preference? Those type safety issues can be corrected during the development life cycle as well. – Sotirios Delimanolis Feb 19 '14 at 20:33
  • @SotiriosDelimanolis I think it really is, and for right now I see significant chunks of work that are easier in one or the other (Java objects that need customization, XML namespace plugins like `sec` that still do lots of magic under the hood). Some of the Spring Boot autoconfig is really starting to pull toward Javaconfig, I think. – chrylis -cautiouslyoptimistic- Feb 19 '14 at 22:17