获取使用名称创建bean的错误

时间:2017-01-14 15:07:48

标签: java spring maven spring-mvc

我正在创建一个基于Spring MVC Maven模块的项目,因此我将每个层划分为一个maven模块。当我尝试运行一个基本站点时,代码编译得很好,但我得到错误:

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addressController' defined in file [C:\Program Files\Apache Software Foundation\Tomcat 8.0\wtpwebapps\croz-web\WEB-INF\classes\hr\croz\bspoljaric\controller\AddressController.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect bean class [hr.croz.bspoljaric.controller.AddressController] for resource metadata: could not find class that it depends on
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:525)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4851)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Failed to introspect bean class    [hr.croz.bspoljaric.controller.AddressController] for resource metadata: could not find class that it depends on
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:344)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:297)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:953)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
... 20 more
Caused by: java.lang.NoClassDefFoundError: Lhr/croz/bspoljaric/service/AddressService;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Unknown Source)
at java.lang.Class.getDeclaredFields(Unknown Source)
at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:715)
at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:656)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.buildResourceMetadata(CommonAnnotationBeanPostProcessor.java:361)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:340)
... 23 more
Caused by: java.lang.ClassNotFoundException:      hr.croz.bspoljaric.service.AddressService
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
... 30 more

AddressDao.java

package hr.croz.bspoljaric.dao;

import java.util.List;

import hr.croz.bspoljaric.entity.Address;

public interface AddressDAO {

public void save(Address address);

public void delete(int addressID);

public Address get(int contactId);

public List<Address> getAll();
}

AddressDaoImpl.java

import java.util.List;

import org.springframework.stereotype.Repository;

import hr.croz.bspoljaric.dao.AddressDAO;
import hr.croz.bspoljaric.entity.Address;

@Repository
public class AddressDAOImpl implements AddressDAO{

@Override
public void save(Address address) {
    // TODO Auto-generated method stub

}

@Override
public void delete(int addressID) {
    // TODO Auto-generated method stub

}

@Override
public Address get(int contactId) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public List<Address> getAll() {
        List<Address> as = new ArrayList<>();
        Address a = new Address();
        a.setId(1l);
        a.setStreet("MAKSIMIRSKA");
        a.setStreetNo("2");
        a.setCity(null);
        as.add(a);
        System.out.println("TEST: " + as.get(1));
        return as;

}

}

AddressService.java

package hr.croz.bspoljaric.service;

import java.util.List;

import hr.croz.bspoljaric.entity.Address;

public interface AddressService {

public List<Address> getAll();

}    

AddressServiceImpl.java

package hr.croz.bspoljaric.serviceimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import hr.croz.bspoljaric.dao.AddressDAO;
import hr.croz.bspoljaric.entity.Address;
import hr.croz.bspoljaric.service.AddressService;

@Service
public class AddressServiceImpl implements AddressService {
@Autowired
AddressDAO dao;

public List<Address> getAll() {
    return dao.getAll();

}
}

AddressController.java

package hr.croz.bspoljaric.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;



import hr.croz.bspoljaric.service.AddressService;


@Controller
@RequestMapping("/test")
public class AddressController {

@Autowired
AddressService ads;

@RequestMapping(value = "/getAll", method = RequestMethod.GET)
public String getAllAddresses(Model model)
{
  model.addAttribute("addresses", ads.getAll());
  System.out.println("TEST!");
  return "addressesListDisplay";

 }


 }

的web.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <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_2_5.xsd"
version="2.5">
<display-name>Croz Web Application</display-name>

<servlet>
    <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

弹簧servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context/
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="hr.croz.bspoljaric" />


<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

项目视图(除了网络模块以外):

https://imgur.com/a/mgBpd

项目视图(网络模块):

https://imgur.com/a/4gtp8

2 个答案:

答案 0 :(得分:1)

在单独运行Web模块之前,您需要在父模块中mvn clean install将其他模块安装到本地存储库。

或者您可以在网络模块中mvn clean install -am,以便它构建依赖的模块。

请将包装更改为服务模块中的jar,或者只是删除它,默认包装为jar。

<packaging>jar</packaging>
<name>croz-service</name>

答案 1 :(得分:0)

转到包含项目pom文件的文件夹。然后获取cmd并运行

mvn clean install

enter image description here如果失败,请在删除目标文件夹后尝试