我不明白,为什么找不到类CategoryFormatter.java。 (IntellijIdea + Tomcat 9 + Maven)

mvc-core.config

<mvc:annotation-driven conversion-service="conversionService"/>
      <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
            <property name="formatters">
                <set>
                    <bean class="diplom.whatsnew.server.web.CategoryFormatter"/>
                </set>
            </property>
        </bean>


WhatsNewService.java

package diplom.whatsnew.server.service;

import diplom.whatsnew.server.model.*;
import org.springframework.dao.DataAccessException;

import javax.jws.soap.SOAPBinding;
import java.util.Collection;



public interface WhatsNewService {

    Collection<Category> findCategory() throws DataAccessException;

    City findCityById(int id) throws DataAccessException;

    Places findPlaceById(int id) throws DataAccessException;

    void savePlace(Places place) throws DataAccessException;

    void deletePlace(Places place) throws DataAccessException;

    void saveEvents(Events events) throws DataAccessException;

    void deleteEvente(Events events) throws DataAccessException;

    void saveCity(City city) throws DataAccessException;

    Collection<Users> findUsers() throws  DataAccessException;

    Collection<City> findCityByName(String name) throws  DataAccessException;
}


CategoryFormatter.java

package diplom.whatsnew.server.web;
import diplom.whatsnew.server.model.Category;
import diplom.whatsnew.server.service.WhatsNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.Formatter;

import java.text.ParseException;
import java.util.Collection;
import java.util.Locale;
public class CategoryFormatter implements Formatter<Category> {

private final WhatsNewService whatsNewService;

@Autowired
public CategoryFormatter(WhatsNewService whatsNewService)
{
    this.whatsNewService=whatsNewService;
}

@Override
public String print(Category category, Locale locale)
{
    return  category.getName();
}

@Override
public Category parse(String text,Locale locale) throws ParseException{
    Collection<Category> findCategory=this.whatsNewService.findCategory();
    for(Category category: findCategory){
        if(category.getName().equals(text)){
            return category;
        }
    }
    throw new ParseException("type not found: " + text, 0);
}

}


web.xml

 <?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0" metadata-complete="true">

<display-name>Spring WhatsNew</display-name>
<description>Spring WhatsNew sample application</description>


<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>jdbc</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/business-config.xml, classpath:spring/tools-config.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<servlet>
    <servlet-name>whatsnew</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/mvc-core-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>whatsnew</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<servlet>
    <servlet-name>dandelionServlet</servlet-name>
    <servlet-class>com.github.dandelion.core.web.DandelionServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dandelionServlet</servlet-name>
    <url-pattern>/dandelion-assets/*</url-pattern>
</servlet-mapping>


<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


<filter>
    <filter-name>dandelionFilter</filter-name>
    <filter-class>com.github.dandelion.core.web.DandelionFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>dandelionFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


<filter>
    <filter-name>datatables</filter-name>
    <filter-class>com.github.dandelion.datatables.core.web.filter.DatatablesFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>datatables</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>


错误


org.springframework.beans.factory.BeanCreationException:错误
在类路径中创建名称为“ conversionService”的bean
资源[spring / mvc-core-config.xml]:无法创建内部bean
类型的'diplom.whatsnew.server.web.CategoryFormatter#19b8e7d5'
设置bean时[diplom.whatsnew.server.web.CategoryFormatter]
具有键[0]的属性“格式器”;嵌套异常为
org.springframework.beans.factory.CannotLoadBeanClassException:无法
查找bean的类[diplom.whatsnew.server.web.CategoryFormatter]
名称为“ diplom.whatsnew.server.web.CategoryFormatter#19b8e7d5”
在类路径资源[spring / mvc-core-config.xml]中定义;嵌套的
异常是java.lang.ClassNotFoundException:
diplom.whatsnew.server.web.CategoryFormatter

org.springframework.beans.factory.CannotLoadBeanClassException:无法
查找bean的类[diplom.whatsnew.server.web.CategoryFormatter]
名称为“ diplom.whatsnew.server.web.CategoryFormatter#77b6dc78”
在类路径资源[spring / mvc-core-config.xml]中定义;嵌套的
异常是java.lang.ClassNotFoundException:
diplom.whatsnew.server.web.CategoryFormatter

java.lang.ClassNotFoundException:
diplom.whatsnew.server.web.CategoryFormatter

最佳答案

这里是bean定义的问题,Spring等待无参数构造函数:

<bean class="diplom.whatsnew.server.web.CategoryFormatter"/>


在你的课上,你有一个带有autowired参数的

@Autowired
public CategoryFormatter(WhatsNewService whatsNewService)
{
    this.whatsNewService=whatsNewService;
}


所以没有创建bean

修复xml声明中的更改(mvc-core-config.xml)

<bean class="diplom.whatsnew.server.web.CategoryFormatter">
      <constructor-arg ref="whatsNewService"/>
</bean>

10-04 13:02