我需要为一个简单的spring项目设计我的应用程序上下文文件,如何放置属性和ref?

时间:2016-08-10 09:02:22

标签: java spring applicationcontext

以下是我的虚拟对象,服务,DAO和控制器的java代码

public class Person implements Serializable{

    private int personId;
    private String personName;

    /** default constructor */
    public Person() {
    }

    /** minimal constructor */
    public Person(int personId) {
        this.personId= personId;
    }

    /** full constructor */
    public Person(int personId, String personName) {
        this.personId= personId;
        this.personName= personName;
    }       

    // Property accessors   

    public int getPersonId() {
        return personId;
    }    
    public void setPersonId(int personId) {
        this.personId = personId;
    }

    public String getPersonName() {
        return personName;
    }    
    public void setPersonName(String personName) {
        this.personName = personName;
    }
}

在服务包中:

public class ServiceManager {
    private PersonService personService;

    public PersonService getPersonService() {
        return personService;
    }
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }   
}
import com.mydb.*;

public class BaseServices {

    protected DAOManager daoManager;
    public DAOManager getDaoManager() {
        return daoManager;
    }
    public void setDaoManager(DAOManager daoManager) {
        this.daoManager = daoManager;
    }   
}
import java.util.List;

import com.myvos.Person;

public interface PersonService {

    public abstract void save(Person transientInstance);
    public abstract void delete(Person persistentInstance);
    public abstract Person getById(int id); 
    public abstract Person getByName(String name);
    public abstract List getAll();
}
import java.util.List;

import com.myvos.Person;
public class PersonServiceImpl extends BaseServices implements PersonService {

    @Override
    public void save(Person person) {
        daoManager.getPersonDAO().update(person);
    }
    @Override
    public void delete(Person persistentInstance) {
        daoManager.getPersonDAO().delete(persistentInstance);

    }
    @Override
    public Person getById(int id) {
        // TODO Auto-generated method stub
        return daoManager.getPersonDAO().getbyId(id);
    }   
    @Override
    public Person getByName(String name) {
        // TODO Auto-generated method stub
        return daoManager.getPersonDAO().getbyName(name);
    }

    @Override
    public List getAll() {
        // TODO Auto-generated method stub
        return daoManager.getPersonDAO().getAllPersons();
    }
}

在DAO包中,我已经放置了以下类

public class DAOManager {

    public PersonDAO personDAO;

    public PersonDAO getPersonDAO() {
        return personDAO;
    }

    public void setPersonDAO(PersonDAO personDAO) {
        this.personDAO = personDAO;
    }   
}
public interface PersonDAO {

    public List<Person> getAllPersons();

    public Person getbyId(int id);

    public Person getbyName(String name);

    public void update(Person person);

    public void delete(Person person);
}
public class PersonDAOImpl implements PersonDAO {

   //list is working as a database
   List<Person> persons;
   //Person person;

   public PersonDAOImpl(){
       persons = new ArrayList<Person>();
       Person person1 = new Person(0, "Neelam");
       Person person2 = new Person(1, "Saeeda");
       Person person3 = new Person(2, "Sofia");
      persons.add(person1);
      persons.add(person2); 
      persons.add(person3); 

   }
   @Override
   public void delete(Person person) {
      persons.remove(person.getPersonId());
      System.out.println("Person: Id " + person.getPersonId() + ", deleted from record/db");
   }

   //retrive list of students from the database
   @Override
   public List<Person> getAllPersons() {
      return persons;
   }

   @Override
   public Person getbyId(int id) {
      return getbyId(id);
   }

   @Override
   public Person getbyName(String name) {
      return getbyName(name);
   }

   @Override
   public void update(Person person) {
       persons.get(person.getPersonId()).setPersonName(person.getPersonName());
      System.out.println("Person: Id " + person.getPersonId() + ", updated in the record/db");
   }
}

最后控制器类(以及抽象控制器)是:

@ManagedBean(name="firstController")
public class FirstController extends AbstractController {

    private String personName;
    private List allpersons;            

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }

    public List getAllpersons() {
        return allpersons;
    }

    public void setAllpersons(List allpersons) {
        this.allpersons = allpersons;
    }   
    public FirstController() {
        personName  =   personService.getById(0).getPersonName();                   
        allpersons  =   personService.getAll();

    }

值得一提的是:我没有使用hibernate或任何数据库访问,只想从PersonDAOImpl.java中获取硬编码值 我试图设计applicationContext文件,但肯定是错误的,并给出了错误。 请帮我纠正以下applicationContext.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


    <bean id="PersonDAO" class="com.mydb.PersonDAOImpl/">
        <property name="persons">
            <ref bean="Person" />
        </property>
    </bean>

    <bean id="daoManager" class="com.mydb.DAOManager" >
            <property name="personDAO">
            <ref bean="personDAOImpl" />
        </property>
    </bean>

    <bean id="PersonService" class="com.myservices.PersonServiceImpl">
        <property name="daoManager">
            <ref bean="daoManager" />
        </property>
    </bean>

    <bean id="serviceManager" class="com.myservices.ServiceManager">
        <property name="personService">
            <ref bean="personService" />
        </property>
    </bean>

</beans>

提前致谢...

0 个答案:

没有答案