当以下两个条件同时符合时,自动配置就会利用Spring 的核心容器
1.只有一个特定的服务类型。如:你只能在应用中绑定一个相关的数据库服务(MySQL或PostgreSQL)。
2.只有一个匹配类型。如:在应用程序设置上下文中只有一类 DataSource。
如果满足这些条件,Cloudfoundry就会自动拦截当地数据源,并利用Cloudfoundry服务取代Spring context中设置的服务。
The only thing left to do is to answer positively when vmc asks for service binding when the application is uploaded to the cloud. 接下来只需做一件事,就是当应用程序上传到云中,vmc要求绑定服务时,给出确定的回答。
对于更复杂的应用程序来说,Spring 3.1预期加入特殊的云命名空间并和运行配置支持。
Earlier this year VMware released CloudFoundry, an opesource PAAS solution, with initial support for several services such as MongoDB, MySQL, and Redis. Lately it has added PostgreSQL and RabbitMQ to the list of cloud services that applications can depend on, as well as offering a Micro edition of the cloud that can run on a single workstation.
PostgreSQL is an interesting addition since it is a full featured traditional database with a different audience than MySQL or MongoDB users. To accommodate PostgreSQL, cloudfoundry does not use a vanilla version but instead a customized one based on vFabric as explained in the official blog post.
The same post gives an example of using PostgreSQL in a Java project built with Spring Roo. Here we illustrate an alternative approach where the Java application is a Spring based WAR utilizing JPA.
Assume that your application has a Spring context setup as below:
This application can be uploaded to CloudFoundry completely unchanged thanks to the auto-configuration feature of CloudFoundry.
PostgreSQL (as well as MySQL and the rest of supported services) takes advantage of the auto-configuration feature offered by Cloudfoundry. Under certain conditions a local Java Application that uses PostgreSQL can be uploaded to CloudFoundry with zero code changes.
Auto-configuration employs the Spring Core container and takes places when both of the following are true:
1.There may exactly be only one service of a given service type. For example, you may bind only one relational database service (MySQL or PostgreSQL) to an application.
2.There may exactly be only one bean of the matching type. For example, you may have only one DataSource bean in the application context.
If these requirements hold then Cloudfoundry will automatically intercept your local datasource and use the Cloudfoundry service instead of what is set in the Spring context.
For more complex applications, Spring 3.1 is expected to add a special cloud namespace and runtime profile support. |