Monday, February 6, 2012

Project 2 Beans

In this tutorial, all the beans discussed are Spring beans.  We aren't using EJB, CDI, Managed, or whatever.  We have different types of spring beans, but they are still all Spring.  During the first tutorial, even though we were doing dependency injection, we didn't really discuss design patterns and or the use of interfaces.  Let us take a moment to understand how this is all "supposed" to work.

First the use of interfaces:  Dependency injection or inversion of control allows us to uncouple our bean from other classes, why then would we couple the bean to other classes by having them rely on specific classes? i.e.  Given that PartDaoClass implements PartDaoInterface.  We define our class by:
Class PartService {
    PartDaoInterface dao;
}
instead of:
Class PartService {
     PartDaoClass dao;
}

We inject PartDaoClass to field dao in PartService.  This gives us the "write to interface" approach and allows us to inject the specific class we need for our environment.  i.e. in testing we may inject PartDaoTestClass into PartService.

In Spring, we take a layered approach for development.  We will start with our Entity, access it with a Dao, access the Dao with a Service, access the Service with a Controller, access the Controller with the View.

Because we are injecting to interfaces, you will see an interface for each implemented bean.  Although Spring does NOT require this.

Let us review our ServiceAndDaoBeans.xml file, which is included in applicationContext.xml file.

ServiceAndDaoBeans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd">

    <bean id="partDao" class="com.sample.dao.PartDaoImp">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <bean id="partService" class="com.sample.services.PartServiceImp">
        <property name="dao" ref="partDao" />
    </bean>
     <bean id="orderPartsDao" class="com.sample.dao.OrderPartsDaoImp">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <bean id="orderPartsService" class="com.sample.services.OrderPartsServiceImp">
        <property name="dao" ref="orderPartsDao" />
    </bean>
    <bean id="orderDao" class="com.sample.dao.OrderDaoImp">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <bean id="orderService" class="com.sample.services.OrderServiceImp">
        <property name="dao" ref="orderDao" />
        <property name="partDao" ref="partDao" />
    </bean>
</beans>

We first notice that we have defined 8 beans.  Without overriding the defaults, the name given via the id will be the name of the field we inject into with the @Autowire or @Inject annotations.  We see in the OrderServiceImp class, we have created an instance called orderService and injected the bean orderDao into the field called dao.  We also inject the bean partDao into the field named partDao.  Based on the beans defined in the applicationContext.xml file, we can also inject an EntityManagerFactory, called entityManagerFactory into the bean partDao.

No comments:

Post a Comment