我目前正在开发一个基于Spring启动的应用程序,该应用程序还集成了ActiveMQ功能,以处理两个不同应用程序之间的数据库更新。我目前正在尝试集成spring数据jpa(使用hibernate)提供的默认功能来处理与DB相关的操作。 当尝试使用JpaRepository将收到的日期保存到数据库时,它被指示为Null。
引用类似方案的链接
Spring @Autowired can not wire Jpa repository
其他资源
https://spring.io/guides/gs/accessing-data-jpa/
异常
java.lang.NullPointerException
at samples.reportdata.reports.sampleSalesReport_Observer.createSampleSalesRecord(sampleSalesReport_Observer.java:107)
at samples.reportdata.reports.sampleSalesReport_Observer.update(sampleSalesReport_Observer.java:23)
at samples.reportdata.reports.BaseReport.call(BaseReport.java:36)
at samples.reportdata.observer.observers.BaseObserver.update(BaseObserver.java:39)
at samples.reportdata.observer.subject.BaseSubject.notifyAllObservers(BaseSubject.java:36)
at samples.reportdata.observer.subject.BaseSubject.setMessage(BaseSubject.java:70)
at samples.reportdata.utilities.CommonConsumer.process(CommonConsumer.java:127)
at samples.reportdata.Controller.reportController(Controller.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:220)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at samples.reportdata.filters.CrossOriginRequestSharingFilter.doFilter(CrossOriginRequestSharingFilter.java:57)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:89)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:117)
at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:61)
at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:110)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)strong text
我的代码段:
应用
package samples.reportdata;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import samples.reportdata.utilities.CommonLoader;
@SpringBootApplication
/*@Configuration
@ComponentScan
@EnableAutoConfiguration
@EntityScan(basePackages = {"samples.reportdata.pojo"})
@EnableJpaRepositories(basePackages = {"samples.reportdata.repository"})
@EnableTransactionManagement*/
public class RezgReportDataServiceApplication {
public static void main(String[] args) {
new CommonLoader();
SpringApplication.run(RezgReportDataServiceApplication.class, args);
}
}
控制器
注意:它充当从Active MQ *
收到的数据的入口点 package samples.reportdata;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import samples.reportdata.utilities.CommonConsumer;
@RestController
public class Controller {
@RequestMapping(value = "/receive", method = RequestMethod.POST)
public String reportController(@RequestBody String postPayload, @RequestParam(value = "queue") String queue) {
String msg = "";
try {
System.out.println("queue==>" + queue);
System.out.println(URLDecoder.decode(postPayload, "UTF-8"));
msg = CommonConsumer.getInstance().process(URLDecoder.decode(postPayload, "UTF-8"), queue.split("queue://")[1]);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return msg;
}
}
实体类 sample_sales_report数据库表的持久化类。
package samples.reportdata.pojo;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(schema="rezbase_v3_reports", name="sample_sales_report")
@NamedQuery(name="sampleSalesReport.findAll", query="SELECT t FROM sampleSalesReport t")
public class sampleSalesReport implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private Integer id;
@Column(name="booking_channel", length=5)
private String bookingChannel;
@Column(name="cancel_charges")
private double cancelCharges;
@Column(name="cancel_charges_base")
private double cancelChargesBase;
@Column(name="cash_base_fare")
private double cashBaseFare;
@Column(name="cash_base_fare_base")
private double cashBaseFareBase;
@Column(name="cash_currency", length=10)
private String cashCurrency;
@Column(name="cash_land_fare")
private double cashLandFare;
@Column(name="cash_land_fare_base")
private double cashLandFareBase;
@Column(name="cash_other_tax")
private double cashOtherTax;
@Column(name="cash_other_tax_base")
private double cashOtherTaxBase;
@Column(name="cash_tot_tax")
private double cashTotTax;
@Column(name="cash_tot_tax_base")
private double cashTotTaxBase;
@Column(name="cash_yqyrtax")
private double cashYqyrtax;
@Column(name="cash_yqyrtax_base")
private double cashYqyrtaxBase;
@Column(name="city_id")
private Integer cityId;
@Column(name="city_name", length=50)
private String cityName;
@Column(length=5)
private String combination;
@Column(name="country_id")
private Integer countryId;
@Column(name="country_name", length=50)
private String countryName;
@Column(name="credit_base_fare")
private double creditBaseFare;
@Column(name="credit_base_fare_base")
private double creditBaseFareBase;
@Column(name="credit_currency", length=5)
private String creditCurrency;
@Column(name="credit_land_fare")
private double creditLandFare;
@Column(name="credit_land_fare_base")
private double creditLandFareBase;
@Column(name="credit_other_tax")
private double creditOtherTax;
@Column(name="credit_other_tax_base")
private double creditOtherTaxBase;
@Column(name="credit_tot_tax")
private double creditTotTax;
@Column(name="credit_tot_tax_base")
private double creditTotTaxBase;
@Column(name="credit_yqyrtax")
private double creditYqyrtax;
@Column(name="credit_yqyrtax_base")
private double creditYqyrtaxBase;
@Column(name="eticket_number", length=15)
private String eticketNumber;
@Column(name="flight_included", length=3)
private String flightIncluded;
@Column(length=3)
private String packagetype;
@Column(name="passenger_type", length=5)
private String passengerType;
@Column(name="payment_category", length=10)
private String paymentCategory;
@Column(name="pkg_name", length=40)
private String pkgName;
@Column(name="pkg_reservation_number", length=15)
private String pkgReservationNumber;
@Column(name="portal_id", length=50)
private String portalId;
@Column(name="pos_agent_commision")
private double posAgentCommision;
@Column(name="pos_agent_commision_percentage")
private double posAgentCommisionPercentage;
@Column(name="pos_gsa_commision")
private double posGsaCommision;
@Column(name="pos_gsa_commision_percentage")
private double posGsaCommisionPercentage;
@Column(name="pos_total_commision")
private double posTotalCommision;
@Column(name="poslocation_currency", length=4)
private String poslocationCurrency;
@Column(name="poslocation_currency_id")
private Integer poslocationCurrencyId;
@Column(name="poslocation_id")
private Integer poslocationId;
@Column(name="poslocation_name", length=50)
private String poslocationName;
@Column(name="product_type", length=1)
private String productType;
@Column(length=50)
private String region;
@Column(name="region_id")
private Integer regionId;
private Integer resconfirmid;
@Temporal(TemporalType.DATE)
@Column(name="reservation_date")
private Date reservationDate;
@Column(name="reservation_no", length=15)
private String reservationNo;
private Integer respkgconfirmid;
@Column(length=2)
private String resstatus;
@Column(name="supconf_number", length=20)
private String supconfNumber;
@Column(name="total_cancelation_charge")
private double totalCancelationCharge;
@Column(name="total_cancelation_charge_base")
private double totalCancelationChargeBase;
private double yqyrtax;
@Column(name="yqyrtax_base")
private double yqyrtaxBase;
public sampleSalesReport() {
}
/*Note:Getters and setters for above mentioned fields have been omitted for clarity*/
}
/ sampleSalesReportRepository(注意:这个不能自动装配并返回Null) /
package samples.reportdata.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import samples.reportdata.pojo.sampleSalesReport;
@Repository
public interface sampleSalesReportRepository extends JpaRepository<sampleSalesReport, Integer> {
List<sampleSalesReport> findAll();
sampleSalesReport findOne(Integer id);
}
服务
package samples.reportdata.reports;
import java.util.Date;
import java.util.List;
import javax.xml.ws.soap.Addressing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import samples.reportdata.pojo.sampleSalesReport;
import samples.reportdata.repository.sampleSalesReportRepository;
@ComponentScan
public class sampleSalesReport_Observer extends BaseReport {
@Override
public void update() throws Exception {
System.out.println("sampleSalesReport_Observer " + this.getSubject().getData());
this.setMessage("message");
try {
createSampleSalesRecord();
} catch (Exception e) {
e.printStackTrace();
}
}
/*Note:This is the Object that does not get auto wired*/
@Autowired
private sampleSalesReportRepository sampleSalesReportRepository;
/*Save Sample Record to DB*/
public List<sampleSalesReport> createSampleSalesRecord() {
//Note:sample data set to check JPA operation
sampleSalesReport sampleSalesReport= new sampleSalesReport();
Date dNow = new Date( );
sampleSalesReport.setId(6);
sampleSalesReport.setProductType("F");
sampleSalesReport.setResconfirmid(123);
sampleSalesReport.setRegion("Asia");
sampleSalesReport.setRegionId(21);
sampleSalesReport.setCountryName("Sri Lanka");
sampleSalesReport.setCountryId(123);
sampleSalesReport.setCityId(22);
sampleSalesReport.setCityName("Colombo");
sampleSalesReport.setPoslocationName("Pos_colombo");
sampleSalesReport.setPoslocationId(100);
sampleSalesReport.setBookingChannel("CC");
sampleSalesReport.setPoslocationCurrency("LKR");
sampleSalesReport.setPoslocationCurrencyId(180);
sampleSalesReport.setReservationDate(dNow);
sampleSalesReport.setFlightIncluded("F");
sampleSalesReport.setPkgReservationNumber("P1234567");
sampleSalesReport.setPackagetype("D");
sampleSalesReport.setCombination("FHA");
sampleSalesReport.setReservationNo("F0001W090308");
sampleSalesReport.setEticketNumber("1571234567890");
sampleSalesReport.setPkgName("FlightOne");
sampleSalesReport.setPaymentCategory("Credit");
sampleSalesReport.setSupconfNumber("PAY-12345678");
sampleSalesReport.setPassengerType("ADT");
sampleSalesReport.setPosAgentCommisionPercentage(10.00);
sampleSalesReport.setPosAgentCommision(20.33);
sampleSalesReport.setPosAgentCommisionPercentage(12.00);
sampleSalesReport.setPosAgentCommision(30.66);
sampleSalesReport.setPosTotalCommision(50.99);
sampleSalesReport.setResstatus("N");
sampleSalesReport.setCashCurrency("LKR");
sampleSalesReport.setCashLandFare(100.00);
sampleSalesReport.setCashBaseFare(100.00);
sampleSalesReport.setCashTotTax(220.00);
sampleSalesReport.setCashYqyrtax(10.00);
sampleSalesReport.setCashOtherTax(10.00);
sampleSalesReport.setCashLandFareBase(100.00);
sampleSalesReport.setCashBaseFareBase(100.00);
sampleSalesReport.setCashTotTaxBase(220.00);
sampleSalesReport.setCashYqyrtaxBase(10.00);
sampleSalesReport.setCashOtherTaxBase(10.00);
sampleSalesReport.setCreditCurrency("OMR");
sampleSalesReport.setCreditLandFare(200.00);
sampleSalesReport.setCreditBaseFare(200.00);
sampleSalesReport.setCreditTotTax(440.00);
sampleSalesReport.setCreditYqyrtax(20.00);
sampleSalesReport.setCreditOtherTax(20.00);
sampleSalesReport.setCreditLandFareBase(200.00);
sampleSalesReport.setCreditBaseFareBase(200.00);
sampleSalesReport.setCreditTotTaxBase(440.00);
sampleSalesReport.setCreditYqyrtaxBase(20.00);
sampleSalesReport.setCreditOtherTaxBase(20.00);
sampleSalesReport.setYqyrtax(40.00);
sampleSalesReport.setYqyrtaxBase(80.00);
sampleSalesReport.setCancelCharges(1.00);
sampleSalesReport.setCancelChargesBase(2.00);
sampleSalesReport.setTotalCancelationCharge(1.00);
sampleSalesReport.setTotalCancelationChargeBase(2.00);
/*Note: This is the line the nullpoint is thrown for sampleSalesReportRepository*/
sampleSalesReportRepository.save(sampleSalesReport);
return sampleSalesReportRepository.findAll();
}
}
答案 0 :(得分:2)
对于课程@Component
,您应该使用@ComponentScan
代替sampleSalesReport_Observer
。
答案 1 :(得分:0)
您在@ComponentScan
上应用了错误的注释sampleSalesReport_Observer
。
将其删除并改为使用@Component
。
@Component
时, component
会使其有资格被视为component
,在此情况下,sampleSalesReport_Observer
为autowired
一旦发现所有必需的依赖项/ bean /其他注册组件将根据您的要求sampleSalesReportRepository
。在您的情况下,sampleSalesReport_Observer
失败,因为component
从未被检测为using Windows.UI.Xaml;
public class Foo
{
DispatcherTimer dispatcherTimer;
public void StartTimers()
{
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
}
// callback runs on UI thread
void dispatcherTimer_Tick(object sender, object e)
{
// execute repeating task here
}
}
。