最近在TSS上看到aridpojo框架(
code.google.com/p/aridpojos/),能够自动创建spring的bean定义(Chris Richardson's Spring library to autocreate bean definitions)。忍不住试了下。
创建一个Dao接口:
package test.dao;
import test.TestBean;
public interface IDao {
public TestBean add(TestBean bean);
public TestBean delete(TestBean bean);
}
写一个简单的Dao实现类,仅仅输出一些文字:
package test.dao;
import test.TestBean;
public class PlainDaoImpl implements IDao {
@Override
public TestBean add(TestBean bean) {
// TODO Auto-generated method stub
System.out.println("dao add method...");
return bean;
}
@Override
public TestBean delete(TestBean bean) {
// TODO Auto-generated method stub
System.out.println("dao delete method...");
return bean;
}
}
再写一个服务类接口:
package test.service;
import test.TestBean;
public interface IService {
public TestBean add(TestBean bean);
public TestBean delete(TestBean bean);
}
写一个简单的实现类,调用Dao实现类中的相应方法:
package test.service;
import test.TestBean;
import test.dao.IDao;
public class PlainServiceImpl implements IService{
private IDao plainDao=null;
@Override
public TestBean add(TestBean bean) {
// TODO Auto-generated method stub
System.out.println("service add method...");
this.plainDao.add(bean);
return null;
}
@Override
public TestBean delete(TestBean bean) {
// TODO Auto-generated method stub
System.out.println("service delete method...");
this.plainDao.delete(bean);
return null;
}
public void setPlainDao(IDao plainDao) {
this.plainDao = plainDao;
}
}
写一个简单的javabean:
package test;
public class TestBean {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
配置文件testApplicationContext.xml,这是重点:
<beans xmlns="http://www.springframework.org/schema/beans"><arid:define-beans package="test.service"><arid:define-beans package="test.dao"><beans xmlns="http://www.springframework.org/schema/beans"><arid:define-beans package="test.service"><arid:define-beans package="test.dao">
</arid:define-beans>《?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:arid="http://chrisrichardson.net/schema/arid"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://chrisrichardson.net/schema/arid http://chrisrichardson.net/schema/arid.xsd"
default-autowire="byName"》
《arid:define-beans package="test.service"
autowire="byType" /》
《arid:define-beans package="test.dao"
autowire="byType" /》
《/beans》
</arid:define-beans>
首先引入:http://chrisrichardson.net/schema/arid http://chrisrichardson.net/schema/arid.xsd。然后使用arid命名空间配置。package属性设置需要自动创建bean定义的包名;autowire属性设置按何种方式自动设置bean直接的引用关系,这个属性和spring本身的autowire的含义和取值一致。
写一个junit测试类:
package test;
import org.junit.Test;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import test.service.IService;
public class PlainServiceTest extends AbstractDependencyInjectionSpringContextTests{
private IService service;
@Override
protected String[] getConfigLocations() {
return new String[]{"classpath:appctx/**/testApplicationContext.xml"};
}
@Test
public void testAdd() {
TestBean bean=new TestBean();
this.service.add(bean);
}
@Test
public void testDelete() {
TestBean bean=new TestBean();
this.service.delete(bean);
}
public void setService(IService service) {
this.service = service;
}
}
输出:
service add method...
dao add method...
service delete method...
dao delete method...
确实能够工作。如果有bean需要依赖框架自动定义的bean呢?再试试。
写一个简单的bean调用PlainService:
package test.service;
import test.TestBean;
public class DelegateServiceImpl {
private IService plainService;
public void setPlainService(IService plainService) {
this.plainService = plainService;
}
public TestBean add(TestBean bean) {
// TODO Auto-generated method stub
System.out.println("delegate service add method...");
this.plainService.add(bean);
return null;
}
public TestBean delete(TestBean bean) {
// TODO Auto-generated method stub
System.out.println("delegate service delete method...");
this.plainService.delete(bean);
return null;
}
}
配置文件中增加:
<bean id="delegateService" class="test.service.DelegateServiceImpl"></bean>《bean id="delegateService" class="test.service.DelegateServiceImpl"》《/bean》
框架自动配置的bean名称缺省使用以下规则:
类名去掉Impl后缀。比如,PlainServiceImpl在spring容器中配置名称为“plainService”。
再写个junit测试类:
package test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import test.service.DelegateServiceImpl;
public class DelegateServiceTest extends
AbstractDependencyInjectionSpringContextTests {
private DelegateServiceImpl service;
@Override
protected String[] getConfigLocations() {
return new String[] { "classpath:appctx/**/testApplicationContext.xml" };
}
@Test
public void testAdd() {
TestBean bean = new TestBean();
this.service.add(bean);
}
@Test
public void testDelete() {
TestBean bean = new TestBean();
this.service.delete(bean);
}
public void setService(DelegateServiceImpl service) {
this.service = service;
}
}
输出:
delegate service add method...
service add method...
dao add method...
delegate service delete method...
service delete method...
dao delete method...
感觉非常不错,准备最近有时间一定要好好研究下。</beans></arid:define-beans></arid:define-beans></beans>
分享到:
相关推荐
藏区特产销售平台--论文.zip
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
该单片机项目可作为课程设计和期末大作业或者毕设,项目完整,有原理图和代码,需要的自行下载即可!
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载