弹簧nullpointerexception-着接入自动装配在一个没有注释类注释服务(或DAO) [英] spring-nullpointerexception- cant access autowired annotated service (or dao) in a no-annotations class
问题描述
我有这个问题,我无法修复。
I have this problem that I cannot fix.
从我 @Controller
,我可以轻松地访问我的自动装配 @Service
类,并发挥它没有问题。
但是,当我这样做,从没有标注一个单独的类,它给了我一个 NullPointerException异常
。
From my @Controller
, i can easily access my autowired @Service
class and play with it no problem.
But when I do that from a separate class without annotations, it gives me a NullPointerException
.
我的控制器(作品) -
My Controller (works)-
@Controller
public class UserController {
@Autowired
UserService userService;...
我单独的Java类(没有工作) -
My separate Java class (not working)-
public final class UsersManagementUtil {
@Autowired
UserService userService;
或
@Autowired
UserDao userDao;
userService或userDAO的总是空!
只是想,如果他们中的任何一个作品。
userService or userDao are always null! Was just trying if any one of them works.
我的组件扫描设置具有根级封装扫描设置,这样应该没问题。
My component scan setting has the root level package set for scanning so that should be OK.
我的servlet上下文 -
my servlet context -
<?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>
任何线索?
推荐答案
从<一个href=\"http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-scanning-filters\">Spring参考3.0
在默认情况下,类与注释
@Component
, @Repository
, @Service
,
@Controller
,或自定义注释
本身都被注解
@Component
是唯一的检测
候选组件。
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
应根据您的需要其中的一个被注解。
UsersManagementUtil
should be annotated with one of them based on your need.
这篇关于弹簧nullpointerexception-着接入自动装配在一个没有注释类注释服务(或DAO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!