Spring3 and Annotation

本文介绍如何使用Spring3及注解实现数据访问层(DAO)和业务逻辑层(Manager)的开发。通过具体示例代码展示PersonDAOImpl和PersonManagerImpl类中注解的应用,包括@Autowired、@Qualifier和@Repository等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring3 and Annotation

I use xml file to define the spring bean. Right now, I try to use annotation to do that.

For the DAO java class, PersonDAOImpl.java:
package com.sillycat.easyjpa.dao;

import java.util.List;

import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.stereotype.Repository;

import com.sillycat.easyjpa.model.Person;

@Repository
@Qualifier("personDAO")
public class PersonDAOImpl extends JpaDaoSupport implements PersonDAO
{

@Autowired
@Required
public void setJpaEntityManagerFactory(
@Qualifier("entityManagerFactory")EntityManagerFactory entityManagerFactory) {
super.setEntityManagerFactory(entityManagerFactory);
}

public void delete(Integer id)
{
Person person = getJpaTemplate().find(Person.class, id);
if (person != null)
{
getJpaTemplate().remove(person);
}
}

public Person get(Integer id)
{
return getJpaTemplate().find(Person.class, id);
}

@SuppressWarnings("unchecked")
public List<Person> getAll()
{
List<Person> list = getJpaTemplate().find("select o from Person o order by o.id asc");
return list;
}

public void insert(Person person)
{
getJpaTemplate().persist(person);
}

public void update(Person person)
{
getJpaTemplate().merge(person);
}

public void updateName(String name, Integer id)
{
Person person = getJpaTemplate().find(Person.class, id);
if (person != null)
{
person.setName(name);
}
}

}

For the Manager layer, PersonManagerImpl.java:
package com.sillycat.easyjpa.manager;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.sillycat.core.webservice.interfaces.UserService;
import com.sillycat.core.webservice.model.IUser;
import com.sillycat.easyjpa.dao.PersonDAO;
import com.sillycat.easyjpa.model.Person;

@Service
@Qualifier("personManager")
public class PersonManagerImpl implements PersonManager
{

@Autowired
@Qualifier("personDAO")
PersonDAO personDAO;

// @Autowired
// @Qualifier("userServiceLocal")
// UserServiceLocal userServiceLocal;

@Autowired
@Qualifier("userServiceRemote")
UserService userServiceRemote;

public List<Person> getAll()
{
return personDAO.getAll();
}

public Person get(Integer id)
{

// IUser iuser = userServiceLocal.get(Integer.valueOf(1));
IUser iuser = userServiceRemote.get(Integer.valueOf(1));
Person p = personDAO.get(id);
p.setName(iuser.getEmail());
return p;
}

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);
}

}

We can test them like this:
package com.sillycat.easyjpa.manager;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sillycat.easyjpa.model.Person;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public class PersonManagerTest
{

@Autowired
@Qualifier("personManager")
private PersonManager manager;

private Person item;

@Before
public void setUp() throws Exception
{
item = getItem();
}

@After
public void tearDown() throws Exception
{
if (item != null && item.getId() != null)
{
manager.delete(item.getId());
}
}

@Test
public void dummy()
{
assertTrue(true);
}

@Test
public void save()
{
manager.save(item);
assertNotNull(item);
assertNotNull(item.getId());
}

@Test
public void getAll()
{
manager.save(item);
assertNotNull(item);
assertNotNull(item.getId());
List<Person> list = manager.getAll();
assertNotNull(list);
assertTrue(list.size() > 0);
}

@Test
public void get()
{
manager.save(item);
assertNotNull(item);
assertNotNull(item.getId());
Person t = manager.get(item.getId());
assertNotNull(t);
assertEquals(t.getId(), item.getId());
}

@Test
public void updateName()
{
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.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;
}

}

There will be this in our test-context.xml file:
<context:annotation-config />
<context:component-scan base-package="com.sillycat">
</context:component-scan>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值