java org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有定义名为“leadDAO”的 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25936740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'leadDAO' is defined
提问by Pan
I want to run spring+JDBC+Webservice programme. Getting exception while run this.org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'leadDAO' is defined
我想运行spring+JDBC+Webservice程序。运行 this.org.springframework.beans.factory.NoSuchBeanDefinitionException 时出现异常:未定义名为“leadDAO”的 bean
Please help. I am stuck. Thank you
请帮忙。我被困住了。谢谢
Web.xml
网页.xml
<!-- Processes application requests -->
<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>/lms/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml, /WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Now my spring-servlet.xml
现在我的 spring-servlet.xml
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- declare datasource bean -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3307/lms" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- declare beans -->
<bean id="leadDAO" class="com.varazo.dao.LeadDAOImpl">
<!-- <property name="dataSource" ref="dataSource"></property> -->
</bean>
<bean id="newLeadService" class="com.varazo.service.NewLeadService"/>
</beans>
applicationContext.xml
应用上下文.xml
<import resource="classpath*:spring-servlet.xml"/>
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
For example @Controller and @Service. Make sure to set the correct base-package-->
<context:component-scan base-package="com.varazo.dao" />
<context:component-scan base-package="com.varazo.service" />
<context:component-scan base-package="com.varazo.app" />
<context:component-scan base-package="com.varazo.pojo" />
<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />
</beans>
and bean class:
和豆类:
package com.varazo.pojo;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="newlead")
public class NewLead {
private Long id;
private String firstName;
private String lastName;
private Double money;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
My Service class
我的服务类
package com.varazo.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import com.varazo.dao.LeadDAOImpl;
import com.varazo.pojo.NewLead;
@Service("newLeadService")
public class NewLeadService {
/**
* Retrieves all persons
*/
public List<NewLead> getAll() {
// logger.debug("Retrieving all persons");
System.out.println("Retrieving all persons 1");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring-servlet.xml");
System.out.println("Retrieving all persons 2");
LeadDAOImpl leadDAOImplObj = (LeadDAOImpl) context.getBean("leadDAO");
List<NewLead> lList = new ArrayList<NewLead>();
lList = leadDAOImplObj.listNewLeads();
System.out.println("Retrieving all persons "+lList);
return lList;
}
}
My daoImpl
我的 daoImpl
package com.varazo.dao;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.varazo.pojo.NewLead;
@Repository
public class LeadDAOImpl implements LeadDAO {
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<NewLead> listNewLeads() {
JdbcTemplate select = new JdbcTemplate(dataSource);
return select.query("select ID from llead LIMIT 2", new LeadRowMapper());
}
}
My controller
我的控制器
package com.varazo.app;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.varazo.pojo.NewLead;
import com.varazo.pojo.NewLeadList;
@Controller
@RequestMapping("/lead")
public class LeadController {
@Resource(name="newLeadService")
private com.varazo.service.NewLeadService newLeadService;
@RequestMapping(value = "/newLeads", method = RequestMethod.GET, headers="Accept=application/xml, application/json")
public @ResponseBody com.varazo.pojo.NewLeadList getNewLead() {
// Call service here
NewLeadList result = new NewLeadList();
result.setData(newLeadService.getAll());
System.out.println("*********************LeadController Retrieving all persons ***"+result+"****************");
return result;
}
}
URL:
网址:
/lms-mobile/lms/lead/newLeads
/lms-mobile/lms/lead/newLeads
回答by Manuel Jordan
One:
一:
Why you are declaring the bean twice?
为什么要两次声明 bean?
<bean id="leadDAO" class="com.varazo.dao.LeadDAOImpl">
<!-- <property name="dataSource" ref="dataSource"></property> -->
</bean>
and
和
@Repository
public class LeadDAOImpl implements LeadDAO {
Two:
二:
How you are injecting these two dependencies:
您如何注入这两个依赖项:
@Repository
public class LeadDAOImpl implements LeadDAO {
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
Three:
三:
you can change from
你可以从
<context:component-scan base-package="com.varazo.dao" />
<context:component-scan base-package="com.varazo.service" />
<context:component-scan base-package="com.varazo.app" />
<context:component-scan base-package="com.varazo.pojo" />
To (not very sure, but you can have just one declaration)
To(不太确定,但您可以只有一个声明)
<context:component-scan base-package="com.varazo.dao,com.varazo.service,com.varazo.app, com.varazo.pojo" />
Four:
四:
Why the following from the @Service class?
为什么以下来自@Service 类?
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring-servlet.xml");
Is really a badpractice do that, a @Service must not has knowledge about the Weblayer.
这样做真的是一种不好的做法,@Service 必须不了解Web层。
I suggest change to
我建议改为
@Service("newLeadService")
public class NewLeadService {
private LeadDAO leadDAOImplObj;
@Autowired
public NewLeadService(LeadDAO leadDAOImplObj){
this.leadDAOImplObj = leadDAOImplObj;
}
public List<NewLead> getAll() {
// logger.debug("Retrieving all persons");
System.out.println("Retrieving all persons 1");
System.out.println("Retrieving all persons 2");
List<NewLead> lList = new ArrayList<NewLead>();
lList = leadDAOImplObj.listNewLeads();
System.out.println("Retrieving all persons "+lList);
return lList;
}
Five:
五:
Read all the reference documentation about DI, you are doing the things really wrong. You must declare your dependencies in a Java class based on Interfaces, not through concrete classes. Your @Service class does not implements an interface.
阅读所有关于 DI 的参考文档,你做的事情真的错了。您必须在基于接口的 Java 类中声明您的依赖项,而不是通过具体类。您的 @Service 类没有实现接口。