Spring JPA 与 ApplicationContext.xml、DAO 和 Service 为 NULL
我有这个问题。所有 DAO 和服务都是空的,我不知道如何解决这个问题:(
中的配置部分,
...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
...
这是 web.xml和 applicationContext.xml
<context:component-scan base-package="com.tipytut" />
<context:annotation-config />
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>/WEB-INF/classes/tipytut.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
我有一个像这样的 DAO:
@Repository("tagDAO")
@Transactional
public class TagDAOImpl extends JpaDAO implements TagDAO {
public List<Tag> getTags() {
return getList("SELECT t FROM Tag t");
}
}
... 和服务
@Service("managementService")
@Transactional
public class ManagementServiceImpl implements ManagementService {
@Autowired
private TagDAO tagDAO;
public List<Tag> getTags() {
return tagDAO.getTags();
}
}
当我调用该服务时在我的控制器中,它始终为 NULL
@Autowired
private ManagementService managementService;
public List<Tag> getTags() {
try {
managementService.getTags();
} catch (Exception ex) {
ex.printStackTrace();
}
return tags;
}
任何帮助将不胜感激
。 我上传了我的项目这里(只是最初的部分,非常简单),所以大家可以看一下。希望有人能弄清楚这有什么问题。 :(
I'm having a problem with this. All the DAOs and Services are null, I don't how to fix that :(
Here is the config part in web.xml
...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
...
and in applicationContext.xml
<context:component-scan base-package="com.tipytut" />
<context:annotation-config />
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>/WEB-INF/classes/tipytut.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
I have a DAO like this:
@Repository("tagDAO")
@Transactional
public class TagDAOImpl extends JpaDAO implements TagDAO {
public List<Tag> getTags() {
return getList("SELECT t FROM Tag t");
}
}
... and the Service
@Service("managementService")
@Transactional
public class ManagementServiceImpl implements ManagementService {
@Autowired
private TagDAO tagDAO;
public List<Tag> getTags() {
return tagDAO.getTags();
}
}
When I call that service in my Controller, it's always NULL
@Autowired
private ManagementService managementService;
public List<Tag> getTags() {
try {
managementService.getTags();
} catch (Exception ex) {
ex.printStackTrace();
}
return tags;
}
Any help will be appreciated.
UPDATED:
I uploaded my project HERE (just the initial part, very simple), so everyone can take a look. Hope someone can figure out what is wrong with that. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请检查这些 service/Dao 是否在 com.tipytut 包中。 context:annotation-config 在组件扫描中自动启用。您的控制器是用 @Controller 注解还是通过 @Component 制成组件?否则它将不会被检测到,并且不会是 Spring bean,除非您手动连接它。所以如果不是Spring bean,自动装配将不起作用
Please check if these service/Dao are in the package com.tipytut . context:annotation-config is automatically enabled in component scan . Is your controller annotated with @Controller or made a component via @Component ? Else it'll not be detected and will not be a Spring bean unless you have wired it manually . So Autowiring will not work if it is not a Spring bean
我忘了更新我的问题,终于弄清楚了!我忘记添加
struts2-spring-plugin
的依赖项,问题是我没有收到有关此 X 的错误消息-(希望这会对某人有所帮助 ^^
I forgot to update my question, finally figured it out! I forgot to add dependency for
struts2-spring-plugin
, the problem is that I got no error message about this X-(Hope this will help someone ^^
使用
@Resource
注释,在配置文件中
使用
@Controller
注释您的控制器,并将@Resource
注释添加到您的服务中,与 DAO 的服务相同。use
@Resource
annotation with thisin your configuration file
annotate your controller with
@Controller
and add@Resource
annotation to your services also same like DAO's.