论坛首页 Java版

利用Spring2.0的AOP功能向Domain Model注入DAO

浏览 1616 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2006-11-25
目前在Spring 2.0里,可以向Domain Model对象里注入DAO,让POJO真正变成充血模型成为现实,这就意味著可以直接在POJO对象里进行业务逻辑和事物托管,只要我们在代码里用new操作符构造出来的对象都可以给Spring容器进行托管、具体使用如下:
java 代码
  1. @Configurable(autowire=Autowire.BY_TYPE) // 加上基于jdk1.5的注解   
  2. public class User {   
  3.   private int id;   
  4.   private String name;   
  5.      
  6.   private UserDao userDao;   
  7.     
  8.   public int getId() {   
  9.     return this.id;   
  10.   }   
  11.     
  12.   public void setId(int id) {   
  13.     this.id = id;   
  14.   }   
  15.     
  16.   public String getName() {   
  17.    return this.name;   
  18.   }   
  19.     
  20.   public void setName(String name) {   
  21.     this.name = name;   
  22.   }   
  23.     
  24.   public UserDao getUserDao() {   
  25.     return userDao;   
  26.   }   
  27.     
  28.   public void setUserDao(UserDao userDao) {   
  29.     this.userDao = userDao;   
  30.   }   
  31. }   
  32.     
  33. public interface UserDao {   
  34.   public List loadAll();   
  35. }   
  36.     
  37. public class UserDaoImpl extends HibernateDaoSupport {   
  38.   public List loadAll() {   
  39.     .......................   
  40.     .......................   
  41.   }   
  42. }  
 
spring.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:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  <aop:spring-configured />
  <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url"
   value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
  <property name="username" value="root" />
  <property name="password" value="avaJevoLI" />
  <property name="maxWait" value="120000" />
  <property name="maxIdle" value="10" />
  <property name="maxActive" value="500" />
  <property name="initialSize" value="50" />
 </bean>
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="annotatedClasses">
   <list>
    <value>com.zhenuu.model.User</value>
   </list>
  </property>
  <!--
   <property name="annotatedPackages">
   <list>
   <value>test.package</value>
   </list>
   </property>
  -->
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLInnoDBDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>
 
 <bean id="userDao" class="com.zhenuu.user.dao.UserDaoImpl">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <bean id="user" class="com.zhenuu.user.model.User">
  <property name="userDao" ref="userDao" />
 </bean>
 
</beans>
 
client.java
java 代码
  1. public class Client {   
  2.   public static void main(String...args) {   
  3.     new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"});   
  4.     User user = new User();   
  5.     user.setId(10);   
  6.     List users = user.loadAll();   
  7.     for(User user1 : users) {   
  8.       System.out.println(user1.getName());   
  9.     }   
  10.   }   
  11. }  
 
运行:
java -javaagent:G:\我的开发包\spring\spring-framework-2.0-rc2\lib\aspectj\aspectjweaver.jar Client
   
时间:2006-11-25
晕,JavaEye的可视化编辑器有问题,一发就成这样子,带有HTML标签的XML文档就乱了,我编辑了好几次都不行,郁闷死啦,还有本来发表后修改想使用BBCODE编辑器来修改,可是发现修改的时候没有BBCODE这么一项选择,JavaEye有空加上去吧,顺便管理员看到这篇帖后麻烦删除掉,太难看啦!
   
0 请登录后投票
时间:2006-11-25
其实大师说的不一定就是好的,看不出这样比用一个service好在哪里
   
0 请登录后投票
时间:2006-11-25
现在不流行让Spring帮着弄AOP了,现在流行直接用AOP注入:

package org.sunflower.finderinjector;

import java.lang.reflect.Field;

import org.aspectj.lang.Signature;
import org.sunflower.IDomainObject;
import org.sunflower.info.*;

public aspect FinderInjector {
	
	private IFinderFactory finderFactory;
	
	public void setFinderFactory(IFinderFactory finderFactory){
		this.finderFactory = finderFactory;
	}
	
    pointcut getFinder() : 
        get(public static * IDomainObject+.*Finder);
    
    Object around() : getFinder(){
        try {
        	Signature signature = thisJoinPoint.getSignature();
        	Class clazz = signature.getDeclaringType();
        	Field getter = clazz.getField(signature.getName());
        	
        	Object finder;
        	finder = getter.get(thisJoinPoint.getThis());
        	if(finder == null){
        		finder = finderFactory.create(getter);
    	    	getter.set(thisJoinPoint.getThis(), finder);
        	}
		} catch (Exception e) {
			throw new SunflowerException(e);
		}         
    	return proceed();
    }

}
   
0 请登录后投票
时间:2006-11-25
public interface UserDao {
public List loadAll();
}

这和有servervice层有有什么区别。模型驱动也没法贯彻啊
   
0 请登录后投票
时间:2006-11-25
直接注入EntityManger,而不是什么dao
public class User {
private int id;
private String name;

private EntityManager entityManager;
}
   
0 请登录后投票
时间:2007-09-07
对new出来的对象可以注入dao,那如果是通过方法查找出来的对象,
怎么注入dao呢?
   
0 请登录后投票
时间:2007-09-07
是否new或者查询出来的对象都能注入dao?
另外有什么办法实现静态调用,如User.find(id)这样
   
0 请登录后投票
论坛首页 Java版

跳转论坛:
JavaEye推荐