我是spring MVC的新手。我正面临UnsatisfiedDependencyException
。我添加了stereotype annotations
,但仍然面临同样的问题。
上下文初始化过程中遇到异常-取消刷新尝试:org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“user controller”的bean时出错:通过字段“user service”表示的不满足的依赖关系;嵌套异常为org.springframework.beans.factory.noSuchBeanDefinitionException:找不到依赖项的合格bean[com.demo.app.service.userservice]:至少需要一个合格的自带候选bean。依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}看起来正向重播。
@CrossOrigin
@RestController
public class UserController {
@Autowired(required=true)
private UserService userService;
@RequestMapping(value = { "/userSave" },consumes = {"multipart/form-data"}, method = RequestMethod.POST)
@ResponseBody
public String saveUserDetails(@RequestPart(value="file",required=false) MultipartFile file,
@RequestPart("user")User user,
HttpSession session, HttpServletRequest request,
HttpServletResponse response){
System.out.println("data reached...!");
String result=userService.saveUserData(user,session);
return result;
}
}
用户服务:
public interface UserService {
public String saveUserData(User user,HttpSession session);
}
UserServiceImpl:
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
UserRepository userRepository;
public String saveUserData(User user,HttpSession session){
String output;
Date regDate=new Date();
user.setRegDate(regDate);
output= userRepository.saveUserData(user);
return output;
}
@Component
@Transactional
public class UserRepository extends BaseRepository{
@Autowired
protected SessionFactory sessionFactory;
public String saveUserData(User user) {
final Session session = (Session) getSessionFactory();
try {
session.beginTransaction();
Query query=session.createQuery("UPDATE User set user_Name =:userName,"
+ "reg_Date =:regDate,img_Id=:imgId");
query.setParameter("userName", user.getUserName());
query.setParameter("regDate", user.getRegDate());
query.setParameter("imgId", user.getImgId());
query.setParameter("emailId", user.getImgId());
session.save(user);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<context:annotation-config />
<context:component-scan base-package="com.demo.app" />
<mvc:default-servlet-handler />
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<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/html/" />
<property name="suffix" value="html" />
</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:3306/UserDB" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.demo.app.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">Error</prop>
</props>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2097152" />
</bean>
</beans>
SEVERE: Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [com.demo.app.service.UserService]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
谢谢你
@service(“userservice”)
应靠近实现而不是接口。
“我是Spring新手,我刚开始一个Spring MVC CRUD项目,但在尝试了很多方法之后,我一次又一次地面临同样的错误。 这是打印HTTP状态500的第一个异常-内部服务器错误。 HTTP状态500–内部服务器错误 javax。servlet。ServletException:Servlet。servlet[dispatcher]的init()引发异常组织。阿帕奇。卡塔琳娜。验证者。Auth
我在尝试启动应用程序时遇到此错误。我看过许多类似的问题和话题,但似乎没有一个对我有帮助。 创建名为“databaseManager”的bean时出错:通过字段“articleRepo”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“pl.dzejkobdevelopment.da
unsatisfiedDependencyException:创建名为“home controller”的bean时出错:通过字段“dao”表示的不满足依赖项;嵌套异常为org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“alien dao”的bean时出错:通过字段“session factory”表示的不满
引导类: 有人能帮我解决问题吗?
当我尝试启动应用程序时,我得到以下消息: “不满意的依赖关系”异常:创建名称为“产品服务”的 Bean 时出错 [C:\Users\Acasa\0 SDA\0 Proiecte 实践\attentive2细节\目标\类\com\示例\attentive2细节\服务\产品服务.class]:通过构造函数参数 0 表示的不满意的依赖关系;嵌套的异常是组织.springframework.bean.fa
我查了一些类似的问题,但这些答案帮不了我。 错误 组织。springframework。豆。工厂未满足的依赖项异常:创建名为“accountController”的bean时出错:未满足的依赖项通过字段“accountService”表示;嵌套的异常是org。springframework。豆。工厂NoSuchBeanDefinitionException:没有类型为“com”的合格bean。服务