gpt4 book ai didi

java - spring-nullpointerexception- 无法访问无注释类中的 Autowiring 注释服务(或 dao)

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:00:30 24 4
gpt4 key购买 nike

我遇到了无法解决的问题。

从我的@Controller,我可以很容易地访问我的 Autowiring 的@Service 类并毫无问题地使用它。但是当我从一个没有注释的单独类执行此操作时,它会给我一个 NullPointerException

我的 Controller (工作)-

@Controller
public class UserController {
@Autowired
UserService userService;...

我单独的 Java 类(不工作)-

public final class UsersManagementUtil {
@Autowired
UserService userService;

@Autowired
UserDao userDao;

userService 或 userDao 总是空的!只是尝试其中任何一个是否有效。

我的组件扫描设置设置了用于扫描的根级包,所以应该没问题。

我的 servlet 上下文 -

<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- the application context definition for the
springapp DispatcherServlet -->
<!-- Enable annotation driven controllers, validation etc... -->

<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="x" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />

<!-- package shortended -->
<bean id="messageSource"
class="o.s.c.s.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>

<!-- package shortened -->
<bean id="viewResolver"
class="o.s.w.s.v.InternalResourceViewResolver">

<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order">
<value>0</value>
</property>
</bean>

<!-- package shortened -->
<bean id="sessionFactory"
class="o.s.o.h3.a.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>orion.core.models.Question</value>
<value>orion.core.models.User</value>
<value>orion.core.models.Space</value>
<value>orion.core.models.UserSkill</value>
<value>orion.core.models.Question</value>
<value>orion.core.models.Rating</value>
</list>
</property>
<property name="hibernateProperties">

<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>

<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

有什么线索吗?

最佳答案

来自 Spring Reference 3.0

By default, classes annotated with @Component, @Repository, @Service, @Controller, or a custom annotation that itself is annotated with @Component are the only detected candidate components.

UsersManagementUtil 应根据您的需要使用其中之一进行注释。

关于java - spring-nullpointerexception- 无法访问无注释类中的 Autowiring 注释服务(或 dao),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4289805/

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