38 publicDataSourcedataSource(){
39 DruidDataSourcedataSource=newDruidDataSource();
40 dataSource.setUsername("root");
41 dataSource.setPassword("123456");
42 dataSource.setDriverClassName("com.mysql.jdbc.Driver");
43 dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis_example");
44 returndataSource;
45 }
1 publicclassSqlSessionFactoryBeanimplementsFactoryBean<SqlSessionFactory>,Initializin
gBean,ApplicationListener<ApplicationEvent>{
实现FactoryBean接口的getObject方法:
1 /**
2 *
3 *将SqlSessionFactory对象注入spring容器
4 *{@inheritDoc}
5 */
6 @Override
7 publicSqlSessionFactorygetObject()throwsException{
8 if(this.sqlSessionFactory==null){
9 afterPropertiesSet();
10 }
11
12 returnthis.sqlSessionFactory;
13 }
SqlSessionFactoryBean实现InitializingBean接口,需要实现其afterPropertiesSet():
1 /**
2 *{@inheritDoc}
3 */
4 /**
5 *方法实现说明:我们自己配置文件中配置了SqlSessionFactoryBean,我们发现配置了该类实现了FactoryB
ean接口,也实现了bean的生命周期回调接口InitializingBean
6 *首先我们会调用生命周期的回调afterPropertiesSet()就是我们的SqlSessionFactorybean已经调用了
构造方法,已经调用了我们的
7 *
8 *@author:xsls
9 *@return:
10 *@exception:
11 *@date:2019/8/2319:33
12 */
13 @Override
14 publicvoidafterPropertiesSet()throwsException{
15 notNull(dataSource,"Property'dataSource'isrequired");
16 notNull(sqlSessionFactoryBuilder,"Property'sqlSessionFactoryBuilder'isrequired");
17 state((configuration==null&&configLocation==null)||!(configuration!=null&&c
onfigLocation!=null),