getObject()
方法创建 SqlSessionFactory。<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:config/mybatis-config-master.xml"/>
<property name="mapperLocations" value="classpath:config/mappers/master/**/*.xml"/>
</bean>
说明:我们看上面的 bean,因为实现了 FactoryBean 接口,所以返回的不是 SqlSessionFactoryBean 的实例,而是它的 SqlSessionFactoryBean.getObject() 的返回值。
getBean()
里。getBean()
的 doGetBean()
方法调用 getSingleton()
进行 bean 的创建。注:单例模式的依赖注入部分见《框架》篇第一章。
- JDK 中继承于 EventObject,在 Spring 框架中所有的事件都需要继承 ApplicationEvent,并且通过构造器参数 source 得到事件源。
- 在 Spring 框架中主要使用的事件类,是 ApplicationEvent 的实现类 **ApplicationContextEvent**,表示 ApplicaitonContext 的容器事件;
- 代码:
public abstract class ApplicationEvent extends EventObject {
private static final long serialVersionUID = 7099057708183571937L;
private final long timestamp;
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
public final long getTimestamp() {
return this.timestamp;
}
}
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
void onApplicationEvent(E event);
}
public interface ApplicationEventPublisher {
void publishEvent(ApplicationEvent event);
}
public void publishEvent(ApplicationEvent event) {
Assert.notNull(event, "Event must not be null");
if (logger.isTraceEnabled()) {
logger.trace("Publishing event in " + getDisplayName() + ": " + event);
}
getApplicationEventMulticaster().multicastEvent(event);
if (this.parent != null) {
this.parent.publishEvent(event);
}
}
publishEvent()
方法需要调用其方法 getApplicationEventMulticaster()
;public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext, DisposableBean {
private ApplicationEventMulticaster applicationEventMulticaster;
protected void registerListeners() {
// Register statically specified listeners first.
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let post-processors apply to them!
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String lisName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(lisName);
}
}
}
public abstract class JdbcTemplate {
public final Object execute(String sql){
Connection con=null;
Statement stmt=null;
try{
con = getConnection();
stmt = con.createStatement();
Object retValue=executeWithStatement(stmt, sql);
return retValue;
} catch (SQLException e) {
...
} finally {
closeStatement(stmt);
releaseConnection(con);
}
}
protected abstract Object executeWithStatement(Statement stmt, String sql);
}
public interface StatementCallback{
Object doWithStatement(Statement stmt);
}
public class JdbcTemplate {
public final Object execute(StatementCallback callback) {
Connection con = null;
Statement stmt = null;
try {
con = getConnection();
stmt = con.createStatement();
Object retValue = callback.doWithStatement(stmt);
return retValue;
} catch (SQLException e) {
...
} finally {
closeStatement(stmt);
releaseConnection(con);
}
}
...//其它方法定义
}
Jdbc使用方法如下:
JdbcTemplate jdbcTemplate = ...;
final String sql=...;
StatementCallback callback=new StatementCallback(){
public Object=doWithStatement(Statement stmt){
return ...;
}
}
jdbcTemplate.execute(callback);