我在Spring Boot上创建了一个项目。
我有两个提供程序扩展同一个Abstract提供程序,我在启动时通过Spring Profile加载我感兴趣的提供程序。
其中一个提供者基于JPA,另一个提供了他的接口,我调用了webservices。
这是我不想使用数据库的提供者的界面:
package net.worldline.mst.metro.ds.core.massilia.provider;
import net.worldline.mst.metro.ds.core.contract.IProductRepository;
import net.worldline.mst.metro.ds.core.massilia.model.MassiliaProduct;
import org.springframework.context.annotation.Profile;
import org.springframework.data.repository.NoRepositoryBean;
@Profile("massilia")
@NoRepositoryBean
public interface MassiliaProductRepository extends IProductRepository<MassiliaProduct,String> {
}
这是使用数据库的提供者的接口:
package net.worldline.mst.metro.ds.core.local.provider;
import net.worldline.mst.metro.ds.core.contract.IProductRepository;
import net.worldline.mst.metro.ds.core.local.model.Product;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
import org.springframework.stereotype.Repository;
@Profile("local")
@Repository
public interface MonBoProductRepository extends IProductRepository<Product,String> {
@Query("select p.variants from Product p where p.ean = :ean")
List<Product> findVariantByEan(@Param("ean") String ean);
@Query("select p.companions from Product p where p.ean = :ean")
List<Product> findCompanionByEan(@Param("ean") String ean);
}
他们共同扩展了这个界面:
package net.worldline.mst.metro.ds.core.contract;
import net.worldline.mst.metro.ds.core.model.AbstractProduct;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.Serializable;
import java.util.List;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.Serializable;
import java.util.List;
@NoRepositoryBean
public interface IProductRepository<T extends AbstractProduct,ID extends Serializable> extends CrudRepository<T, ID> {
@RestResource(path = "byEAN")
T findByEan(@Param("ref") Integer ean);
T findProductByEan(@PathVariable ID ean);
List<T> findVariantByEan(@PathVariable ID ean);
List<T> findCompanionByEan(@PathVariable ID ean);
}
提供者没有使用数据库有一个实现,出于工作原因,我无法向您展示实现,但它调用webservices内部
与我的提供者一样,我有两个模型,扩展了相同的抽象类。
一个用@ Entity,@ Id和co注释,我不想在另一个类上添加这个注释,因为对我来说,我已经预先知道我不想要任何数据库application-${profile}.properties
。
这是我与bdd一起使用的模型:
package net.worldline.mst.metro.ds.core.local.model;
import net.worldline.mst.metro.ds.core.model.AbstractProduct;
import net.worldline.mst.metro.ds.core.model.AbstractProductCharacteristic;
import org.hibernate.validator.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name = "PRODUCTS")
@Profile("local")
public class Product extends AbstractProduct {
private static final Logger log = LoggerFactory.getLogger(Product.class);
@ManyToMany(
fetch = FetchType.LAZY
)
@JoinTable(
name="products_to_variants",
joinColumns = @JoinColumn(name="productEan"),
inverseJoinColumns = @JoinColumn(name="productEanVariant")
)
private List<Product> variants;
@ManyToMany(
fetch = FetchType.LAZY
)
@JoinTable(
name="products_to_companions",
joinColumns = @JoinColumn(name="productEan"),
inverseJoinColumns = @JoinColumn(name="productEanCompanion")
)
private List<Product> companions;
@Column(name = "accroche")
private String accroche;
@Id
@Column(name = "ean", unique = false)
private String ean;
@Column(name = "descriptif")
private String descriptif;
@Column(name = "libelle")
@NotEmpty
private String libelle;
@Column(name = "oldPrice")
private String oldPrice;
@Column(name = "price")
@NotEmpty
//@Digits(fraction = 0, integer = 10)
private String price;
@Column(name = "stock")
private String stock;
@OneToMany(mappedBy = "ean" )
protected List<ProductCharacteristic> characteristics;
@OneToMany(mappedBy = "product" )
@NotEmpty
protected List<ProductVisual> visuals;
public List<Product> getVariants() {
return variants;
}
public void setVariants(List<Product> variants) {
this.variants = variants;
}
public List<Product> getCompanions() {
return companions;
}
public void setCompanions(List<Product> companions) {
this.companions = companions;
}
@Override
public String getAccroche() {
return accroche;
}
@Override
public void setAccroche(String accroche) {
this.accroche = accroche;
}
@Override
public String getEan() {
return ean;
}
public void setRef(String ean) {
this.ean = ean;
}
@Override
public String getLibelle() {
return libelle;
}
@Override
public void setLibelle(String libelle) {
this.libelle = libelle;
}
@Override
public String getOldPrice() {
return oldPrice;
}
@Override
public void setOldPrice(String oldPrice) {
this.oldPrice = oldPrice;
}
@Override
public String getPrice() {
return price;
}
@Override
public void setPrice(String price) {
this.price = price;
}
@Override
public String getStock() {
return stock;
}
@Override
public void setStock(String stock) {
this.stock = stock;
}
@Override
public List<? extends AbstractProductCharacteristic> getCharacteristics() {
return characteristics;
}
@Override
public List<ProductVisual> getVisuals() {
return visuals;
}
public String getDescriptif() {
return this.descriptif;
}
public void setDescriptif(String descriptif) {
this.descriptif=descriptif;
}
}
这是我不想用于数据库的模型:
package net.worldline.mst.metro.ds.core.massilia.model;
import net.worldline.mst.metro.ds.core.model.AbstractProduct;
import org.springframework.context.annotation.Profile;
import javax.persistence.*;
import java.util.List;
@Profile("massilia")
public class MassiliaProduct extends AbstractProduct {
@Override
public String getEan() { return this.ean; }
@Override
public String getLibelle() { return this.libelle; }
@Override
public String getPrice() { return this.price; }
@Override
public String getAccroche() { return this.accroche; }
@Override
public String getOldPrice() { return oldPrice; }
@Override
public String getStock() { return stock; }
@Override
public String getDescriptif() {
return descriptif;
}
@Override
public List<MassiliaCharacteristic> getCharacteristics() {
return (List<MassiliaCharacteristic>)characteristics;
}
@Override
public List<MassiliaProductVisual> getVisuals() {
return (List<MassiliaProductVisual>)visuals;
}
}
他们共同分享这个模型:
package net.worldline.mst.metro.ds.core.model;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.core.Relation;
import java.util.List;
@Relation(value = "product", collectionRelation = "product")
public abstract class AbstractProduct extends ResourceSupport {
protected String ean;
protected String libelle;
protected String accroche;
protected String price;
protected String oldPrice;
protected String stock;
protected String descriptif;
protected List<? extends AbstractProductCharacteristic> characteristics;
protected List<? extends AbstractProductVisual> visuals;
public abstract String getEan();
public abstract String getLibelle();
public abstract String getPrice();
public abstract String getAccroche();
public abstract String getOldPrice();
public abstract String getStock();
public abstract List<? extends AbstractProductCharacteristic> getCharacteristics();
public abstract List<? extends AbstractProductVisual> getVisuals();
public abstract String getDescriptif();
public void setEan(String ean) {
this.ean = ean;
}
public void setLibelle(String libelle) {
this.libelle = libelle;
}
public void setPrice(String price) {
this.price = price;
}
public void setAccroche(String accroche) {
this.accroche = accroche;
}
public void setOldPrice(String oldPrice) {
this.oldPrice = oldPrice;
}
public void setStock(String stock) {
this.stock = stock;
}
public void setCharacteristics(List<? extends AbstractProductCharacteristic> characteristics) {
this.characteristics = characteristics;
}
public void setVisuals(List<? extends AbstractProductVisual> visuals) {
this.visuals = visuals;
}
public void setDescriptif(String descriptif) {
this.descriptif = descriptif;
}
}
在应用程序-${profile}.properties
中,我确切地说:
spring.datasource.platform = hsqldb for the jpa instance
。
spring.datasource.platform = none for the instance where i call my webservices.
我的问题很简单:我希望春天让我按照我想要的方式实现存储库,但是当我启动我的服务器时,spring说我的对象没有被管理,所以如果我不将@Entity添加到我的模型,它不想运行。
那么为什么Spring数据看起来默认加载JPA存储库呢?
答案 0 :(得分:0)
事实上这是一个人为错误。
我忘记了spring.datasource.platform = hsqldb
文件中的application.properties
。
我没有看它,因为我正在使用弹簧配置文件所以我正在查看我的application-massilia.properties
包含spring.datasource.platform = none
并且现在已经收听了因为我删除了其他文件中的副本