当前位置: 首页 > 知识库问答 >
问题:

创建名为“转换服务”的bean时出错

史谦
2023-03-14

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);
}

}
 <?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:创建类路径资源[spring/mvc-core-config.xml]中定义的名称为“转换服务”的bean时出错:在使用键[0]设置bean属性“formatters”时,无法创建类型为[diplom.whatsnew.server.web.categoryformatter]的内部bean“diplom.whatsnew.server.web.categoryformatter#19B8E7D5”;嵌套异常是org.springframework.beans.factory.CannotLoadBeanClassException:找不到类路径资源[spring/mvc-core-config.xml]中定义的名为'diplom.whatsnew.server.web.categoryformatter#19b8e7d5'的bean的类[diplom.whatsnew.server.web.categoryformatter];嵌套异常是java.lang.ClassNotFoundException:diplom.whatsnew.server.web.CategoryFormatter

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

ClassNotFoundException:diplom.whatsnew.server.web.CategoryFormatter

共有1个答案

侯令雪
2023-03-14

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

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

在你的课上,你有一个论点很少

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

因此不会创建bean

<bean class="diplom.whatsnew.server.web.CategoryFormatter">
      <constructor-arg ref="whatsNewService"/>
</bean>
 类似资料:
  • 我有一个实体类InAppNotification。看起来像这样的java: 我使用JPA来处理数据库查询,这就是JPA接口的定义: 这是我application.properties的样子: 但是,当我试图在构建后通过运行 来打包应用程序时,我会遇到以下问题: 尝试调用不存在的方法。尝试从以下位置进行:javax.el.ELManager.getExpress sionWorks(ELManage

  • 在将project从Spring Boot版本从1.2.3.release迁移到1.3.0.release之后,我已经开始得到以下异常。 创建类路径资源[org/springframework/boot/autoconfigure/admin/springapplicationadminjmxautoconfiguration.class]中定义的名为'Spring ApplicationAdmi

  • 我遵循本教程将消息发送到azure服务队列:https://docs.microsoft.com/en-us/azure/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-service-bus 到我现有的spring boot应用程序,但我得到以下错误: 用户类别: 控制器类: pom xml: 添加它

  • 我试图将弹性搜索集成到spring-boot应用程序中,但我得到了这个*创建名为“client”的bean时出错*异常,不确定是什么地方出了问题,因为我之前找不到任何类似的线索...非常感谢你为大家指路。这是mu elasticsearch配置: 这是我的主要应用程序: 我的pom.xml: 这是te异常跟踪:

  • 我有这些错误编译,我不知道我哪里错了,我只有这3个类

  • 我正在尝试在spring boot应用程序中使用SQLite。但是应用程序不能创建下面的bean。 org.springframework.boot.autocigure.orm.jpa.hibernatejaconfiguration 我该怎么办?我在这个站点上搜索了相关的问题,但是找不到一个合适的。 如下所示。 4.0.0 org.springframework.Boot spring-boo