简体   繁体   中英

CannotLoadBeanClassException while trying to read properties file in spring

I am trying to read properties from a properties file in Spring Framework I amusing SpringToolSuite MVC template my code as follows

here is my project directory structure enter image description here

I am getting

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

though there is the java class defined.

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 Message

        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 ....
    ....
    ....

Try something like this Spring config :

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

You could do something like this: Maven dependency

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

Add the import.

import javax.annotation.Resource;

...

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

In your spring xml application context:

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

You will need this namespace

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM