简体   繁体   中英

Spring injected bean is null

I am using Spring Framework / Data / HATEOAS and trying to add Dozer.

I have the following bean in my spring-config.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:data="http://www.springframework.org/schema/data/jpa"
        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-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/data/jpa
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="POSTGRESQL" />
        <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/cp" />
        <property name="username" value="cp_user" />
        <property name="password" value="+JMJ+pw0m2d" />
    </bean>

    <context:component-scan base-package="com.mydomain.data.assembler" />
    <data:repositories base-package="com.mydomain.repository" />

    <mvc:annotation-driven />

    <bean id="dozerFactory" class="org.dozer.spring.DozerBeanMapperFactoryBean" scope="singleton">
        <property name="mappingFiles" value="classpath*:/*mapping.xml"/>
    </bean>

</beans>

And the following assembler:

@Component
public class UserResourceAssembler {

    @Inject 
    private Mapper dozerBeanMapper;

    public UserResource toResource(User user) {
        UserResource resource = dozerBeanMapper.map(user, UserResource.class);
        resource.add(linkTo(methodOn(UserController.class).get(user.getId())).withSelfRel());
        return resource;
    }

    public User toEntity(UserResource resource) {
        User user = dozerBeanMapper.map(resource, User.class);
        return user;
    }
}

So, - I'm very new to beans and injection - but I guess that the factory bean is ?supposed? to inject the Mapper. But the Mapper is definitely null. I know I'm not doing this right, but what am I doing wrong?

Spring injects its beans into spring managed beans. You are using an unmanaged static context. Change UserResourceAssembler into a managed bean as well:

@Component
public class UserResourceAssembler {

    @Inject
    private Mapper dozerBeanMapper;

    public UserResource toResource(User user) {
    }

    public User toEntity(UserResource resource) {
    }

}

See why can't we autowire static fields in spring .

I would have preferred something like the above. But then I read:

Dozer Singleton Startup Bean injetced as Null

That worked. Here was my implementation.

I removed the bean from spring-config, and the context scan.

I added this class:

@Singleton
public class DozerInstantiator {
    public static DozerBeanMapper getInstance(){
        return MapperHolder.instance;
    }

    private static class MapperHolder{
        static final DozerBeanMapper instance = new DozerBeanMapper();
    }
}

And updated my assembler like this:

public class UserResourceAssembler {

    private DozerBeanMapper mapper;

    public UserResourceAssembler() {
        mapper = DozerInstantiator.getInstance();
    }

    public UserResource toResource(User user) {     
        UserResource resource = mapper.map(user, UserResource.class);
        resource.add(linkTo(methodOn(UserController.class).get(user.getId())).withSelfRel());
        return resource;
    }

    public User toEntity(UserResource resource) {
        User user = mapper.map(resource, User.class);
        return user;
    }
}

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