gpt4 book ai didi

java - 没有看到使用 Autowired 注释的 bean,没有任何帮助

转载 作者:行者123 更新时间:2023-12-02 05:04:30 25 4
gpt4 key购买 nike

我正在尝试在 Spring MVC 应用程序中 Autowiring 服务,但它总是失败!尝试了互联网上每个问题的所有内容,但我找不到答案。问题是Spring实际上无法 Autowiring 字段并说没有这样的bean,

尝试了所有方法,更改代码、配置等。也许现在我项目中的所有内容都太困惑了,我找不到错误。

@Controller
public class ViewController {

@Autowired
private IUserService userService;


public void setUserService(IUserService userService) {
this.userService = userService;
}
}
@Component
@Service("userService")
public class UserServiceImpl implements IUserService {

private static final SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").
addAnnotatedClass(User.class).addAnnotatedClass(Restaurant.class).
buildSessionFactory();

public UserServiceImpl() {
}
}

调度程序-servlet.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.springmvcdemo.Controller" />

<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Step 5: Define Spring MVC view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">

<display-name>spring-mvc-demo</display-name>

<absolute-ordering />
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="com.springmvcdemo.Services" />
</beans>
Error creating bean with name 'viewController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.springmvcdemo.Services.IUserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

最佳答案

所以问题是 SessionFactory 是在服务的静态字段中构造的。

可以找到该类,但创建 session 工厂时出现一些错误,这就是错误消息包含“Bean 实例化失败”的原因。

将 session 工厂移至由 Spring 管理,然后注入(inject)它是正确的方法。

关于java - 没有看到使用 Autowired 注释的 bean,没有任何帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56362030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com