博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring+mybati java config配置引起的bean相互引用日志报警告问题
阅读量:6815 次
发布时间:2019-06-26

本文共 12048 字,大约阅读时间需要 40 分钟。

hot3.png

  • 报循环引用警告的配置


/** * 数据源配置 * 数据源配置个人觉得还是xml好些。用xml配置改动增加配置只需重启 *  * @author doctor * * @time 2015年3月3日 下午2:57:10 */@Configurationpublic class DataSourceConfig {	@Bean	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {		return new PropertySourcesPlaceholderConfigurer();	}	@Target(ElementType.TYPE)	@Retention(RetentionPolicy.RUNTIME)	@Documented	public @interface DbH2 {	}	/**	 * H2 数据原配置	 * 不用import ,原因文档见:{@code Configuration}文档中的With nested  Configuration  classes部分	 * spring源码如何处理见:	 * {@link org.springframework.context.annotation.ConfigurationClassParser#processMemberClasses(ConfigurationClass, SourceClass) }	 * 	 * @author doctor	 *	 * @time 2015年3月3日 下午3:26:15	 */	@Configuration	@MapperScan(basePackages = { "com.doctor.spring4.common.mapper" }, annotationClass = DbH2.class, sqlSessionFactoryRef = "dbH2SqlSessionFactory")	@PropertySource("classpath:/spring4_2015Pro/jdbc-H2.properties")	static class MybatisH2Config {		@Value("${jdbc.H2.url}")		private String url;		@Value("${jdbc.H2.user}")		private String user;		@Value("${jdbc.H2.password}")		private String password;		@Value("${jdbc.H2.driverClassName}")		private String driverClassName;		@Value("${jdbc.H2.initialSize}")		private int initialSize;		@Value("${jdbc.H2.minIdle}")		private int minIdle;		@Value("${jdbc.H2.maxActive}")		private int maxActive;		@Value("${jdbc.H2.maxWait}")		private long maxWait;		@Value("${jdbc.H2.minEvictableIdleTimeMillis}")		private long minEvictableIdleTimeMillis;		@Value("${jdbc.H2.timeBetweenEvictionRunsMillis}")		private long timeBetweenEvictionRunsMillis;		@Value("${jdbc.H2.validationQuery}")		private String validationQuery;		@Value("${jdbc.H2.testWhileIdle}")		private boolean testWhileIdle;		@Value("${jdbc.H2.testOnBorrow}")		private boolean testOnBorrow;		@Value("${jdbc.H2.testOnReturn}")		private boolean testOnReturn;		@Value("${jdbc.H2.poolPreparedStatements}")		private boolean poolPreparedStatements;		@Value("${jdbc.H2.maxPoolPreparedStatementPerConnectionSize}")		private int maxPoolPreparedStatementPerConnectionSize;		@Bean(name = "dbH2DataSource", initMethod = "init", destroyMethod = "close")		public DataSource dbH2DataSource() {			DruidDataSource druidDataSource = new DruidDataSource();			druidDataSource.setUrl(url);			druidDataSource.setUsername(user);			druidDataSource.setPassword(password);			druidDataSource.setDriverClassName(driverClassName);			druidDataSource.setInitialSize(initialSize);			druidDataSource.setMinIdle(minIdle);			druidDataSource.setMaxActive(maxActive);			druidDataSource.setMaxWait(maxWait);			druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);			druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);			druidDataSource.setValidationQuery(validationQuery);			druidDataSource.setTestWhileIdle(testWhileIdle);			druidDataSource.setTestOnBorrow(testOnBorrow);			druidDataSource.setTestOnReturn(testOnReturn);			druidDataSource.setPoolPreparedStatements(poolPreparedStatements);			druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);			return druidDataSource;		}		@Bean(name = "dbH2SqlSessionFactory")		@Resource(name = "dbH2DataSource")		public SqlSessionFactory DbH2SqlSessionFactory(DataSource dataSource) throws Exception {			SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();			sqlSessionFactoryBean.setDataSource(dataSource);			sqlSessionFactoryBean.setTypeHandlersPackage("");			sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("/spring4_2015Config/mybatis-db-config.xml"));			return sqlSessionFactoryBean.getObject();		}	}}

dbH2DataSource,dbH2SqlSessionFactory,MapperScan有依赖关系.

  • ##如果把MapperScan单独配置,就不会有警告,例如:##

 

/** * 数据源配置 * 数据源配置个人觉得还是xml好些。用xml配置改动增加配置只需重启 *  * @author doctor * * @time 2015年3月3日 下午2:57:10 */@Configurationpublic class DataSourceConfig {	@Bean	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {		return new PropertySourcesPlaceholderConfigurer();	}	@Target(ElementType.TYPE)	@Retention(RetentionPolicy.RUNTIME)	@Documented	public @interface DbH2 {	}	/**	 * H2 数据原配置	 * 不用import ,原因文档见:{@code Configuration}文档中的With nested  Configuration  classes部分	 * spring源码如何处理见:	 * {@link org.springframework.context.annotation.ConfigurationClassParser#processMemberClasses(ConfigurationClass, SourceClass) }	 * 	 * @author doctor	 *	 * @time 2015年3月3日 下午3:26:15	 */	@Configuration	@PropertySource("classpath:/spring4_2015Pro/jdbc-H2.properties")	static class MybatisH2Config {		@Value("${jdbc.H2.url}")		private String url;		@Value("${jdbc.H2.user}")		private String user;		@Value("${jdbc.H2.password}")		private String password;		@Value("${jdbc.H2.driverClassName}")		private String driverClassName;		@Value("${jdbc.H2.initialSize}")		private int initialSize;		@Value("${jdbc.H2.minIdle}")		private int minIdle;		@Value("${jdbc.H2.maxActive}")		private int maxActive;		@Value("${jdbc.H2.maxWait}")		private long maxWait;		@Value("${jdbc.H2.minEvictableIdleTimeMillis}")		private long minEvictableIdleTimeMillis;		@Value("${jdbc.H2.timeBetweenEvictionRunsMillis}")		private long timeBetweenEvictionRunsMillis;		@Value("${jdbc.H2.validationQuery}")		private String validationQuery;		@Value("${jdbc.H2.testWhileIdle}")		private boolean testWhileIdle;		@Value("${jdbc.H2.testOnBorrow}")		private boolean testOnBorrow;		@Value("${jdbc.H2.testOnReturn}")		private boolean testOnReturn;		@Value("${jdbc.H2.poolPreparedStatements}")		private boolean poolPreparedStatements;		@Value("${jdbc.H2.maxPoolPreparedStatementPerConnectionSize}")		private int maxPoolPreparedStatementPerConnectionSize;		@Bean(name = "dbH2DataSource", initMethod = "init", destroyMethod = "close")		public DataSource dbH2DataSource() {			DruidDataSource druidDataSource = new DruidDataSource();			druidDataSource.setUrl(url);			druidDataSource.setUsername(user);			druidDataSource.setPassword(password);			druidDataSource.setDriverClassName(driverClassName);			druidDataSource.setInitialSize(initialSize);			druidDataSource.setMinIdle(minIdle);			druidDataSource.setMaxActive(maxActive);			druidDataSource.setMaxWait(maxWait);			druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);			druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);			druidDataSource.setValidationQuery(validationQuery);			druidDataSource.setTestWhileIdle(testWhileIdle);			druidDataSource.setTestOnBorrow(testOnBorrow);			druidDataSource.setTestOnReturn(testOnReturn);			druidDataSource.setPoolPreparedStatements(poolPreparedStatements);			druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);			return druidDataSource;		}		@Bean(name = "dbH2SqlSessionFactory")		@Resource(name = "dbH2DataSource")		public SqlSessionFactory DbH2SqlSessionFactory(DataSource dataSource) throws Exception {			SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();			sqlSessionFactoryBean.setDataSource(dataSource);			sqlSessionFactoryBean.setTypeHandlersPackage("");			sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("/spring4_2015Config/mybatis-db-config.xml"));			return sqlSessionFactoryBean.getObject();		}	}	@Configuration	@MapperScan(basePackages = { "com.doctor.spring4.common.mapper" }, annotationClass = DbH2.class, sqlSessionFactoryRef = "dbH2SqlSessionFactory")	static class MyBatisMapperConfig {		// MapperScan注解不要和数据源定义的配置写在一起,(如MybatisH2Config配置上),		// 否此会导致循环引用初始化bean问题.		// 看来xml配置还是有优势的		// 03-11 17:01:50.010 main WARN o.s.b.f.s.DefaultListableBeanFactory - Bean creation		// exception on FactoryBean type check:		// org.springframework.beans.factory.BeanCreationException: Error creating bean with name		// 'userMapper' defined in file		// [/home/cui/workspace/spring4-2015/target/classes/com/doctor/spring4/common/mapper/UserMapper.class]:		// Cannot resolve reference to bean 'dbH2SqlSessionFactory' while setting bean property		// 'sqlSessionFactory'; nested exception is		// org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean		// with name 'dbH2SqlSessionFactory': Requested bean is currently in creation: Is there an		// unresolvable circular reference?	}}
  • ##但是最好的配置还是如下:##
/** * 数据源配置 * 数据源配置个人觉得还是xml好些。用xml配置改动增加配置只需重启 *  * 注意:MybatisH2Config 类中两个有依赖关系的bean注入方法,不要用set方法形式注入, * 有可能导致注入循环引用问题.(@MapperScan 注解在MybatisH2Config,而且注解依赖里面的bean定义). * @author doctor * * @time 2015年3月3日 下午2:57:10 */@Configurationpublic class DataSourceConfig {	@Bean	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {		return new PropertySourcesPlaceholderConfigurer();	}	@Target(ElementType.TYPE)	@Retention(RetentionPolicy.RUNTIME)	@Documented	public @interface DbH2 {	}	/**	 * H2 数据原配置	 * 不用import ,原因文档见:{@code Configuration}文档中的With nested  Configuration  classes部分	 * spring源码如何处理见:	 * {@link org.springframework.context.annotation.ConfigurationClassParser#processMemberClasses(ConfigurationClass, SourceClass) }	 * 	 * @author doctor	 *	 * @time 2015年3月3日 下午3:26:15	 */	@Configuration	@MapperScan(basePackages = { "com.doctor.spring4.common.mapper" }, annotationClass = DbH2.class, sqlSessionFactoryRef = "dbH2SqlSessionFactory")	@PropertySource("classpath:/spring4_2015Pro/jdbc-H2.properties")	static class MybatisH2Config {		@Value("${jdbc.H2.url}")		private String url;		@Value("${jdbc.H2.user}")		private String user;		@Value("${jdbc.H2.password}")		private String password;		@Value("${jdbc.H2.driverClassName}")		private String driverClassName;		@Value("${jdbc.H2.initialSize}")		private int initialSize;		@Value("${jdbc.H2.minIdle}")		private int minIdle;		@Value("${jdbc.H2.maxActive}")		private int maxActive;		@Value("${jdbc.H2.maxWait}")		private long maxWait;		@Value("${jdbc.H2.minEvictableIdleTimeMillis}")		private long minEvictableIdleTimeMillis;		@Value("${jdbc.H2.timeBetweenEvictionRunsMillis}")		private long timeBetweenEvictionRunsMillis;		@Value("${jdbc.H2.validationQuery}")		private String validationQuery;		@Value("${jdbc.H2.testWhileIdle}")		private boolean testWhileIdle;		@Value("${jdbc.H2.testOnBorrow}")		private boolean testOnBorrow;		@Value("${jdbc.H2.testOnReturn}")		private boolean testOnReturn;		@Value("${jdbc.H2.poolPreparedStatements}")		private boolean poolPreparedStatements;		@Value("${jdbc.H2.maxPoolPreparedStatementPerConnectionSize}")		private int maxPoolPreparedStatementPerConnectionSize;		@Bean(name = "dbH2DataSource", initMethod = "init", destroyMethod = "close")		public DataSource dbH2DataSource() {			DruidDataSource druidDataSource = new DruidDataSource();			druidDataSource.setUrl(url);			druidDataSource.setUsername(user);			druidDataSource.setPassword(password);			druidDataSource.setDriverClassName(driverClassName);			druidDataSource.setInitialSize(initialSize);			druidDataSource.setMinIdle(minIdle);			druidDataSource.setMaxActive(maxActive);			druidDataSource.setMaxWait(maxWait);			druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);			druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);			druidDataSource.setValidationQuery(validationQuery);			druidDataSource.setTestWhileIdle(testWhileIdle);			druidDataSource.setTestOnBorrow(testOnBorrow);			druidDataSource.setTestOnReturn(testOnReturn);			druidDataSource.setPoolPreparedStatements(poolPreparedStatements);			druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);			return druidDataSource;		}		@Bean(name = "dbH2SqlSessionFactory")		public SqlSessionFactory DbH2SqlSessionFactory() throws Exception {			SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();			sqlSessionFactoryBean.setDataSource(dbH2DataSource());			sqlSessionFactoryBean.setTypeHandlersPackage("");			sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("/spring4_2015Config/mybatis-db-config.xml"));			return sqlSessionFactoryBean.getObject();		}	}}

转载于:https://my.oschina.net/doctor2014/blog/386431

你可能感兴趣的文章
正则表达式
查看>>
360前端星计划学习-html
查看>>
专注dApp高效执行和高并发的下一代公有链
查看>>
ONE-sys 整合前后端脚手架 koa2 + pm2 + vue-cli3.0 + element
查看>>
携带更方便功能全 iPone与Apple Watch球形尿袋
查看>>
行为型模式:策略模式
查看>>
实现批量数据增强 | keras ImageDataGenerator使用
查看>>
太忙女友消息未及时回复,分手吗?python微信自动消息帮你谈恋爱
查看>>
Java 多线程NIO学习
查看>>
命名实体识别
查看>>
动态切换的动态代理
查看>>
电商项目(下)
查看>>
寻找一种易于理解的一致性算法(扩展版)下
查看>>
MySQL - 高可用性:少宕机即高可用?
查看>>
2018电影票房分析-谁才是票房之王
查看>>
程序员可以干到多少岁?
查看>>
Storm系列(六)storm和kafka集成
查看>>
东南亚的招聘骗局,程序员请注意!
查看>>
Android 获得View宽高的几种方式
查看>>
iOS正则表达式
查看>>