標籤:
/** * 資料來源配置 * 資料來源配置個人覺得還是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(); } }}
spring+mybati java config配置引起的bean相互引用日誌警示告問題