自动装配不适用于@Repository

时间:2014-06-27 17:33:35

标签: spring repository

我正在尝试创建一个将数据存储在数据库中的简单REST服务。这是一个从REST控制器到MVC控制器的示例体系结构,它实例化实体并尝试通过自动装配的存储库将其存储在数据库中。 正确调用REST服务并回复它所拥有的内容;但是,存储实体失败并且自动装配的存储库为空。 有人可以帮忙吗?

我的REST服务:

@RestController
@RequestMapping("/coord")
public class CoordService {

    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    public String getMuseo(@PathVariable String name) {
        String result = "Hello " + name + ", I am saving on the db.";
        new CoordController().saveCoord();

        return result;
    }
}

我的应用程序业务逻辑(MVC中的控制器):

@Component
public class CoordController {
    @Autowired
    private CoordRepository coordRepository;

    public void saveCoord() {
        System.out.println("Ok controller");

        Coord cg = new Coord();
        System.out.println("Ok new");
        cg.setCoord("xyz");
        cg.setId(1L);

        if (coordRepository == null) {
            System.out.println("REP NULL!");
        } else
            coordRepository.save(cg);

        System.out.println("Ok save()");
    }
}

我的实体:

@Entity
@Configurable
public class Coord extends IdentifiableEntity {

    @NotNull
    private String coord;

    public String getCoord() {
        return this.coord;
    }

    public void setCoord(String coord) {
        this.coord = coord;
    }
}

我的存储库:

@Repository
public interface CoordRepository extends
    JpaSpecificationExecutor<Coord>,
    JpaRepository<Coord, Long> {
}

我的applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <context:property-placeholder location="classpath*:spring/*.properties" />
    <context:component-scan base-package="com.lh.clte" />


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
        <property name="driverClassName" value="${database.driverClassName}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />
        <property name="initialSize" value="3" />
        <property name="maxActive" value="10" />
    </bean>

    <tx:annotation-driven mode="proxy"
    transaction-manager="transactionManager" />
    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
    id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <jpa:repositories base-package="com.lh.clte.repository" />
</beans>

我的persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
    <persistence-unit name="persistenceUnit"
    transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
            <property name="hibernate.connection.charSet" value="UTF-8" />
        </properties>
    </persistence-unit>
</persistence>

1 个答案:

答案 0 :(得分:2)

你的问题在这里

new CoordController().saveCoord();

您需要将CoordController自动装配到CoordService中。通过使用新的CoordController(),您将创建一个不由spring管理的CoordController实例,因此其字段不会自动装配。

@RestController
@RequestMapping("/coord")
public class CoordService {

    @Autowired
    private CoordController coordController;

    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    public String getMuseo(@PathVariable String name) {
        String result = "Hello " + name + ", I am saving on the db.";
        coordController.saveCoord();

        return result;
    }
}

顺便说一下,你的CoordService类应该被命名为CoordController,因为它是一个控制器(它有@RestController注释!),你的CoordController应该是CoordService,因为它包含业务逻辑。