1
我想創建一個簡單的REST服務,在數據庫中存儲數據。這是一個從REST控制器到MVC控制器的示例架構,MVC控制器實例化一個實體並嘗試通過自動裝配的存儲庫將其存儲在數據庫中。 正確調用REST服務並回復它的內容;但是,存儲實體失敗並且自動裝載的存儲庫爲空。 有人可以幫忙嗎?自動裝配不工作@Repository
我的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>
行動,謝謝,我不知道怎麼會錯過!但是,只有當您想要留在Spring詞典中時,我才同意您的術語問題。在文獻中,控制器沒有提到「網絡控制器」(不管它是否是REST),而是指向業務邏輯。出於這個原因,與Spring相比,我喜歡稱之爲「控制器」,Spring將使用@Service來註釋。 – Manu