spring - Autowiring not working for @Repository -


i trying create simple rest service stores data in db. sample architecture going rest controller mvc-controller, instantiates entity , tries store in db via autowired repository. rest service correctly invoked , replies has to; however, storing entity fails , autowired repository null. can help?

my rest service:

@restcontroller @requestmapping("/coord") public class coordservice {      @requestmapping(value = "/{name}", method = requestmethod.get)     public string getmuseo(@pathvariable string name) {         string result = "hello " + name + ", saving on db.";         new coordcontroller().savecoord();          return result;     } } 

my application business logic (controller in 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()");     } } 

my entity:

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

my repository:

@repository public interface coordrepository extends     jpaspecificationexecutor<coord>,     jparepository<coord, long> { } 

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

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

your problem here

new coordcontroller().savecoord(); 

you need autowire coordcontroller coordservice. using new coordcontroller(), creating instance of coordcontroller not managed spring fields not autowired.

@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 + ", saving on db.";         coordcontroller.savecoord();          return result;     } } 

by way, coordservice class should named coordcontroller since controller (it has @restcontroller annotation!) , coordcontroller should coordservice since contains business logic.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -