spring(上一篇文章使用构造器,这一篇使用set方法)
来源:互联网 发布:兰州知豆车体广告 编辑:程序博客网 时间:2024/05/22 06:45
cn.dao
- <pre name="code" class="java">package cn.dao;
- public interface PersonDaoInterface {
- public abstract void add();
- }
cn.dao.imp
- package cn.dao.imp;
- import cn.dao.PersonDaoInterface;
- public class PersonDao implements PersonDaoInterface {
- public void add()
- {
- System.out.println("执行PersonDao.add()");
- }
- }
- package cn.service;
- public interface PersonService {
- public abstract void save();
- }
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;}@Resourcepublic 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
- 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
- <?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>
- spring(上一篇文章使用构造器,这一篇使用set方法)
- 使用SQL语句得到上一篇文章以及下一篇文章
- 文章上一篇,下一篇
- 实现上一篇下一篇文章的方法
- 上一篇文章、下一篇文章实现
- 转一篇lucene的使用的文章
- 一篇文章学会日志logback的使用
- 一篇文章轻松上手使用 less
- 文章上一篇 下一篇 功能
- 文章页上一篇下一篇
- SG上一篇好文章
- ASP.NET 实现上一篇文章 下一篇文章
- 用dataTable实现上一篇文章、下一篇文章
- PHP Laravel 取上一篇文章和下一篇文章
- 黑马程序员-我的第十一篇学习日记:OC构造方法和分类的使用
- 推荐一篇文章:构造健壮系统
- Spring MVC(三)基于上一篇使用JUnit编写测试用例
- Android:学习AIDL,这一篇文章就够了(上)
- Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子
- 【C++】对象作为函数参数【原创技术】
- 计算出生星期
- C# List Examples
- 计算纳税金额
- spring(上一篇文章使用构造器,这一篇使用set方法)
- ps抠图二:背景橡皮擦,通道抠图
- 《Windows程序设计》之位图操作
- python 中的 operator模块
- 开博24个月了,记录一下足迹,访问量10万次
- MFC应用程序中浏览PDF、Word文件
- 基于ArcGIS的遥感专题产品自动生产和发布
- 集合使用注意事项
- Object转换为String[]数组,或者其他类型的数组