Tags

, , ,

Spring JavaConfig introduces a means of configuring the Spring Container using pure-java, i.e., without XML. I believe through the Java Config approach we got a better control on creation and initialization of beans. https://github.com/mrtong/FlightReservation.git contains all the code we will discuss in the rest of the post.
How to replace the applicationContext.xml?
Usually we put bean definitions in this file. A bean definition looks like:
<bean id=”flightDao” class=”com.foo.FlightDaoImpl”/>
Now things have changed we can do it in this way:
1. We define a class with @Configuration annotation which tells the system this is a kind of applicationContext.xml class.
2. Use the @Bean annotation to define a bean
Below is a sample

@Configuration
@EnableTransactionManagement
public class DefaultDaoConfig implements DaoConfig, TransactionManagementConfigurer, InitializingBean {

@Bean
@Override
public FlightDao getFlightDao() {
return new FlightDaoImpl(getSessionFactory());
}

}

We need to register those configure files in the system

In web.xml we have the following section

<servlet>
<servlet-name>springExample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>

<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.foo.flight.web.WebConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

This tells Spring WebConfig is the entry point for all the config files.

In the WebConfig.java we have:

@EnableWebMvc
@Configuration
@Import({ ControllerConfig.class, DefaultServiceConfig.class, DefaultDaoConfig.class })
public class WebConfig extends WebMvcConfigurerAdapter {

…….

}

As you may have known, in Spring, we have two kinds of transaction, one is declarative the other one the programmatic. I do not want to write any code regarding transaction thus in this sample we use the declarative mode.

How to add transactions in your Spring code?

The @EnableTransactionManagement annotation (which I mentioned in code snippet 1) enables Spring’s annotation driven transaction management capabilities for services annotated with @Transactional.

You can find the following code snippet in the ReservationServiceImpl.java

@Transactional(rollbackFor = { NoSeatAvailableException.class }, readOnly=false)
public Ticket bookFlight(Reservation booking) throws NoSeatAvailableException {

}

It is quite to implement REST in the Spring Framework. The following snippet can be found in ReservationController.java file.  The getReservations returns an XML or JSON representation of the reservations.

@RequestMapping(value = “/reservations”, method = RequestMethod.GET, produces = {
MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public Reservations getReservations() {
return new Reservations(reservationService.getReservations());
}

A known bug within Maven. 

When I tried to integrate everything into a war file and put it into my Tomcat server, it raised the following exception.

NoSuchMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;

Oooooops! After investigating, I found there must be some conflict between jsp-api-2.0.jar and Tomcat(I tried it later in JBoss 5.0, unfortunately, it has this problem as well). Furthermore I do not know why this jar file was automatically added into my war file! The temp solution is, removing that jar file every time you do the deployment. Or, which is my approach, adding the following into pom.xml

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>

Done!

===

Summary, in this post I demonstrated the Java Config feature of Spring. The whole code can be found in my git which is  https://github.com/mrtong/FlightReservation.git

Advertisements