您的位置:首页 > 编程语言 > Java开发

spring(上一篇文章使用构造器,这一篇使用set方法)

2012-05-25 08:43 761 查看
cn.dao

[java] view
plaincopy

<pre name="code" class="java">package cn.dao;

public interface PersonDaoInterface {

public abstract void add();

}



cn.dao.imp

[java] view
plaincopy

package cn.dao.imp;

import cn.dao.PersonDaoInterface;

public class PersonDao implements PersonDaoInterface {

public void add()

{

System.out.println("执行PersonDao.add()");

}

}

cn.service

[java] view
plaincopy

package cn.service;

public interface PersonService {

public abstract void save();

}

cn.service.imp

package cn.service.imp;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;

import cn.dao.imp.PersonDao;
import cn.service.PersonService;

public class PersonServiceBean implements PersonService {

private String name;
private PersonDao personDao;

public PersonServiceBean(){}

public PersonServiceBean(String name,PersonDao personDao) {
this.name = name;
this.personDao=personDao;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public PersonDao getPersonDao() {
return personDao;
}

@Resource
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}

public void save()
{
personDao.add();
//System.out.println(name);

}
}


也可以这样写

package cn.service.imp;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
import org.springframework.beans.factory.annotation.Qualifier;

import cn.dao.imp.PersonDao;
import cn.service.PersonService;

public class PersonServiceBean implements PersonService {

private String name;
@Autowired @Qualifier("personDao") private PersonDao personDao;

public PersonServiceBean(){}

public PersonServiceBean(String name,PersonDao personDao) {
this.name = name;
this.personDao=personDao;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public PersonDao getPersonDao() {
return personDao;
}

public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}

public void save()
{
personDao.add();
//System.out.println(name);

}
}


juintest

[java] view
plaincopy

package junit.test;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.service.imp.PersonServiceBean;

public class SpringTest

{

@Test

public void instanceSpring()

{

AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});

PersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personService");

personServiceBean.save();

ctx.close();

}

}

bean.xml

[html] view
plaincopy

<?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:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config></context:annotation-config>

<bean id="personDao" class="cn.dao.imp.PersonDao"></bean>

<bean id="personService" class="cn.service.imp.PersonServiceBean">

</bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐