Java Reference
In-Depth Information
In addition to Java EE technologies, there's one framework that's widely used through-
out enterprise programming: the Spring dependency injection framework.
11.2.4
Moving to Blueprint from the Spring Framework
The Spring Framework is a large ecosystem of projects, and there are topics covering
the wide variety of features available. Although there are powerful aspect-oriented
programming tools and Web MVC helpers, the majority of Spring applications make
significant use of Spring's dependency injection, but are much less reliant on the
other Spring projects. A typical Spring application might contain configuration simi-
lar to the following listing.
Listing 11.3
A simple Spring application definition
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<jee:jndi-lookup id="dataSourceFromJNDI"
jndi-name="jdbc/MyDataSource"
cache="true"
lookup-on-startup="false"
proxy-interface="javax.sql.DataSource"/>
Find datasource
in JNDI
<bean id="myBusinessBean" class="fancyfoods.business.Sprin gBe an">
<property name="datasource">
<ref bean="dataSourceFromJNDI" />
</property>
</bean>
</beans>
The Spring XML in listing 11.3 is simple, but it demonstrates an important point.
Because of Blueprint's heritage, it's extremely easy to turn Spring XML into valid Blue-
print XML . This can often be accomplished with few changes. If you were to write a
roughly equivalent Blueprint version of the Spring example, it would look something
like the following listing.
Inject beans into
business logic
Listing 11.4
A simple Blueprint application definition
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
Use Service
Registry, not JNDI
<reference id="dataSourceFromJNDI"
filter="(osgi.jndi.service.name=jdbc/MyDataSource)"
interface="javax.sql.DataSource"/>
<bean id="myBusinessBean" class="fancyfoods.business.SpringBean" >
<property name="datasource">
<ref bean="dataSourceFromJNDI" />
Injecting beans
is the same
 
Search WWH ::




Custom Search