Revision 2bec70cd
Added by Andreas Kohlbecker over 6 years ago
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/opt/config/DataSourceConfigurer.java | ||
---|---|---|
26 | 26 |
import org.hibernate.dialect.MySQL5MyISAMUtf8Dialect; |
27 | 27 |
import org.hibernate.dialect.PostgreSQL82Dialect; |
28 | 28 |
import org.springframework.beans.BeansException; |
29 |
import org.springframework.beans.factory.annotation.Autowired; |
|
29 | 30 |
import org.springframework.beans.factory.xml.XmlBeanFactory; |
30 | 31 |
import org.springframework.context.annotation.Bean; |
31 | 32 |
import org.springframework.context.annotation.Configuration; |
33 |
import org.springframework.context.annotation.Lazy; |
|
34 |
import org.springframework.core.env.ConfigurableEnvironment; |
|
35 |
import org.springframework.core.env.MutablePropertySources; |
|
36 |
import org.springframework.core.env.PropertiesPropertySource; |
|
32 | 37 |
import org.springframework.core.io.FileSystemResource; |
33 | 38 |
import org.springframework.jndi.JndiObjectFactoryBean; |
34 | 39 |
|
... | ... | |
43 | 48 |
|
44 | 49 |
/** |
45 | 50 |
* The <code>DataSourceConfigurer</code> can be used as a replacement for a xml configuration in the application context. |
51 |
* <p> |
|
52 |
* The id of the loaded data source bean aka the <b>cdm instance name</b> is put into the <b>Spring environment</b> from where it can be retrieved using the |
|
53 |
* key {@link CDM_DATA_SOURCE_ID}. |
|
54 |
* <p> |
|
46 | 55 |
* Enter the following in your application context configuration in order to enable the <code>DataSourceConfigurer</code>: |
47 | 56 |
* |
48 | 57 |
<pre> |
... | ... | |
82 | 91 |
protected static final String HIBERNATE_SEARCH_DEFAULT_INDEX_BASE = "hibernate.search.default.indexBase"; |
83 | 92 |
protected static final String CDM_BEAN_DEFINITION_FILE = "cdm.beanDefinitionFile"; |
84 | 93 |
|
94 |
/** |
|
95 |
* key for the spring environment to the datasource bean id aka instance name |
|
96 |
*/ |
|
97 |
public static final String CDM_DATA_SOURCE_ID = "cdm.dataSource.id"; |
|
98 |
|
|
85 | 99 |
/** |
86 | 100 |
* Attribute to configure the name of the data source as set as bean name in the datasources.xml. |
87 | 101 |
* This name usually is used as the prefix for the webapplication root path. |
... | ... | |
120 | 134 |
beanDefinitionFile = filename; |
121 | 135 |
} |
122 | 136 |
|
137 |
@Autowired |
|
138 |
private ConfigurableEnvironment env; |
|
123 | 139 |
|
124 | 140 |
private String dataSourceId = null; |
125 | 141 |
|
... | ... | |
135 | 151 |
|
136 | 152 |
|
137 | 153 |
@Bean |
154 |
@Lazy(false) |
|
138 | 155 |
public DataSource dataSource() { |
139 | 156 |
|
140 | 157 |
String beanName = findProperty(ATTRIBUTE_DATASOURCE_NAME, true); |
... | ... | |
155 | 172 |
return null; |
156 | 173 |
} |
157 | 174 |
|
175 |
MutablePropertySources sources = env.getPropertySources(); |
|
176 |
Properties props = new Properties(); |
|
177 |
props.setProperty(CDM_DATA_SOURCE_ID, dataSourceId); |
|
178 |
sources.addFirst(new PropertiesPropertySource("cdm-datasource", props)); |
|
179 |
|
|
158 | 180 |
// validate correct schema version |
159 | 181 |
Connection connection = null; |
160 | 182 |
try { |
Also available in: Unified diff
data source bean id aka instance name now available from the Spring environment