public void setAge(int age) {
this.age = age;
}
public String toString() {
return
"Name: " + name + "\n"
+ "Age: " + age;
}
}
As you can see, the main() method of the SimpleBean class grabs both the inheritChild and
inheritParent beans from the ApplicationContext and writes the contents of their properties to stdout.
Here is the output from this example:
Parent:
Name: Clarence Ho
Age: 22
Child:
Name: Clarence Ho
Age: 35
As expected, the inheritChild bean inherited the value for its name property from the inheritParent
bean but was able to provide its own value for the age property.
Child beans inherit both constructor arguments and property values from the parent beans, so you
can use both styles of injection with bean inheritance. This level of flexibility makes bean inheritance a
powerful tool for building applications with more than a handful of bean definitions. If you are declaring
a lot of beans of the same value with shared property values, then avoid the temptation to use copy and
paste to share the values; instead, set up an inheritance hierarchy in your configuration.
When you are using inheritance, remember that bean inheritance does not have to match a Java
inheritance hierarchy. It is perfectly acceptable to use bean inheritance on five beans of the same type.
Think of bean inheritance as more like a templating feature than an inheritance feature. Be aware,
however, that if you are changing the type of the child bean, then that type must be compatible with the
type of the parent bean.
Summary
In this chapter, we covered a lot of ground with both the Spring core and IoC in general. We showed you
examples of the different types of IoC and presented a discussion of the pros and cons of using each
mechanism in your applications. We looked at which IoC mechanisms Spring provides and when and
when not to use each within your applications. While exploring IoC, we introduced the Spring
BeanFactory, which is the core component for Spring's IoC capabilities, and then the
ApplicationContext, which extends BeanFactory and provide additional functionalities. For
ApplicationContext, we focused on the GenericXmlApplicationContext that allows external
configuration of Spring using XML. Another method to declare DI requirements for ApplicationContext,
that is, using Java annotations, was also discussed.
This chapter also introduced you to the basics of Spring's IoC feature set including Setter Injection,
Constructor Injection, Method Injection, autowiring, and bean inheritance. In the discussion of
configuration, we demonstrated how you can configure your bean properties with a wide variety of
different values, including other beans, using both XML and annotation type configurations and the
GenericXmlApplicationContext.
This chapter only scratched the surface of Spring and Spring's IoC container. In the next chapter, we
look at some IoC-related features specific to Spring, and we take a more detailed look at other
functionality available in the Spring core.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home