Datasheet

Understanding Spring’s Inversion of Control (IoC) Container
The Spring container is often referred to as an inversion of control (IoC) container. In standard component
containers, the components themselves ask the server for specific resources. Consider the following seg-
ment of code, which can frequently be found in components created for J2EE components:
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(“jdbc/WroxJDBCDS”);
Connection conn = ds.getConnection(“user”, “pass”);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery( “SELECT * FROM Cust” );
while( rs.next() )
System.out.println( rs.getString(1)) ;
rs.close() ;
stmt.close() ;
conn.close() ;
con.close();
This code obtains a JDBC (Java Database Connectivity) DataSource from the container using JNDI (Java
Naming and Directory Interface) lookup. It is the J2EE-sanctioned way of getting a JDBC connection, and
occurs very frequently in component coding. Control for the code is always with the component; in par-
ticular the component obtains the DataSource via the following steps:
1. Obtains a new JNDI InitialContext() from the container
Value of autowire Attribute Description
byType
The container examines the argument type of the setter methods
on the bean, and tries to locate a bean with the same type. An
error is raised when more than one bean with the same type
exists, or when there is no bean of the required type.
constructor
The container checks for a constructor with a typed argument,
and tries to find a bean with the same type as the argument to the
constructor and to wire that bean during a call to the constructor.
An error is raised when more than one bean with the required
type exists, or when there is no bean of the required type.
autodetect
The container performs either the constructor or byType setter
autowiring by examining the bean to be wired. It raises an error
if the bean cannot be autowired.
no
This is the default behavior when the autowire attribute is not
specified. The container does not attempt to wire the properties
automatically; they must be explicitly wired. This is desirable in
most cases if you want explicit documentation of the component
wiring.
16
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 16