这是我的spring控制器类KalamController
package com.kalam.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.kalam.model.Employee;
import com.kalam.service.EmployeeService;
@Controller
@RequestMapping("/hello")
public class KalamController {
@Autowired
EmployeeService employeeService;
@RequestMapping("/kalam")
public String showMessage(ModelMap map) {
map.put("dollar", "50 US $");
return "KalamWorld";
}
@RequestMapping("/insertData")
public void InserData() {
Employee emp= new Employee();
emp.setEmpID(7);
emp.setEmpName("Prashant");
emp.setEmpSalary(20000);
emp.setAddress("Vashi");
employeeService.addEmployee(emp);
}
}
Hibernate配置xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- Activate Spring annotation support -->
<context:annotation-config />
<mvc:annotation-driven />
<context:component-scan base-package="com.kalam.controller" />
<context:component-scan base-package="com.kalam.serviceimpl" />
<context:component-scan base-package="com.kalam.service" />
<context:component-scan base-package="com.kalam.daoimpl" />
<context:component-scan base-package="com.kalam.testclass" />
<!-- DataSource configurationt -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/kalamdb"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="mysessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="employeeDaoImpl" class="com.kalam.daoimpl.EmployeeDaoImpl">
<property name="sessionFactory" ref="mysessionFactory" />
</bean>
<bean id="employeeDaoImpl" class="com.kalam.daoimpl.EmployeeDaoImpl" />
<bean id="employeeService" class="com.kalam.service.EmployeeService" />
<bean id="kalamController" class="com.kalam.controller.KalamController" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mysessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
EmployeeServiceImpl.Java档案
package com.kalam.serviceimpl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
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.kalam.dao.EmployeeDao;
import com.kalam.daoimpl.EmployeeDaoImpl;
import com.kalam.model.Employee;
import com.kalam.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
EmployeeDao employeeDao;
public void addEmployee(Employee emp) {
employeeDao.addEmployee(emp);
}
public void updateEmployee(Employee emp) {
}
public void deleteEmployee(Employee emp,int id) {
}
}
最后是
package com.kalam.daoimpl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import com.kalam.dao.EmployeeDao;
import com.kalam.model.Employee;
@Repository
public class EmployeeDaoImpl {
private SessionFactory sessionFactory;
public void addEmployee(Employee emp) {
Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction();
session.save(emp);
tx.commit();
session.close();
}
public void updateEmployee(Employee emp) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.update(emp);
session.getTransaction().commit();
}
public void deleteEmployee(Employee emp, int id) {
Session session = this.sessionFactory.getCurrentSession();
session.beginTransaction();
Employee employee = (Employee) session.get(Employee.class, new Integer(id));
if(null != employee){
session.delete(emp);
}
session.getTransaction().commit();
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
当我运行程序时,我得到以下错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kalamController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.kalam.service.EmployeeService com.kalam.controller.KalamController.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kalam.service.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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
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:5110)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5633)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1694)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1684)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.kalam.service.EmployeeService com.kalam.controller.KalamController.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kalam.service.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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kalam.service.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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 24 more
请告诉我哪里出了问题。我是否忘记了xml文件中的任何配置?我的意图是了解如何使用@AutoWired注释在controller中调用服务。
而不是
<context:component-scan base-package="com.kalam.controller" />
<context:component-scan base-package="com.kalam.serviceimpl" />
<context:component-scan base-package="com.kalam.service" />
<context:component-scan base-package="com.kalam.daoimpl" />
<context:component-scan base-package="com.kalam.testclass" />
尝试使用
<context:component-scan base-package="com.kalam.controller, com.kalam.serviceimpl ..." />
在配置(Hibernate Config Xml)文件中,而不是使用
<bean id="employeeService" class="com.kalam.service.EmployeeService" />
您需要使用
<bean id="employeeService" class="com.kalam.serviceimpl.EmployeeServiceImpl"/>
您将EmployeeService传递给作为接口的bean。传递已实现的类,并将Controller中的引用和使用作为
@Autowired
EmployeeService employeeService;
我想知道为什么来自UserDetailsServiceImpl类的方法不会出现在控制器中...我只能访问接口中的方法,但不能访问其他实现的方法。我尝试使用限定符,甚至制作了一个config类来在按接口注入bean时实例化impl类。 谁能告诉我我做错了什么?
我创建了一个捆绑包,我想用它的最佳实践。 所以我所有的服务都是私人的 https://symfony.com/doc/current/service_container/alias_private.html 和 https://symfony.com/blog/new-in-symfony-3-4-services-are-private-by-default 在Symfony core中,我们已
问题内容: 我的测试课: 我在行上得到一个空指针异常: 在精确的给出空指针异常 如何使Junit类中的beanObject字段自动装配成为可能,以便可以使用“ BeanClass”类中的方法? 从评论中复制: 用简单的术语来说.. beanClass是具有某些方法的接口..我用 注释标记了该beanClass。.banClass 是由具有方法实现的beanClassImpl类实现的。.我需要在我的
UI代码:在资源\视图\分发器Registration.php 在控制器类中。。。。。 名称空间App\Http\Controllers; 使用App\User; 使用App\Http\Controller\Controller; 使用\Http\Request; 使用资源\视图\分销商或注册; 当我在路线上呼叫这个控制器时 致命错误:找不到类“App\Http\Controllers\distr
我已经读了这些问题,但没有一个有效: Spring boot MVC -无法自动连接服务类中的存储库 为什么不能@Autowired a JPA存储库-Spring boot JPA JpaRepository在服务类中获取Null 还有这个:https://www.baeldung.com/spring-autowired-field-null 不幸的是,它们都不起作用。 我拥有的是: 服务接口
问题内容: 我正在使用Jersey框架(JAX-RS实现)来构建RESTful Web服务。 我无法使用@DELETE REST方法,因为当我尝试调用它时会抛出异常。以下@DELETE方法用于删除Employee: } 我正在使用以下代码块来调用服务: 当我运行客户端时,它将引发以下异常: 如果有人可以指导我解决该问题,那将是很好的? 问题答案: 对我来说,它有助于从客户端的delete()方法中