浏览 1616 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2006-11-25
目前在Spring 2.0里,可以向Domain Model对象里注入DAO,让POJO真正变成充血模型成为现实,这就意味著可以直接在POJO对象里进行业务逻辑和事物托管,只要我们在代码里用new操作符构造出来的对象都可以给Spring容器进行托管、具体使用如下:
java 代码
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&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 代码
运行:
java -javaagent:G:\我的开发包\spring\spring-framework-2.0-rc2\lib\aspectj\aspectjweaver.jar Client
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2006-11-25
晕,JavaEye的可视化编辑器有问题,一发就成这样子,带有HTML标签的XML文档就乱了,我编辑了好几次都不行,郁闷死啦,还有本来发表后修改想使用BBCODE编辑器来修改,可是发现修改的时候没有BBCODE这么一项选择,JavaEye有空加上去吧,顺便管理员看到这篇帖后麻烦删除掉,太难看啦!
|
|
| 返回顶楼 | |
|
时间:2006-11-25
其实大师说的不一定就是好的,看不出这样比用一个service好在哪里
|
|
| 返回顶楼 | |
|
时间: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();
}
}
|
|
| 返回顶楼 | |
|
时间:2006-11-25
public interface UserDao {
public List loadAll(); } 这和有servervice层有有什么区别。模型驱动也没法贯彻啊 |
|
| 返回顶楼 | |
|
时间:2006-11-25
直接注入EntityManger,而不是什么dao
public class User { private int id; private String name; private EntityManager entityManager; } |
|
| 返回顶楼 | |
|
时间:2007-09-07
对new出来的对象可以注入dao,那如果是通过方法查找出来的对象,
怎么注入dao呢? |
|
| 返回顶楼 | |
|
时间:2007-09-07
是否new或者查询出来的对象都能注入dao?
另外有什么办法实现静态调用,如User.find(id)这样 |
|
| 返回顶楼 | |











