Spring+mybati The Java config configuration causes the bean to cross-reference the journal warning issue

Source: Internet
Author: User

    • The configuration of the report circular reference warning
/** * Data Source Configuration * Data Source configuration Personally think it's better to be XML. Add configuration with XML configuration change only restart * * @author doctor * * @time March 3, 2015 PM 2:57:10 */@Configurationpublic class Datasourceconfig {@Bea n public static Propertysourcesplaceholderconfigurer Propertysourcesplaceholderconfigurer () {return new Propert    Ysourcesplaceholderconfigurer (); } @Target (Elementtype.type) @Retention (retentionpolicy.runtime) @Documented public @interface DbH2 {}/ * * H2 Data Original configuration * Do not import, reason document see: {@code configuration} document with nested configuration classes part * How to handle the spring source code see : * {@link org.springframework.context.annotation.configurationclassparser#processmemberclasses ( ConfigurationClass, Sourceclass)} * * @author doctor * * @time March 3, 2015 afternoon 3:26:15 */@Configurat Ion @MapperScan (basepackages = {"Com.doctor.spring4.common.mapper"}, Annotationclass = Dbh2.class, Sqlsessionfactoryr    EF = "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 D Bh2sqlsessionfactory (DataSource DataSource) throws Exception {Sqlsessionfactorybean Sqlsessionfactorybean = NE            W Sqlsessionfactorybean ();            Sqlsessionfactorybean.setdatasource (DataSource);            Sqlsessionfactorybean.settypehandlerspackage (""); SqlsessionFactorybean.setconfiglocation (New Classpathresource ("/spring4_2015config/mybatis-db-config.xml"));        return Sqlsessionfactorybean.getobject (); }    }}

Dbh2datasource,dbh2sqlsessionfactory,mapperscan has a dependency relationship.

    • If the Mapperscan is configured separately, there will be no warning, for example:

 

/** * Data Source Configuration * Data Source configuration Personally think it's better to be XML. Add configuration with XML configuration change only restart * * @author doctor * * @time March 3, 2015 PM 2:57:10 */@Configurationpublic class Datasourceconfig {@Bea n public static Propertysourcesplaceholderconfigurer Propertysourcesplaceholderconfigurer () {return new Propert    Ysourcesplaceholderconfigurer (); } @Target (Elementtype.type) @Retention (retentionpolicy.runtime) @Documented public @interface DbH2 {}/ * * H2 Data Original configuration * Do not import, reason document see: {@code configuration} document with nested configuration classes part * How to handle the spring source code see : * {@link org.springframework.context.annotation.configurationclassparser#processmemberclasses ( ConfigurationClass, Sourceclass)} * * @author doctor * * @time March 3, 2015 afternoon 3:26:15 */@Configurat Ion @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 D Bh2sqlsessionfactory (DataSource DataSource) throws Exception {Sqlsessionfactorybean Sqlsessionfactorybean = NE            W 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.clas s, sqlsessionfactoryref = "Dbh2sqlsessionfactory") static class Mybatismapperconfig {//Mapperscan annotations do not and data source defined configuration        Written together, (as in Mybatish2config configuration),//No This causes a circular reference to initialize the bean problem. It seems that the XML configuration still has advantages//03-11 17:01:50.010 main WARN O.s.b.f.s.defaultlistablebeanfactory-bean creation//EXC Eption on Factorybean type check://Org.springframework.beans.factory.BeanCreationException:Error Creating Bean W ITH name//' usermapper ' defined in file//[/home/cui/workspace/spring4-2015/target/classes/com/doctor/spri  Ng4/common/mapper/usermapper.class]://cannot resolve reference to beans ' 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? }}
    • but the best configuration is as follows:
/** * Data Source Configuration * Data Source configuration Personally think it's better to be XML. Add configuration changes with XML configuration just restart * * Note: Two dependent bean injection methods in the Mybatish2config class, do not inject in the form of a set method, and may cause injection circular reference problems. (@MapperScan annotations are mybatish2config, and annotations depend on the bean definition inside). * @author Doctor * * @time March 3, 2015 afternoon 2:57:10 */@Configurationpublic class Datasourceconfig {@Bean public static P Ropertysourcesplaceholderconfigurer Propertysourcesplaceholderconfigurer () {return new PROPERTYSOURCESPLACEHOLDERC    Onfigurer (); } @Target (Elementtype.type) @Retention (retentionpolicy.runtime) @Documented public @interface DbH2 {}/ * * H2 Data Original configuration * Do not import, reason document see: {@code configuration} document with nested configuration classes part * How to handle the spring source code see : * {@link org.springframework.context.annotation.configurationclassparser#processmemberclasses ( ConfigurationClass, Sourceclass)} * * @author doctor * * @time March 3, 2015 afternoon 3:26:15 */@Configurat Ion @MapperScan (basepackages = {"Com.doctor.spring4.common.mapper"}, Annotationclass = Dbh2.class, Sqlsessionfactoryref = "Dbh2sqlsessionfactory") @PropertySource ("Classpath:/spring4_2015pro/jdbc-h2.properties") s Tatic 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 configuration causes the bean to cross-reference the log-report warning issue

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.