gpt4 book ai didi

java - 使用名称创建 bean 时出错

转载 作者:行者123 更新时间:2023-11-30 10:34:27 33 4
gpt4 key购买 nike

我正在创建一个基于 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>

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

项目 View (除 Web 模块外的所有项目):

/image/hvyGL.jpg

项目 View (网络模块):

/image/YBbgU.jpg )

最佳答案

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

或者你可以在Web模块中mvn clean install -am,这样它将构建依赖的模块。

请将服务模块中的打包改为jar,或者直接删除,默认打包为jar。

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

关于java - 使用名称创建 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41651440/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com