没有定义名为“PlaceDAO”的 bean
Posted
技术标签:
【中文标题】没有定义名为“PlaceDAO”的 bean【英文标题】:No bean named 'PlaceDAO' is defined 【发布时间】:2015-12-13 23:37:16 【问题描述】:我正在创建一个简单的应用程序,它应该使用 spring MVC 从数据库中打印一条记录。我设法到达这里,但现在我得到一个例外,上面写着:
SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/springProject] threw exception [Request processing failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'PlaceDAO' is defined] with root cause
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'PlaceDAO' is defined
这是我的代码
Spring-Module.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<import resource="database/Spring-Datasource.xml" />
<import resource="place/Spring-Place.xml" />
</beans>
Spring-Place.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="placeDAO" class="springProject.springProject.model.JdbcPlaceDAO">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Spring-Datasource.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/webservisdb" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
</beans>
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springProject</groupId>
<artifactId>springProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springProject</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.6</java.version>
<spring.version>3.1.0.RELEASE</spring.version>
<cglib.version>2.2.2</cglib.version>
</properties>
<dependencies>
<!-- Spring core & mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>$spring.version</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>$spring.version</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<!-- CGLib for @Configuration -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>$cglib.version</version>
<scope>runtime</scope>
</dependency>
<!-- Servlet Spec -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>springsource-milestones</id>
<name>SpringSource Milestones Proxy</name>
<url>https://oss.sonatype.org/content/repositories/springsource-milestones</url>
</repository>
</repositories>
<build>
<finalName>springProject</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>$java.version</source>
<target>$java.version</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
以及发生异常的我的控制器:
@RequestMapping(value="/getPlace")
public ModelAndView getPlace(HttpServletResponse response) throws IOException
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
placeDAO = (PlaceDAO) context.getBean("PlaceDAO");
Place p1 = placeDAO.getPlace(1);
ModelAndView model=new ModelAndView("places/getPlace");
model.addObject("text", p1.name);
return model ;
最后是文件层次结构:
【问题讨论】:
Check this out 附带问题:为什么要创建自己的 SpringApplicationContext
?您不应该配置 servlet 容器为您创建它吗?
我创建它只是为了检查我的数据库是否配置正确,在我看来这是最快的检查方式。我当然会删除它并以其他方式删除它。
【参考方案1】:
您的示例中有错字:
context.getBean("PlaceDAO")
应该是:
context.getBean("placeDAO")
【讨论】:
以上是关于没有定义名为“PlaceDAO”的 bean的主要内容,如果未能解决你的问题,请参考以下文章
如何摆脱这个异常“没有定义名为'userDetailsService'的bean”虽然它是在xml文件中定义的?
没有使用休眠和 Spring MVC 定义名为“myUserDetailsService”的 bean
没有名为“myJobPerformable”的 bean 可用
Spring Boot 说没有可用的名为“entityManagerFactory”的bean
Workday Studio - 请求HTTP输出到供应商API接收错误:未定义名为“http-token-auth”的bean