我正在尝试从Spring Framework中的属性文件读取属性
我正在使用SpringToolSuite MVC模板编写我的代码,如下所示
这是我的项目目录结构
enter image description here
我正进入(状态org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.iodevops.uhc.prop.JavaMailPropImpl] for bean with name 'mailProp' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: com.iodevops.uhc.prop.JavaMailPropImpl
尽管定义了java类。
HomeController.java
package com.iodevops.uhc;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.iodevops.uhc.prop.JavaMailPropImpl;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
@RequestMapping(value="/mailProp", method=RequestMethod.GET)
public String mailProp(@Validated JavaMailPropImpl mailProp,Model model){
model.addAttribute("mailPropValues", mailProp.toString());
return "mailProp";
}
}
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<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>
<!--
<beans:bean id="mailProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location" value="classpath:mail.properties" />
</beans:bean>
-->
<beans:bean id="mailProp" class="com.iodevops.uhc.prop.JavaMailPropImpl">
<beans:property name="host" value="${smtp.host}" />
<beans:property name="port" value="${smtp.port}" />
<beans:property name="user" value="${smtp.user}" />
<beans:property name="pass" value="${smtp.pass}" />
</beans:bean>
<context:component-scan base-package="com.iodevops.uhc" />
</beans:beans>
JavaMailPropImpl.java:
package com.iodevops.uhc.prop;
public class JavaMailPropImpl {
private String host;
private String port;
private String user;
private String pass;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString(){
return host + "|" + port + "|" + user + "|"+ pass;
}
}
错误信息
ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.iodevops.uhc.prop.JavaMailPropImpl] for bean with name 'mailProp' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: com.iodevops.uhc.prop.JavaMailPropImpl
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.iodevops.uhc.prop.JavaMailPropImpl] for bean with name 'mailProp' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: com.iodevops.uhc.prop.JavaMailPropImpl
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.creat....
....
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.iodevops.uhc.prop.JavaMailPropImpl] for bean with name 'mailProp' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: com.iodevops.uhc.prop.JavaMailPropImpl
at ....
....
....
最佳答案
试试这样的Spring配置:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:project.properties</value>
</list>
</property>
</bean>