我有两个项目,CarpoolDB和拼车。
CarpoolDB:包含后端的东西
拼车应用程序上下文。xml
<context:annotation-config />
<context:component-scan base-package="com.onmobile" />
<context:property-placeholder location="classpath:server.properties" />
server.properties
cm.db.driverClassName=com.mysql.jdbc.Driver
cm.db.url=jdbc:mysql://localhost:3306/carpool1
cm.db.username=abc
cm.db.password=xyz
我做了一罐carpoolDB和地方拼车应用程序
拼车:包含UI东西和后端联系人carpoolDB jar,并具有
carpool-application-context1.xml
<import resource="classpath:carpool-application-context.xml"/>
<context:annotation-config />
<context:component-scan base-package="com.onmobile.carpool.authentication" />
spring-servlet.xml
<context:property-placeholder location="classpath:carpool.properties" />
<context:annotation-config />
<context:component-scan base-package="com.onmobile.carpool.controller, com.onmobile.carpool.util" />
拼车。性质
cm.email.sender.mail.smtp.host=mail.on.com
现在,我有一个类com.onmobile.carpool.util.EailSender,它有一个属性smtpHost,并希望值由Spring使用@Value注入,但它没有被注入。
@Controller
public class EmailSender {
public static final Log logger = LogFactory.getLog(EmailSender.class);
@Value("${cm.email.sender.mail.smtp.host}")
private String smtpHost;
}
我得到的错误是
java.lang.IllegalArgumentException: Could not resolve placeholder 'cm.email.sender.mail.smtp.host'
拼车。属性存在于src文件夹中。
为什么不选择cm。电子邮件发件人。邮政smtp。来自拼车的主人。属性文件。是否与jar文件中存在的属性文件有任何关系。
实际上属性文件被加载,因为我在日志中看不到,比如文件没有找到,但是字段没有自动更新。
删除导入后发布更新的完整配置文件
网状物xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/carpool-application-context1.xml
/WEB-INF/applicationContext-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/jsp/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
carpool-application-context1.xml
<!-- Configure annotated beans -->
<context:annotation-config />
<context:component-scan base-package="com.onmobile.carpooldb.db" />
<context:property-placeholder location="classpath:carpool.properties" />
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<beans:property name="driverClassName"><beans:value>${cm.db.driverClassName}</beans:value></beans:property>
<beans:property name="url"><beans:value>${cm.db.url}</beans:value></beans:property>
<beans:property name="username"><beans:value>${cm.db.username}</beans:value></beans:property>
<beans:property name="password"><beans:value>${cm.db.password}</beans:value></beans:property>
<beans:property name="testOnBorrow"><beans:value>true</beans:value></beans:property>
<beans:property name="testOnReturn"><beans:value>true</beans:value></beans:property>
<beans:property name="validationQuery"><beans:value>select 1</beans:value></beans:property>
<beans:property name="maxIdle"><beans:value>-1</beans:value></beans:property>
<beans:property name="maxActive"><beans:value>-1</beans:value></beans:property>
<beans:property name="maxOpenPreparedStatements"><beans:value>-1</beans:value></beans:property>
<beans:property name="maxWait"><beans:value>30000</beans:value></beans:property>
</beans:bean>
//session factory bean and other configuration
spring-servlet.xml
<context:property-placeholder location="classpath:carpool.properties" />
<context:annotation-config />
<context:component-scan base-package="com.onmobile.carpool.authentication, com.onmobile.carpool.controller, com.onmobile.carpool.util" />
<!-- Declare a view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
拼车。性质
cm.db.driverClassName=com.mysql.jdbc.Driver
cm.db.url=jdbc:mysql://localhost:3306/carpool
cm.db.username=abc
cm.db.password=xyz
user.profile.pic.base.folder=D:\\carpoolRepository\\carpoolProfilePicUpload
google.places.autocomplete.response.xml.base.folder=D:\\carpoolRepository\\googleMapXML
#EMAIL - FORGOT PASSWORD
cm.email.sender.mail.smtp.host=mail.on.com
我试图注入cm的价值。电子邮件发件人。邮政smtp。上面提到的EmailSender“smtpHost”属性方式中的主机,当我阅读它时,它显示为null。其他属性,如cm。db。在carpool-application-context1中正确注入driverClassName等。xml。
您有
在servlet上下文(spring-servlet.xml)中有一个属性占位符配置器。属性占位符配置器(由
上下文:属性占位符
标记定义)是bean后处理器,基于每个容器工作,因此它们不能修改它们未定义的上下文的bean定义。所以它不能修改在根上下文中声明的控制器实例的bean定义(carpool-application-context.xml,carpool-application-context1.xml)。因此,由于双重扫描,您的控制器将被创建两次——在根和servlet上下文中,并且只有一次被正确的占位符配置器处理。
作为修复,您可以在组件扫描中使用过滤器表达式,仅在
springservlet中拾取
/@Controller
注释类。xml,并将其从拼车应用程序上下文中排除。xmlcarpool-application-context1。xml
有关过滤器的示例,请参阅@Service构造两次
请保持您的Spring配置简单,您的配置非常令人费解。
更新您正在混淆控制器(用
@Controller
注释,我认为应该更好地放入aaa.bbb.controllers
包)和服务,这些服务应该在util@Service
注释>包。我的建议是将服务移动到根Web应用上下文(carpool-application-context1.xml),并将属性占位符配置器声明放在那里。
问题内容: 我在程序中需要一个配置文件来存储一些信息,我看到了一些属性文件的示例,并试图使用它们,但是每当在NetBeans上尝试第二行时,我都会得到“ Package sortConfig不存在”,“ >预期”和“类型非法开始”。 问题是我已经看到大约10个示例都以相同的方式执行此操作,而我不知道发生了什么。 任何帮助,将不胜感激 我的.java类和我的属性文件位于src的同一包文件夹中 问题答
我正在为我的Spring启动应用程序编写联调,但当我尝试使用@TestProperty tySource覆盖一些属性时,它正在加载上下文xml中定义的属性文件,但不会覆盖注释中定义的属性。
我在尝试添加
问题内容: 我在加载时遇到问题 在同一个文件夹中。 我在dist文件夹中有一个myProject.jar,在conf文件夹中有test.xml和test.properties。 要加载xml,我正在使用 但是我在加载属性文件时遇到问题 属性文件没有任何作用。 任何帮助表示赞赏。 问题答案: 为什么不取出文件并使用 上面的代码将获取属性文件并将其加载到属性对象中。
我有一个Spring Boot应用程序如下: 我正在使用spring配置文件,并根据活动配置文件加载一个正确的特定环境文件:utils-local.properties、utils-dev.properties等。 当通过application.properties(spring)设置配置文件时,例如spring.profiles.active=local all工作良好,将加载正确的文件(uti
问题内容: 我希望对我在Spring中涉及到属性文件的问题有所帮助。所以我的设置是这样的: opto-mapping.properties –该文件位于我的src文件夹中,其中包含针对我的优化资源的翻译,如下所示: 每次运行“优化”构建时,都会更新此属性文件。然后我用 将属性文件导入所需的jsp中。然后通过使用以下内容引用内容: 除了属性文件需要重新启动tomcat重启外,所有这些工作都很漂亮。我