[Spring Data MongoDB] Learning notes--Registering a MONGO instance

Source: Internet
Author: User

1. Through the Java based bean metadata

@Configuration  Public class AppConfig {publicthrows  unknownhostexceptioin {returnnew Mongo ("localhost");}}

The above method contains exception handling, which is not what we want.

Therefore, should try to use the following way Mongofactorybean, or later XML way.

@Configuration  Public class AppConfig {publicnew  Mongofactorybean (); Mongo.sethost ("localhost" ); return MONGO;}}

2. Via XML based metadata

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mongo= "Http://www.springframework.org/schema/data/mongo"xsi:schemalocation= "Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/sprin G-context-3.0.xsd Http://www.springframework.org/schema/data/mongo Http://www.springframework.org/schema /data/mongo/spring-mongo-1.0.xsd Http://www.springframework.org/schema/beans http://www.springframework. Org/schema/beans/spring-beans-3.0.xsd ">    <!--Default Bean name is ' MONGO ' -    <Mongo:mongoHost= "localhost"Port= "27017"/></Beans>

A more detailed configuration with mongooptions can be as follows, but the value is not a recommended value.

<Beans>  <Mongo:mongoHost= "localhost"Port= "27017">    <mongo:optionsConnections-per-host= "8"Threads-allowed-to-block-for-connection-multiplier= "4"Connect-timeout= "+"Max-wait-time= "the"Auto-connect-retry= "true"socket-keep-alive= "true"Socket-timeout= " the"Slave-ok= "true"Write-number= "1"Write-timeout= "0"Write-fsync= "true"/>  </mongo:mongo/></Beans>

Replica configuration

<id= "Replicasetmongo"  replica-set= "127.0.0.1:27017,localhost : 27018 "/>       

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.