2016-11-07 29 views
0

我想讀從屬性的屬性在Spring文件框架 我會哄SpringToolSuite MVC模板我的代碼如下CannotLoadBeanClassException嘗試讀取性能在春季文件

這裏是我的項目的目錄結構 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 .... 
    .... 
    .... 
+0

轉至項目 - 清理項目>清潔 –

+0

我試過清潔和reb練習..但沒用。 –

回答

1

嘗試是這樣的Spring配置:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath*:project.properties</value> 
     </list> 
    </property> 
</bean> 
+0

只有當我的bean中存在ArrayList類型的屬性時才需要此類型的結構。其他明智的我會得到org.springframework.beans.TypeMismatchException:未能將類型'java.util.ArrayList'的屬性值轉換爲所需的類型'org.springframework.core.io.Resource' –

0

你可以做這樣的事情: Maven的依賴

<dependency> 
     <groupId>javax.annotation</groupId> 
     <artifactId>javax.annotation-api</artifactId> 
     <version>1.2</version> 
    </dependency> 

添加導入。

import javax.annotation.Resource; 

...

@Resource (name="propertiesMapName") 
public Properties someProps; 

在Spring XML應用程序上下文:

<util:properties id="propertiesMapName" location="classpath:yourFile.properties"/> 

你會需要這個命名空間

xmlns:util="http://www.springframework.org/schema/util" 

http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.1.xsd 
+0

我得到的錯誤是org.springframework .beans.factory.CannotLoadBeanClassException:無法找到我的屬性模型類的類... –

+0

您是否添加了Maven依賴性? – cralfaro

+0

Ofcourse,我做了 –