使用名称' conversionService'

时间:2016-05-12 10:47:48

标签: java spring maven spring-mvc intellij-idea

我不明白,为什么没有找到类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:错误   使用名称&#39; conversionService&#39;创建bean在类路径中定义   resource [spring / mvc-core-config.xml]:无法创建内部bean   &#39;#diplom.whatsnew.server.web.CategoryFormatter 19b8e7d5&#39;类型   设置bean时[diplom.whatsnew.server.web.CategoryFormatter]   property&#39; formatters&#39;用键[0];嵌套异常是   org.springframework.beans.factory.CannotLoadBeanClassException:不能   找到bean的类[diplom.whatsnew.server.web.CategoryFormatter]   名称为&#39; diplom.whatsnew.server.web.CategoryFormatter#19b8e7d5&#39;   在类路径资源[spring / mvc-core-config.xml]中定义;嵌套   异常是java.lang.ClassNotFoundException:   diplom.whatsnew.server.web.CategoryFormatter

     

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

     

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

3 个答案:

答案 0 :(得分:0)

问题在于bean定义Spring等待no-arg构造函数:

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

在你的课堂上你有一个自动参数

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

答案 1 :(得分:0)

我想这就是问题。

我看了看你的项目。

我认为该类的名称是PlaceCategoryFormatter,而不是包 diplom.whatsnew.server.web

下的CategoryFormatter

请仔细检查。

答案 2 :(得分:0)

我已经通过重建maven项目解决了这个问题