我有以下 root.xml 中定义的dev和prod环境的spring配置文件: -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${driver_class}" />
<property name="jdbcUrl" value="${dbURL}${dbHostName}:${dbPort}/${dbName}?serverTimezone=${dbTimezone}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
<property name="initialPoolSize" value="${initial_pool_size}" />
<property name="minPoolSize" value="${dbPoolMinSize}" />
<property name="maxPoolSize" value="${dbPoolMaxSize}" />
<property name="idleConnectionTestPeriod" value="50" />
<property name="numHelperThreads" value="${numHelperThreads}" />
<property name="maxIdleTime" value="2500"></property>
<property name="testConnectionOnCheckout" value="true"></property>
<property name="preferredTestQuery" value="select 1"></property>
</bean>
<beans profile="dev">
<context:property-placeholder
ignore-resource-not-found="false"
location="classpath*:/META-INF/dev-database.properties,
classpath*:/META-INF/app.properties" />
</beans>
<beans profile="prod">
<context:property-placeholder
ignore-resource-not-found="false"
location="classpath*:/META-INF/prod-database.properties,
classpath*:/META-INF/app.properties" />
</beans>
我的 servlet-context xml 如下: -
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<annotation-driven />
<default-servlet-handler />
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/javascripts/*.js" location="/resources/javascripts/" />
<resources mapping="/javascripts/cloudinary/*.js" location="/resources/javascripts/cloudinary/" />
<resources mapping="/stylesheets/*.css" location="/resources/stylesheets/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.free.muft" />
<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="maxUploadSize" value="10000000" />
</beans:bean>
<beans:bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basename" value="classpath:messages" />
<beans:property name="defaultEncoding" value="UTF-8" />
</beans:bean>
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">${dbDialect}</beans:prop>
<beans:prop key="hibernate.show_sql">${show_sql}</beans:prop>
<beans:prop key="hibernate.autoReconnect">true</beans:prop>
<beans:prop key="hibernate.autoReconnectForPools">true</beans:prop>
<beans:prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</beans:prop>
<beans:prop key="hibernate.jdbc.batch_size">30</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">${hbm2ddl}</beans:prop>
</beans:props>
</beans:property>
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="packagesToScan">
<beans:list>
<beans:value>com.test.entities</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager" />
我可以提取这些属性文件中定义的属性。这些属性正在为xml文件占位符解析。我想在类级别的请求映射中使用一个属性,如下所示,这对我不起作用: -
@RestController
@RequestMapping("${home.url}")
public class CategoryController {
有没有办法实现它?
编辑: - 这是我在启动日志中获得的内容
信息:已映射&#34; {[/ $ {home.url} / test],methods = [GET]}&#34;
PS: - 我使用的是Spring 4.3.3版。
答案 0 :(得分:1)
控制器:
@Controller
@RequestMapping("${hello.url}")
public class DynamicController {
@Value("${hello.url}")
String helloUrl;
@PostConstruct
public void postConstruct(){
System.out.println("postconstruct: "+helloUrl);
}
@RequestMapping("${hello.url}")
public String get(){
return "GET";
}
}
的applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder
ignore-resource-not-found="false"
location="classpath*:/META-INF/app.properties" />
<context:component-scan base-package="xxx" />
<mvc:annotation-driven/>
</beans>
如果您使用maven,请将app.properties
放在src / main / resources / META-INF文件夹中。