gpt4 book ai didi

java - org.springframework.beans.factory.NoSuchBeanDefinitionException : No bean named 'itemAttrService' is defined

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:28 25 4
gpt4 key购买 nike

我正在尝试 Spring + Hibernate + ZK 框架中的示例。我通过pom.xml文件打包一个war文件。之后,当我在服务器上运行程序时,它会抛出以下异常

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'itemAttrService' is defined

这是 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nagarro</groupId>
<artifactId>ZKSpringHibernateExample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ZKSpringHibernateExample</name>
<url>http://maven.apache.org</url>

<repositories>
<repository>
<id>java.net</id>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<properties>
<org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- Spring framework -->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>



<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>

<!-- Hibernate framework -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>

<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<!-- <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId>
<version>1.0.2</version> </dependency> -->

<!-- Hibernate library dependecy start -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>

<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<!-- Hibernate library dependecy end -->

<!-- ZK Dependency start -->
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zkplus</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zhtml</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zkbind</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zul</artifactId>
<version>6.0.0</version>
</dependency>
<!-- ZK Dependency Ends -->

</dependencies>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>DataSyncTreePOC</display-name>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/BeanLocations.xml,

</param-value>
</context-param>
<servlet>
<description>
The ZK loader for ZUML pages</description>
<servlet-name>zkLoader</servlet-name>
<servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
<init-param>
<param-name>update-uri</param-name>
<param-value>/zkau</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>
The asynchronous update engine for ZK</description>
<servlet-name>auEngine</servlet-name>
<servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zul</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>auEngine</servlet-name>
<url-pattern>/zkau/*</url-pattern>
</servlet-mapping>
<!-- <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/error/error.zul</location>
</error-page> -->
<welcome-file-list>
<welcome-file>pages/showDbData.zul</welcome-file>

</welcome-file-list>
</web-app>

我正在创建这样的bean

ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/config/BeanLocations.xml");
itemAttrService = (ItemAttributeService)appContext.getBean("itemAttrService");

我能够构造 appContext 但不能构造 itemAttrService

itemAttrService 在同一文件中声明如下

@Autowired
private ItemAttributeService itemAttrService

我还在我的服务类中使用了 @Service 注释,如下所示

package com.nagarro.service.impl;

@Service("itemAttrService")
public class ItemAttributeServiceImpl implements ItemAttributeService{

code....
}

看,此类位于 com.nagarro 包下,因此应该自动扫描它。但是,这里的情况并非如此。请告知我是否在 pom.xmlweb.xml

中遗漏了一些重要的内容

最佳答案

您的spring/config/BeanLocations.xml需要包含如下行:

<context:component-scan base-package="com.nagarro.service.impl" />

关于java - org.springframework.beans.factory.NoSuchBeanDefinitionException : No bean named 'itemAttrService' is defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14917264/

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