Dependency Injection with AspectJ and Spring 48
An anonymous reader writes "IBM DeveloperWorks has an interesting article about the complementary aspects of dependency injection and aspect-oriented programming. Adrian Colyer, Chief scientist of Interface21, examines how to combine these two techniques to 'facilitate advanced dependency injection scenarios.' From the article: 'Dependency injection and aspect-oriented programming (AOP) are two key technologies that help to simplify and purify domain models and application layers in enterprise applications. Dependency injection encapsulates the details of resource and collaborator discovery, and aspects can (among other things) encapsulate the details of middleware service invocations'"
Eh, wot? (Score:1)
Good News! (Score:5, Funny)
My brain hurts... (Score:5, Funny)
Re:My brain hurts... (Score:2)
Although I do agree that whoever wrote the article could have used easier words. You have to already be familiar with Dependency Injection (or IoP) and AoP to actually understand. Which sorta voids the purpose of the article.
Re:My brain hurts... (Score:5, Informative)
A better article to start with is: Inversion of Control Containers and the Dependency Injection pattern [martinfowler.com], by Martin Fowler. It's older than the one mentioned in the story, but still relevant.
Summarizing the summary (Score:2)
It's a complicated way of getting around some problems caused by Java; problems which don't occur in dynamic languages such as Ruby.
Re:Summarizing the summary (Score:2)
It's a complicated way of getting around some problems caused by Java; problems which don't occur in dynamic languages such as Ruby.
Please explain
angel'o'sphere
Re:Summarizing the summary (Score:3, Informative)
Take the example given. He defines an API for communications between the application and something that looks up movies for a given director, which he calls the finder.
In Java, he needs to instantiate a particular concrete class to provide the finder service. Because of Java's insistence on compile-time type safety, this presents a problem. He wants to be able to plug in a ColonDelimitedMovieFinder class as his finder initially, but still allow someone else to plug in a SQLMovieFinder or whatever without
Re:Summarizing the summary (Score:4, Interesting)
Dependency injection has NOTHING to do w/ weak or strict type-safety (and, in fact, a concrete class is not required at all.. reflective interface handlers often can facilitate service needs). And if you use dyna-beans, then neither the producer or consumer ever even need an interface.
Your example shows the exact flaw in coding that Inversion of Control (through setter/constructor injection) solves.
Namely that you explicitly had to configure your dependencies. Yes, due to weak-types in such languages you could take almost any arbitrary code and plug it in to fullfill the server.. The fact still remains your code explicitly configured it.
The idea of Inversion of Control is that the thing that needs a service should NEVER configure that service. It's the law of demiter. If I want to use some thing, I shouldn't need to know that it in turn uses something else. If I have to configure both the thing that I want to use AND the thing that it wants to use, then eventually all my code is convoluted (doing and configuring crap that has nothing to do with what I'm actually trying to do).
If you simply take a step back and have something that uses the service configure the service, you'd still have to point some code to the thing that configured the service, so you're just moving the problem around (which is what your example does).
Inversion of control allows you to define the entire application modularly where each module only cares about capabilities of external modules (not their configurations nor their nested dependencies). In your example, the type-weak language merely requires the positional arguments to be compatible (which, I agree is a very powerful thing.. Perl's "grep", "map" and "sort" commands are incredible). In java, it would require an explicit class (interface or conctrete).
When you build your application this way, and you generalize your inter-component communication as genericly as possible (yes perl,ruby,python can much more easily genralize a service), then you can plug pieces together that were never originally meant to work together.
Another very important aspect of IoC is that you can easily test each piece separately, as instead of an application-confired container, you have a testing-configured container which performs appropriate configurations suitable for testing and unit-isolation.
With setter and constructor injection and through the use of interfaces, you facilitate mocking of 3'rd party services such that you completely skip all the complexity of side components and focuses very specificly on what a single component is doing.. You can test 'what would this module do if it's dependency returned something unexpected'.
If you have a perl module that internally uses some other class, how could you easily test interaction with the other class (say a IO::File). Typically you create test files. But what about when it's a database or a web site.. Your unit tests get more and more complicated. Especially if what the module is doing has is really independent of the fact that it's using a URL or database.
If instead the module was fully configured externally (no default configuration w/ zero-arg constructors or default package global variables), then you would be assured that you have full control over the environment of the module in question.
Thanks to these types of interactions, the power of object-orientation can be exploited while at the same time using singletons (essentially context-specific global variables; an otherwise anti-pattern). This has a trade-off of faster execution time at the cost of a higher memory usage and slower startup time. The fact that IoC usually has a non-neglegable setup time often means that we lean towards singletons (which has implications on code design).
Finally, the code you showed is fine for alternating configurations.. And for small tasks, the overhead of IoC is simply not justified. IoC really comes into it's own in very large p
Re:Summarizing the summary (Score:2)
In that case, the article cited was incredibly badly written. What's more, I fail to see how the example given in Java achieves the supposed goal. How does that Spring XML file get written, if not by whoever is using the service code?
Re:Summarizing the summary (Score:2)
But the idea is that in spring/pico/nanno/etc the configuration description is a deployment issue, not an application issue.. It's like editing
Re:Summarizing the summary (Score:1)
Demeter shmemeter, I just want to code! (Score:2)
In Java Swing, from a JFrame object, you say
myWidget = new MyWidget(this);
getContentPane().add(myWidget, BorderLayout.CENTER);
This is a blatent Demeter violation now, isn't it? Or is it only a Demeter violation of either the strong or the weak form of Demeter depending on whether invoking the function getContentPane() to return an internal object of the JFrame from a derived class is co
Re:Demeter shmemeter, I just want to code! (Score:2)
From that, most of my code is layered separately between UI and application. In fact all HTML generation comes from JSP pages.. And even more, most of the HTML in that is segregated stle-sheets + JSP2.0-tag files (not to be confused w/ jsp-includes). And further, the basic framework of a JSP page is built from a sitemesh template (opensymphony.org). which means the content jsp page l
Re:Summarizing the summary (Score:2)
Spring allows Java to inject code.
Ruby has inate support for injecting code.
class Hash
def to_s
return 'hello';
end
end
You can override any method from any point in the code. This can be your best friend or worst enemy. It will, however, make dependency injection trivial.
Re:Summarizing the summary (Score:2)
but thats not right.
finder = CSV::Reader.parse(File.open('movies.csv')) this line is a dependency. You obviously depend on CSV:: and you have hardcoded the dependency here.
IoC or Dependency Injection is about how to avoid hardcoding dependencies between components.
In IoC frameworks you usually use an external description file, e.g. an XML file (deployment descriptor) or an AOP code snipped.
A generator or a runtime component would read that fiel and insert your line: finder = CSV::Reader.parse(File.op
Re:Summarizing the summary (Score:2)
chosenclass = File.open("config.txt").gets.chop
finder = chosenclass.parse("movies.csv")
The example *is* reusable. Those lines you pick out aren't part of the class library, they're an example of how you can call it with whatever class you like (even one chosen from a config file), without needing any kind of complicated scaffolding like in Java. I just put them in to demonstrate that the code actually works.
T
Re:Summarizing the summary (Score:2)
I simply don't understand which point you want to make
And as the core question was: Why is IoC easyer in Python than in Java? You failed IMHO to answer that question.
In Java your code would only look like this:
SomeType chosenclass = File.open("config.txt").gets.chop;
SomeOtherType finder = chosenclass.parse("movies.csv");
Voila! Exactly the same simplicity like in Python, except that we add
Re:Summarizing the summary (Score:2)
Re:Summarizing the summary (Score:2)
you have several classes that work together. Usually one or mroe of those classes would instanciate each otehr to get a net of objects. So at least one of them has the complete construction process of all objects involved in on of their constructors.
If a new class is ment to reuse this network, it has again a construction statment for the root class of the used network.
The more classes you have the ore are constructions, calls to new, clutterd as hardcoded dependencies all over you
Re:Summarizing the summary (Score:1)
Re:My brain hurts... (Score:3, Funny)
More or less. Essentially the author is saying:
"Buy My Book....Buy My Book...."
Re:My brain hurts... (Score:2)
Re:My brain hurts... (Score:2, Informative)
Re:My brain hurts... (Score:1)
I guess the conclusion (which is the one I also draw about operator overl
Re:My brain hurts... (Score:2)
Re:My brain hurts... (Score:2)
Re:My brain hurts... (Score:1)
I know this answer provides no value. I was guessing from the tone of your question that you don't really care to have it answered anyway.
Re:My brain hurts... (Score:2)
Re:My brain hurts... (Score:1)
"Dependency Injection" doesn't really care about that. What it's concerned with is how particular components locate the other components they have to interact with.
Possibilities: hard-coded instantiation -
Foo foo = new Foo();
the same kind of thing, but using reflection
Foo foo = (Foo) Class.forName(fooClass).ne
Re:My brain hurts... (Score:2)
So, it's exactly like what I've been doing with my object oriented approach to C coding, that doesn't make sense in OO languages because the syntax makes you lazily pick the non-DI way...
Re:My brain hurts... (Score:2)
Re:My brain hurts... (Score:1)
More AOP articles wanted (Score:1)
Spring (Score:2, Insightful)
I'd rather use RoR or any other dynamic language for web dev in general. However, there are still many business requirements that can come up (OLAP for one - Mondrian) that keep me coming back to j
film star? (Score:1, Troll)
one good project from this? (Score:3, Insightful)
Aside from more jargonism, can anyone cite one major application, one major project for which this was used?
I'm signing up for my 10-year training plan to learn the terminology, jargon, etc.
Then I'll consider if I even want to learn to apply it. Sheesh, this is embarrassing
Explanation... (Score:1)
OK, I think that it's fair to say that AOP is still very much at the cutting edge of developement in the java community.
Essentially the idea with AOP is that you increase the modularity and decrease the coupling of components in your application. An example of this would be Logging.
Now most (if not all) apps need to have logging code of one sort or another. This means that the code necessary to log to whichever system (commons-logging, log4j etc in the java world) you use tends to touch all of
Re:one good project from this? (Score:2)
Aside from more jargonism, can anyone cite one major application, one major project for which this was used?
Every major development uses either J2EE (JBoss, WebSpehre or what ever)
OR: Spring + Hybernate + AspectJ
Why don't you google for it
And yes, if you code in java you should get familiar with at least one of those.
angel'o'spehre
Re:one good project from this? (Score:2)
j2ee != jboss/websphere..
EJB == jboss/websphere.
j2ee incluedes servlets (tomcat), mail, encryption and transactions..
Then on the other extreme, you groups AOP w/ spring+hibernate.. evil evil man.
I find myself in the situation that when I move on to bigger and more profitable projects, my legacy code gets taken over by outsourced Indian groups, and the shere number of buzz words involved is enough to make their heads explode I should think.
Re:one good project from this? (Score:1)
Most of these applications aren't high-profile public projects. Typically, you're talking about internal business systems.
Overall, it's important to remember: Your belief in the value of a given technique has no effect on the actual value therein.