我刚接触Spring Data JPA。我在加载bean时遇到问题而没有使用ApplicationContext或ClassPathXmlApplicationContext。因为在我当前的项目中我有一个旧的spring相关的jar无法删除。因为我当前的" SpringContext .XML"无法加载。这是我的代码阻止加载SpringContext.xml
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringContext.xml");
RegistrationBean registrationBean = (RegistrationBean)context.getBean("registrationBean");
这是我的Repository
的RegistrationBean@Component
public class RegistrationBean {
@Autowired
private CreditorProfileRepository creditorProfileReposigtory;
public RegistrationBean(){
}
public CreditorProfileRepository getCreditorProfileReposigtory() {
return creditorProfileReposigtory;
}
public void setEmployeeReposigtory(CreditorProfileRepository creditorProfileReposigtory) {
this.creditorProfileReposigtory = creditorProfileReposigtory;
}
}
这是我的存储库
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CreditorProfileRepository extends CrudRepository <CreditorProfile, String>{
public CreditorProfile findBympcreditorprofileidpk(String mpcreditorprofileidpk);
}
所以我教过使用构造函数注入来在我的应用程序类中加载registrationBean。这是我的应用程序类
public class CreateCreditorProfile implements ICreateCreditorProfile{
RegistrationBean repositoryRegistration;
CreateCreditorProfile(RegistrationBean repositoryRegistration){
this.repositoryRegistration=repositoryRegistration;
}
CreateCreditorProfile(){
this.repositoryRegistration=repositoryRegistration;
}
@Override
public void createCreditorProfile(CreditorProfileServiceDetails creditorProfileServiceDetails) {
CreditorProfile creditorProfile = new CreditorProfile();
creditorProfile.setcreditorprofileidpk("343243243");
creditorProfile.setpartyid("000005");
creditorProfile.setpartyname("Federico o Peluso");
repositoryRegistration.getCreditorProfileReposigtory().save(creditorProfile);
}
这是我的ApplicationContext.xml
<bean id="registrationBean" class="com.mi.bn.payment.persistence.RegistrationBean" scope="prototype" />
<bean id="CreateCreditorProfile" class="com.mi.bn.paymen.persistence.CreateCreditorProfile" scope="prototype" >
<constructor-arg name="repositoryRegistration" ref="registrationBean" />
</bean>
<bean id="creditorsProfileConfiguration" class="com.mi.bn.payment.domain.configuration.CreditorsProfileConfiguration" scope="prototype">
<constructor-arg name="creater" ref="CreateCreditorProfile" />
</bean>
这是我的SpringContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
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.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- For consider the using of annotations foe defining Spring Bean -->
<context:annotation-config />
<!-- For defining Spring Bean -->
<context:component-scan base-package="com.cs.ed.paymentmessaging.persistence" />
<!-- For bootstrapping the Spring Repository -->
<jpa:repositories base-package="com.cs.ed.paymentmessaging.persistence" />
<!-- Necessary to get the entity manager injected into the factory bean -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="url" value="jdbc:db2://localhost:50000/DD23"/>
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/>
<property name="username" value="db2admin"/>
<property name="password" value="Wasadm1n"/>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.DB2Dialect"/>
</bean>
<!-- Entity Manager Factory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringData"></property>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Enable Transactional Manner -->
<tx:annotation-driven transaction-manager="transactionManager" />
但仍然是我的应用程序类我在
得到空指针异常repositoryRegistration.getCreditorProfileReposigtory()。保存(creditorprofile)
所以,请让我知道解决此问题的方法。
这是堆栈跟踪
11:03:04,725 INFO [CommandExecuter] An Unexpected Error Occurred An Unexpected Error Occurred java.lang.NullPointerException
com.trapedza.bankfusion.core.BankFusionException: An Unexpected Error Occurred java.lang.NullPointerException
at com.misys.bankfusion.subsystem.microflow.runtime.command.ExecuteMicroflowCommand.createBankFusionException(ExecuteMicroflowCommand.java:1224)
at com.misys.bankfusion.subsystem.microflow.runtime.command.ExecuteMicroflowCommand.execute(ExecuteMicroflowCommand.java:674)
at com.misys.bankfusion.command.impl.AbstractCommand.execute(AbstractCommand.java:596)
at com.trapedza.bankfusion.servercommon.commands.AbstractCommand.execute(AbstractCommand.java:392)
at com.misys.bankfusion.command.impl.CommandExecuter.executeCommand(CommandExecuter.java:435)
at com.misys.bankfusion.controller.ExecutionModeHelper.executeDirect(ExecutionModeHelper.java:142)
at com.misys.bankfusion.controller.ExecutionModeHelper.executeCommand(ExecutionModeHelper.java:121)
at com.misys.bankfusion.controller.ExecutionController.executeCommand(ExecutionController.java:121)
at com.misys.bankfusion.controller.CommandUtils.executeCommand(CommandUtils.java:66)
at com.misys.bankfusion.uxp.plugin.BankFusionServicePlugin.processOtherRequests(BankFusionServicePlugin.java:799)
at com.misys.bankfusion.uxp.plugin.BankFusionServicePlugin.executeService(BankFusionServicePlugin.java:191)
at com.misys.uxp.container.servlet.UXServlet.service(UXServlet.java:104)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException
at com.misys.bankfusion.paymentmessaging.persistence.CreateCreditorProfile.createCreditorProfile(CreateCreditorProfile.java:65)
at com.misys.bankfusion.paymentmessaging.domain.configuration.CreditorsProfileConfiguration.createCreditorProfile(CreditorsProfileConfiguration.java:34)
at com.misys.bankfusion.paymentmessaging.controller.fatom.MaintainCreditorProfiles.process(MaintainCreditorProfiles.java:46)
答案 0 :(得分:0)
您的RegistrationBean似乎不是自动装配的:
public class CreateCreditorProfile implements ICreateCreditorProfile{
@Autowired
RegistrationBean repositoryRegistration;
...
}
关于你的问题,为什么构造函数调用不起作用,据我所知,有2个context.xml声明--ApplicationContext.xml和SpringContext.xml。你需要加载它们:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"SpringContext.xml","ApplicationContext.xml");
或者您可以在SpringContext.xml
中导入Application.xml<import resource="ApplicationContext.xml" />
两种方式都应该导致正确加载bean。