简体   繁体   中英

Spring JPA with ApplicationContext.xml, DAO and Service are NULL

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. :(

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

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 ^^

use @Resource annotation with this

@Resource 
    private TagDAO tagDAO;

in your configuration file

annotate your controller with @Controller and add @Resource annotation to your services also same like DAO's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM