Spring3 and JPA Integration(II)

Spring3 and JPA Integration(II)

My manager Layer, the interface is com.sillycat.easyjpa.manager.PersonManager
package com.sillycat.easyjpa.manager;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.sillycat.easyjpa.model.Person;
@Transactional
public interface PersonManager
{
    public List<Person> getAll();
    public Person get(Integer id);
    public void save(Person person);
    public void updateName(Person person);
    public void delete(Integer id);
}

The implementation of the interface is com.sillycat.easyjpa.manager.PersonManagerImpl
package com.sillycat.easyjpa.manager;

import java.util.List;

import com.sillycat.easyjpa.dao.PersonDAO;
import com.sillycat.easyjpa.model.Person;

public class PersonManagerImpl implements PersonManager
{
    PersonDAO personDAO;
    public List<Person> getAll()
    {
        return personDAO.getAll();
    }
    public Person get(Integer id)
    {
        return personDAO.get(id);
    }
    public void save(Person person)
    {
        if (person != null && person.getId() != null)
        {
            personDAO.update(person);
        }
        else
        {
            personDAO.insert(person);
        }
    }
    public void updateName(Person person)
    {
        if (person != null && person.getId() != null)
        {
            personDAO.updateName(person.getName(), person.getId());
        }
    }
    public void delete(Integer id)
    {
        personDAO.delete(id);
    }
    public void setPersonDAO(PersonDAO personDAO)
    {
        this.personDAO = personDAO;
    }
}

Nothing special, just the common spring bean. My manager layer configuration file is manager-context.xml:
<bean id="personManager" class="com.sillycat.easyjpa.manager.PersonManagerImpl" >
<property name="personDAO" ref="personDAO" />
</bean>

The transaction configuration file is core-context.xml:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
lazy-init="false">
<property name="locations">
<list>
<value>classpath*:config.properties
</value>
</list>
</property>
</bean>
<!-- transaction manager,user&manage entityManagerFactory -->
<bean id="transactionManager"
      class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory"
          ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

right now, we have transaction configuration, we can do full test.
com.sillycat.easyjpa.manager.PersonManagerTest:
package com.sillycat.easyjpa.manager;

import java.util.Date;
import java.util.List;
import com.sillycat.core.BaseTestCase;
import com.sillycat.easyjpa.model.Person;
public class PersonManagerTest extends BaseTestCase
{
    private PersonManager manager;
    private Person    item;
    public void setUp() throws Exception
    {
        super.setUp();
        manager = (PersonManager) ctx.getBean("personManager");
        item = this.getItem();
    }
    public void tearDown() throws Exception
    {
        super.tearDown();
        if (item != null && item.getId() != null)
        {
            manager.delete(item.getId());
        }
    }
    public void testSave()
    {
        manager.save(item);
        assertNotNull(item);
        assertNotNull(item.getId());
    }
    public void testGetAll()
    {
        manager.save(item);
        assertNotNull(item);
        assertNotNull(item.getId());
        List<Person> list = manager.getAll();
        assertNotNull(list);
        assertTrue(list.size() > 0);
    }
    public void testGet()
    {
        manager.save(item);
        assertNotNull(item);
        assertNotNull(item.getId());
        Person t = manager.get(item.getId());
        assertNotNull(t);
        assertEquals(t.getName(),item.getName());
        assertEquals(t.getId(),item.getId());
    }
    public void testUpdateName(){
        manager.save(item);
        assertNotNull(item);
        assertNotNull(item.getId());
        item.setName("haha");
        item.setSex(false);
        manager.updateName(item);
        Person t = manager.get(item.getId());
        assertNotNull(t);
        assertEquals(t.getName(),"haha");
        assertEquals(t.getSex(),true);
    }
    private Person getItem()
    {
        Person t = new Person();
        t.setAge((short) 1);
        t.setBirthday(new Date());
        t.setName("testName");
        t.setSex(true);
        return t;
    }
}

And I build this jar to the JBOSS_HOME/server/default/lib, my build.xml is as follow:
<project name="${app.name}" default="all" xmlns:ivy="antlib:org.apache.ivy.ant">
<!-- some variables used -->
<property file="build.properties" />
<property name%

你可能感兴趣的:(spring,bean,xml,jboss,jpa)