Spring MVC Injection
Dependency
Yet Another
Did SomeoneWeb
say:Framework
JNDI?
copyright 2009 Trainologic LTD
Spring MVC –Injection
Dependency part 1
• Introduction
What is Dependency Injection?
• Injection
The MVC Pattern
Types
• EJB
Spring
Timers
MVC Implementation
• Controllers
• Mapping Request Handlers
• Views
• I18N and Locale Resolvers
• Customizing Look & Feel using Themes
• File Uploading
• Error Handling
• Using Annotations copyright 2009 Trainologic LTD
Dependency Injection
Motivation
• Usually, EJBs need access to different types of
resources:
• DB connection pools.
• JMS resources.
• Environment entries.
• Timers.
• Etc…
3
copyright 2009 Trainologic LTD
Dependency Injection
Use of JNDI
• In previous versions these resources were acquired
using the JNDI API.
• Here is how it was done in EJB 2.1:
public class MyBean implements SessionBean{
private DataSource myDS;
public void ejbCreate()
{
try
{
// connects to the JNDI
Context ctx = new InitialContext();
// lookup the DataSource
myDS = (DataSource)ctx.lookup("jdbc/myDS");
} catch (NamingException e)
{ // ... handle the exception somehow }
}
4
copyright 2009 Trainologic LTD
Dependency Injection
JNDI Lookups
• Following are the disadvantages of JNDI lookups:
• Duplicate code.
• Can't unit-test (need JNDI service).
• Need to handle JNDI checked exceptions.
• Most projects use the “Service Locator” design
pattern to make JNDI lookups abstract.
52
5
copyright 2008 trainologic LTD
Dependency Injection
Enter Dependency Injection
• Dependency Injection is a new feature of EJB3.
• Obsoletes most of the JNDI calls.
• No more “Service Locator” design pattern.
• Eases unit-testing.
• OK, what is it?...
49
6
copyright 2008 trainologic LTD
Dependency Injection
No More “Service Locator”
• With EJB3 you can inject the dependencies directly into
the members.
• Use "push" approach instead of "pull" approach.
• Let’s see how it is done...
53
7
copyright 2008 trainologic LTD
Dependency Injection
@Resource
• The @Resource annotation specifies dependency
injection.
• It can be placed on a field like the following:
@Stateful
public class ShoppingCartBean implements ShoppingCart {
@Resource(name = "jdbc/productionDS”)
private DataSource myDS;
54
8
copyright 2008 trainologic LTD
Dependency Injection
@Resource
• Fields annotated with @Resource get their values
injected into them before any business method (and
before @PostConstruct).
• The field will be injected with the object from the JNDI
located by the name attribute.
• The name attribute of the annotation is optional and
defaults to the name of the field.
55
9
copyright 2008 trainologic LTD
Dependency Injection
Setter Methods
• Dependency injection can be specified on setter methods
instead of fields:
@Stateless
public class SomeBean {
@Resource // the lookup will be for productionDB
public void setProductionDB(DataSource ds1)
{
…
}
59
10
copyright 2008 trainologic LTD
Dependency Injection
• What is Dependency Injection?
• Injection Types
• EJB Timers
copyright 2009 Trainologic LTD
Dependency Injection
Injected Types
• The following types can be injected:
• Managed resources.
• Environment entries.
• EJBContext.
• Session Beans.
• UserTransaction.
• EntityManager.
• TimerService.
60
12
copyright 2008 trainologic LTD
Dependency Injection
Managed Resources
• All of the resources that the Application Server
managed, and which are stored in the JNDI can be
injected.
• This includes:
• DB connection pools.
• JMS connection factories.
• JMS destinations (queues & topics).
60
13
copyright 2008 trainologic LTD
Dependency Injection
Environment Entries
• An EJB may use a dedicated environment entries.
• These entries are per bean type.
• The type of the environment entry can be only one of
the following: String, Character, Integer, Boolean,
Double, Byte, Short, Long and Float.
• These entries are stored in a JNDI context which is
bean-specific and has the name of: java:comp/env.
• Let’s see an example…
60
14
copyright 2008 trainologic LTD
Dependency Injection
Example
@Stateless
public class SimpleSLSB implements Simple {
@Resource float vat;
@Override
public boolean getPriceWithVAT(String catalogNumber) {
return item.get(price) * (1.0f + vat);
}
60
15
copyright 2008 trainologic LTD
Dependency Injection
Setting Environment Entries
• Environment entries can be set in the deployment
descriptor (and thus customized at deployment).
• E.g.:
<enterprise-beans>
<session>
...
<ejb-name>Simple</ejb-name>
<ejb-class>com.trainologic.SimpleSLSB
</ejb-class>
...
<env-entry>
<description>The VAT</description>
<env-entry-name>vat</env-entry-name>
<env-entry-type>java.lang.Float</env-entry-type>
<env-entry-value>16.5</env-entry-value>
</env-entry>
60
16
copyright 2008 trainologic LTD
Dependency Injection
EJBContext
• Every EJB can communicate with the container (the
Application Server) through a special object called:
EJBConext.
• Session Beans use: SessionContext.
• Message Driven Beans use: MessageDrivenContext.
• We’ll discuss MDBs in a later chapter.
60
17
copyright 2008 trainologic LTD
Dependency Injection
EJBContext
• Using the EJBContext, the EJB can perform the following
operations:
• Use transactions API (discussed in the transactions
chapter).
• Retrieve the authenticated identity (discussed in the
security chapter).
• With Session Beans, the EJB can retrieve the
EJBObject associated with the bean (in order to pass
it to another bean).
60
18
copyright 2008 trainologic LTD
Dependency Injection
EJBContext Injection
• The @Resource annotation can be used to inject the
EJBConext:
@Resource
private SessionContext ctx;
56
19
copyright 2008 trainologic LTD
Dependency Injection
Injecting EJB References
• Dependency injection is also supported for EJB
references.
• In previous versions it was like with remote clients:
private StringCounter counter; // another EJB
public void ejbCreate() {
Context ctx = new InitialContext();
CounterHome home =
(CounterHome)ctx.lookup("ejb/otherEJBHome");
counter = home.create();
}
57
20
copyright 2008 trainologic LTD
Dependency Injection
@EJB
• Now, you should use the @EJB annotation:
@EJB(name=“StringCounterBean/local")
private StringCounter counter; // another EJB
58
21
copyright 2008 trainologic LTD
EJB 3.0
Persistence Context Injection
• Dependency injection is also used with the new
persistence framework (JPA).
• We will discuss it in the JPA chapter.
60
22
copyright 2008 trainologic LTD
Dependency Injection
• What is Dependency Injection?
• Injection Types
• EJB Timers
copyright 2009 Trainologic LTD
Dependency Injection
Scheduling
• Many applications need a scheduling infrastructure.
• E.g., perform a cleaning task every hour.
• EJBs support scheduling through EJB timers.
37
24
copyright 2008 trainologic LTD
Dependency Injection
EJB Timers
• Timers can be created for Stateless Session Beans and
Message Driven beans.
• The @Timeout annotation is used to declare a method
as the timer callback.
• The method must have the following signature:
void method-name (Timer)
• The method must not be final or static.
37
25
copyright 2008 trainologic LTD
Dependency Injection
Example
@Stateless
public class SomeBean implements SomeInterface {
@Resource
private TimerService timerService; // Dependency Injection
// a business method
public void createBeep() {
// start after 5 secs, then every 5 secs.
// The third parameter is a Serializable info object
timerService.createTimer(5000, 5000, null);
}
@Timeout
public void beep(Timer timer) {
System.out.println("BEEP!");
}
}
38
26
copyright 2008 trainologic LTD