当前位置: 首页 > 知识库问答 >
问题:

Spring注入bean为空

苏宏逸
2023-03-14

我正在使用Spring Framework/Data/HATEOAS,并试图添加dozer。

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

和以下汇编程序:

@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;
    }
}

所以,-我对豆子和注射很陌生-但我想工厂的豆子是?应该?来注入映射器。但是映射器肯定是空的。我知道我做得不对,但我做错了什么?

共有1个答案

季阳朔
2023-03-14

Spring将其bean注入Spring托管bean中。您正在使用非托管静态上下文。还将UserResourceAssembler更改为托管bean:

@Component
public class UserResourceAssembler {

    @Inject
    private Mapper dozerBeanMapper;

    public UserResource toResource(User user) {
    }

    public User toEntity(UserResource resource) {
    }

}

看看为什么我们不能在Spring自动取回静态场。

 类似资料:
  • 问题内容: 是否可以将Spring bean注入RestEasy @Path类中?我设法通过Jersey并使用@InjectParam注释完成了此操作,但是由于某些其他原因,我需要切换到RestEasy,而且我似乎找不到解决方法(尝试了javax.inject.Inject,但没有)。 编辑 此解决方案有效:http : //www.mkyong.com/webservices/jax-rs/re

  • 主要内容:构造函数注入,setter 注入,短命名空间注入所谓 Bean 属性注入,简单点说就是将属性注入到 Bean 中的过程,而这属性既可以普通属性,也可以是一个对象(Bean)。 Spring 主要通过以下 2 种方式实现属性注入: 构造函数注入 setter 注入(又称设值注入) 构造函数注入 我们可以通过 Bean 的带参构造函数,以实现 Bean 的属性注入。 使用构造函数实现属性注入大致步骤如下: 在 Bean 中添加一个有参构造函数,构造

  • 问题内容: 我已经用spring和spring security开发了一个简化的Web应用程序…现在,我想添加ejb模块来进行数据库访问,我在Internet上查找,但是由于它是我第一次使用EJB,所以我没有找到明确的东西。我想在控制器中使用类似@EJB之类的东西” 以及在有教程或其他帮助的情况下如何在Spring上下文中对其进行配置。很好,谢谢 问题答案: 要将ejb 3 bean注入sprin

  • 我正在开发一个遗留的JSF应用程序,我们正在慢慢地将其移植到Spring MVC。我们正在使用Spring Security来控制登录信息。在用户登录之后,JSF页面全局地实例化一个在任何地方都使用的会话作用域bean。我想更改应用程序,这样我们就可以先进入用Spring MVC开发的页面。 我尝试的一种方法是将bean转换为spring bean,并将其注入JSF,但不幸的是,这需要对bean进

  • 问题内容: 我试图将EJB注入到Spring(3.1.2)服务中(都在不同的 WAR中)两者都非常简单(删除了方法以简化示例): EJB: Service: 事情很简单,但我尝试过: 它没有用。然后我还尝试了: and 但都没有奏效。 我设法使用以下方法注入了EJB: 在我的spring配置和服务中: 但我真的不喜欢这种解决方案。我希望在某些注释中包含我的JNDI路径,例如: 问题答案: 我们找到

  • 问题内容: 我想将Mockito模拟对象注入到Spring(3+)bean中,以进行JUnit的单元测试。我的bean依赖项当前是通过在私有成员字段上使用注释来注入的。 我考虑过使用,但是我希望注入的bean实例实际上是一个代理,因此没有声明目标类的私有成员字段。我不希望为依赖项创建一个公共的setter,因为我将纯粹出于测试目的而修改接口。 我遵循了Spring社区提供的一些建议,但是未创建该模