我对SpringDAO层支持的总结
之前发过两篇关于Spring的总结帖子 反响还不错,再把剩下的几篇发上来。共享给大家。
我对IoC/DI的理解
我对AOP的理解
1、问题
1、JDBC/ORM框架(如Hibernate)开发中编程模型有哪些缺点? 如JDBC

2、解决方案(模板设计模式,本质:将可变的和不可变的分离)
模板方法模式:定义操作的步骤(固定的),将可变的步骤交给子类处理。
public interface JdbcOperations {
//接口定义行为集
public Object execute() throws SQLException ;
}
public abstract class AbstractJdbcOperations implements JdbcOperations {
@Override
public final Object execute() throws SQLException {
Connection conn = DataSourceUtils.getConnection();
try {
Object retVal = doInConnection(conn);
conn.commit();
return retVal;
}catch (Exception e) { conn.rollback(); throw e;}
finally { conn.close(); }
}
public abstract Object doInConnection(Connection conn) throws SQLException;
}
public class DataSourceUtils {
public static Connection getConnection() {
//返回数据库连接
return null;
}
}
JdbcOperations select = new AbstractJdbcOperations() {
@Override
public Object doInConnection(Connection conn) throws SQLException {
PreparedStatement pstmt =
conn.prepareStatement("select * from tbl_user");
ResultSet rs = pstmt.executeQuery();
List result = null;
//处理结果集
return result;
}
};
select.execute();
缺点:不够灵活
3、解决方案(模板设计模式+回调,本质:将可变的和不可变的分离,可变通过回调)
**回调(命令):**由组件定义,但不由组件调用,而是由系统调用
一般用于:可变不可变分离,未知功能。
public interface JdbcOprations2 {
//接口定义行为集
public Object execute(ConnectionCallback callback) throws Exception ;
}
public interface ConnectionCallback {
public Object doInConnection(Connection conn) throws SQLException;
}
public class JdbcTemplate implements JdbcOprations2 {
@Override
public Object execute(ConnectionCallback callback) throws Exception {
Connection conn = DataSourceUtils.getConnection();
try {
Object retVal = callback.doInConnection(conn);
conn.commit();
return retVal;
}
catch (Exception e) {
conn.rollback();
throw e;
}
finally {
conn.close();
}
}
}
jdbcTemplate.execute(new ConnectionCallback() {
@Override
public Object doInConnection(Connection conn) throws SQLException {
//可变操作
return null;
}
});
public interface PreparedStatementCallback {
public Object doInPreparedStatement(PreparedStatement pstmt)
throws SQLException;
}
缺点:灵活但不通用
4、解决方案(Spring JDBC框架)

JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
5、异常问题
JDBC/ORM框架(如Hibernate)开发中异常处理有哪些缺点? 不一致的异常类,如JDBC是SQLException 而hibernate是HibernateException,等。
缺点:不一致的异常体系,需要了解每个框架的异常含义 ,Spring的一致的异常体系(DataAccessException)

6、DAO访问问题
访问JDBC和访问Hibernate使用完全不同且根本不类似的API?
为了便于以一种一致的方式使用各种数据访问技术,如JDBC、JDO和Hibernate,
Spring****提供了一套抽象DAO类供你扩展。
JdbcDaoSupport - JDBC数据访问对象的基类。
需要一个DataSource,同时为子类提供 JdbcTemplate。
HibernateDaoSupport - Hibernate数据访问对象的基类。
需要一个SessionFactory,同时为子类提供 HibernateTemplate。
JdoDaoSupport - JDO数据访问对象的基类。
需要设置一个PersistenceManagerFactory, 同时为子类提供JdoTemplate。
JpaDaoSupport - JPA数据访问对象的基类。
需要一个EntityManagerFactory,同时 为子类提供JpaTemplate。
一致的DAO抽象
Callback:定义可变行为,即不可预知行为(命令设计模式)
Template:提供模板支持,简化开发Support:提供对Template的支持,提供一组简便方法,并提供获取Template的方法
相关资料
http://jinnianshilongnian.iteye.com/blog/1487235你看下这个
其实我要说的意思是这里的setSelf是类,而不是接口,这样可以吗?我尝试了,如果是类会报错,接口没问题。
回答神速啊,拜读啦。。。 里面有个问题,他那个setSelf(ServiceA self)是怎么注入的
我在我自己的Service直接用@Autowired报错,代码片段如下
没找到匹配类型!错误信息: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.oy.service.UserService com.oy.controller.StudentController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.oy.service.UserService.setSelf(com.oy.service.UserService); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
spring内部实现的问题 可以看看我之前回复的 http://www.iteye.com/topic/1125911#2368327
哥们,请教个问题,为什么spring使用cglib动态代理没法拦截被代理类的所有非private方法,而我自己写的由cglib生成的代理类却可以拦截到被代理类的所有非private方法呢?,先道声谢谢,下面贴代码:
1. 注解类,用来注解哪些方法需要写日志
2. 工厂类,由cglib生成被代理对象
3.被代理的类
4.测试类
5.结果输出: ProxyFactory: Method: public java.lang.String test.log.Student.doWork1(java.lang.String,int) ProxyFactory: params: ProxyFactory: Method: java.lang.String test.log.Student.doWork2(java.lang.String,int) ProxyFactory: params: ProxyFactory: Method: protected java.lang.String test.log.Student.doWork3(java.lang.String,int) ProxyFactory: params: ProxyFactory: result:"姓名: Xiao Zhang, 年龄: 22" ProxyFactory: result:"姓名: Xiao Zhang, 年龄: 22" ProxyFactory: result:"姓名: Xiao Zhang, 年龄: 22"
如果把被代理类放到spring里面,则只能拦截到方法doWork1,其他方法拦截不到,为什么会有这种差异?
@AutoWriteLog
public List test1(String arg){
return test2(arg);
}
这样调用不行
原因见 http://jinnianshilongnian.iteye.com/blog/1487235
哥们,请教个aop问题,如果使用cglib动态代理目标对象,是否可以拦截到目标对象的所有非private方法?请看下面:
下面是我的service代码片段
下面是我的application*.xml配置:
调试时,被代理的类又出现CGLIB字样,说明是CGLIB动态代理生成的类
现在问题是:接口方法findAll()访问时可以被拦截到,但是两个非接口测试方法调用时没有被拦截到,是不是我哪里配置少啦,指点下,多谢!
:D :D :idea:
:D
跟进开涛 哥 :idea: