spring+mybati java config配置引起的bean相互引用日志报警告问题

  • 报循环引用警告的配置



/**
 * 数据源配置
 * 数据源配置个人觉得还是xml好些。用xml配置改动增加配置只需重启
 * 
 * @author doctor
 *
 * @time 2015年3月3日 下午2:57:10
 */
@Configuration
public 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
 */
@Configuration
public 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
 */
@Configuration
public 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值