基于Java的配置
package com.mgroup.init;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import com.mgorup.dao.EmployeeDao;
import com.mgroup.controller.EmployeeController;
import com.mgroup.services.EmployeeService;
@Configuration
public class MyConfig {
@Bean
@Scope("prototype")
public EmployeeService employeeServiceBean()
{
//constructor injection on a class StoreService. well it was, nto it is autowire
return new EmployeeService();//(storeDaoBean());
}
@Bean
@Scope("prototype")
public EmployeeDao employeeDaoBean()
{
return new EmployeeDao();
}
}
控制器类
package com.mgroup.controller;
import javax.annotation.Resource;
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 com.mgroup.pojo.Patron;
import com.mgroup.services.EmployeeService;
@Controller
public class EmployeeController {
@Autowired
EmployeeService employeeServiceBean;
/* Loading Home Page */
@RequestMapping("/Login.html")
public String loadLoginPage() {
return "Welcome";
}
}
错误代码
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.fdmgroup.services.EmployeeService com.fdmgroup.controller.EmployeeController.employeeServiceBean; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.fdmgroup.services.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
我还有EmployeeService和EmployeeDao。他们在自动装配方面做得很好。即见下文。当我尝试在Controller类中使用autowire注释注入EmployeeService时遇到的问题我得到上面的错误。是因为我有基于Java的配置类和xml配置类,它有Component-Scan?
@Service
public class EmployeeService implements EmployeeServicesInterface{
@Autowired
EmployeeDao employeeDaoBean;
@Override
这是我的servlet.xml。我删除了括号中的模式以保留一些空间。
<beans >
<!-- Scan the package containing annotations -->
<context:annotation-config />
<context:component-scan base-package="com.mgroup.controller" />
<!-- jspViewResolver bean – This bean defined view resolver for spring mvc. For this bean we also set prefix as “/WEB-INF/jsp/”
and suffix as “.jsp”. Thus spring automatically resolves the JSP from WEB-INF/jsp folder and assigned suffix .jsp to it. -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
这是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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>library</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>*.html</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
您是否配置了配置应用程序的组件或bean的@Configuration类?
您可以在web.xml文件中尝试加载配置类。
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.mgroup.init</param-value>
</context-param>
答案 1 :(得分:0)
您的EmployeeService
超出了套餐扫描范围。尝试将base-package
更改为"com.fdmgroup"
答案 2 :(得分:0)
确保在scaned包中有EmployeeService。尝试改变
<context:component-scan base-package="com.mgroup.controller" />
到
<context:component-scan base-package="com.mgroup" />