gpt4 book ai didi

java - 使用注释的 Spring JDBC 模板为 null

转载 作者:行者123 更新时间:2023-12-02 04:47:24 25 4
gpt4 key购买 nike

我是 spring DAO 和 spring JDBC 模板的新手,在下面的代码中,我在 JDBC 模板中得到 null,并且我检查了一些示例,并且我的代码中的一切看起来都很好。如果我错过了什么,请告诉我

类代码:

import com.bnym.repo.rule.Deal;
import com.bnym.repo.rule.Security;
@Repository
public class Test {
@Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Resource(name = "namedParameterJdbcTemplate")
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

Deal deal= new Deal();
public static void main(String[] args) {

Security sec1 = new Security();
Set<String> sec1Index = new HashSet<String>();
sec1Index.add("NP");
sec1.setSecurityCategory("Fixed Income");
sec1.setBroadType("sdsdfs");
sec1.setCountry("fsdf");
sec1.setCurrency("AUD");
sec1.setIndex(sec1Index);
sec1.setIssuerGroup("AAAAA");
sec1.setIssuerName("issuerName");
sec1.setIssuerRating(10);
sec1.setIssueSize(111111);
sec1.setMarketCap(11111);
sec1.setMonthsToMaturity(10);
sec1.setRepoSecurityID("111");
sec1.setRepoSecurityIdType("repoSecurityIdType");
sec1.setSecurityPrice(22222222);
sec1.setSecurityRating(88);
sec1.setSecuritySubType("Stripped Government Bond");
sec1.setTaxDocReceived(true);
sec1.setUnratedSecurities(false);

Security sec2 = new Security();
sec2.setSecurityCategory("dasdas");
sec2.setBroadType("Bond");
sec2.setCountry("Japan");
sec2.setCurrency("AUD");
Set<String> sec2Index = new HashSet<String>();
sec2Index.add("NP");
sec2.setIndex(sec2Index);
sec2.setIssuerGroup("AAAAA");
sec2.setIssuerName("issuerName");
sec2.setIssuerRating(10);
sec2.setIssueSize(111111);
sec2.setMarketCap(11111);
sec2.setMonthsToMaturity(10);
sec2.setRepoSecurityID("222");
sec2.setRepoSecurityIdType("repoSecurityIdType");
sec2.setSecurityPrice(22222222);
sec2.setSecurityRating(10);
sec2.setSecuritySubType("Stripped Government Bond");
sec2.setTaxDocReceived(true);
sec2.setUnratedSecurities(false);

Security sec3 = new Security();
sec3.setSecurityCategory("dasdas");
sec3.setBroadType("Bond");
sec3.setCountry("Italy");
sec3.setCurrency("sdfsd");
Set<String> sec3Index = new HashSet<String>();
sec3Index.add("NP");
sec3.setIndex(sec3Index);
sec3.setIssuerGroup("AAAAA");
sec3.setIssuerName("issuerName");
sec3.setIssuerRating(10);
sec3.setIssueSize(111111);
sec3.setMarketCap(11111);
sec3.setMonthsToMaturity(10);
sec3.setRepoSecurityID("333");
sec3.setRepoSecurityIdType("repoSecurityIdType");
sec3.setSecurityPrice(22222222);
sec3.setSecurityRating(10);
sec3.setSecuritySubType("Stripped Government Bond");
sec3.setTaxDocReceived(true);
sec3.setUnratedSecurities(false);

Security sec4 = new Security();
sec4.setSecurityCategory("dasdas");
sec4.setBroadType("EQTY");
sec4.setCountry("Portugal");
sec4.setCurrency("sdfsd");
Set<String> sec4Index = new HashSet<String>();
sec4Index.add("NP");
sec4.setIndex(sec4Index);
sec4.setIssuerGroup("AAAAA");
sec4.setIssuerName("issuerName");
sec4.setIssuerRating(10);
sec4.setIssueSize(111111);
sec4.setMarketCap(11111);
sec4.setMonthsToMaturity(10);
sec4.setRepoSecurityID("444");
sec4.setRepoSecurityIdType("repoSecurityIdType");
sec4.setSecurityPrice(22222222);
sec4.setSecurityRating(10);
sec4.setSecuritySubType("Common Stock");
sec4.setTaxDocReceived(false);
sec4.setUnratedSecurities(false);

Security sec5 = new Security();
sec5.setSecurityCategory("Fixed Income");
sec5.setBroadType("sdsdfs");
sec5.setCountry("fsdf");
sec5.setCurrency("AUD");
Set<String> sec5Index = new HashSet<String>();
sec5Index.add("NP");
sec5.setIndex(sec5Index);
sec5.setIssuerGroup("Dexia SA");
sec5.setIssuerName("issuerName");
sec5.setIssuerRating(10);
sec5.setIssueSize(111111);
sec5.setMarketCap(11111);
sec5.setMonthsToMaturity(10);
sec5.setRepoSecurityID("555");
sec5.setRepoSecurityIdType("repoSecurityIdType");
sec5.setSecurityPrice(22222222);
sec5.setSecurityRating(88);
sec5.setSecuritySubType("Stripped Government Bond");
sec5.setTaxDocReceived(true);
sec5.setUnratedSecurities(false);

Security sec6 = new Security();
sec6.setSecurityCategory("dasdas");
sec6.setBroadType("Bond");
sec6.setCountry("dsds");
sec6.setCurrency("sdfsd");
Set<String> sec6Index = new HashSet<String>();
sec6Index.add("NP");
sec6.setIndex(sec6Index);
sec6.setIssuerGroup("AAAAA");
sec6.setIssuerName("issuerName");
sec6.setIssuerRating(10);
sec6.setIssueSize(111111);
sec6.setMarketCap(11111);
sec6.setMonthsToMaturity(10);
sec6.setRepoSecurityID("666");
sec6.setRepoSecurityIdType("repoSecurityIdType");
sec6.setSecurityPrice(22222222);
sec6.setSecurityRating(10);
sec6.setSecuritySubType("Supranational Bond");
sec6.setTaxDocReceived(true);
sec6.setUnratedSecurities(false);

Security sec7 = new Security();
sec7.setSecurityCategory("Equities");
sec7.setBroadType("EQTY");
sec7.setCountry("Australia");
sec7.setCurrency("AUD");
Set<String> sec7Index = new HashSet<String>();
sec7Index.add("S&P/ASX20");
sec7.setIndex(sec7Index);
sec7.setIssuerGroup("AAAAA");
sec7.setIssuerName("issuerName");
sec7.setIssuerRating(10);
sec7.setIssueSize(111111);
sec7.setMarketCap(11111);
sec7.setMonthsToMaturity(10);
sec7.setRepoSecurityID("777");
sec7.setRepoSecurityIdType("repoSecurityIdType");
sec7.setSecurityPrice(22222222);
sec7.setSecurityRating(10);
sec7.setSecuritySubType("Common Stock");
sec7.setTaxDocReceived(true);
sec7.setUnratedSecurities(false);

Security sec8 = new Security();
sec8.setSecurityCategory("Equities");
sec8.setBroadType("EQTY");
sec8.setCountry("Australia");
sec8.setCurrency("AUD");
Set<String> sec8Index = new HashSet<String>();
sec8Index.add("S&P/ASX20");
sec8.setIndex(sec8Index);
sec8.setIssuerGroup("AAAAA");
sec8.setIssuerName("issuerName");
sec8.setIssuerRating(10);
sec8.setIssueSize(111111);
sec8.setMarketCap(11111);
sec8.setMonthsToMaturity(10);
sec8.setRepoSecurityID("888");
sec8.setRepoSecurityIdType("repoSecurityIdType");
sec8.setSecurityPrice(22222222);
sec8.setSecurityRating(10);
sec8.setSecuritySubType("Common Stock");
sec8.setTaxDocReceived(true);
sec8.setUnratedSecurities(false);
List<Security> securities= new ArrayList<Security>();

securities.add(sec1);
securities.add(sec2);
securities.add(sec3);
securities.add(sec4);
securities.add(sec5);
securities.add(sec6);
securities.add(sec7);
securities.add(sec8);
Deal deal = new Deal();
Test t =new Test();
t.checkTaxRule(securities);


}



public List<Security> checkTaxRule(List<Security> securities){
int count= 0;
String reason="fail due to tax rule";
List<Security> passsecurities = null;
MapSqlParameterSource params = new MapSqlParameterSource();
List<Map<String,Object>> rows = null;
params.addValue("userId", "3h9author1");
final String query="SELECT * FROM T_ECT_USER WHERE USER_ID=:userId ";

rows = namedParameterJdbcTemplate.queryForList(query, params);
for(Security security: securities){
for(Map<String,Object> rule :rows){
if(security.getCountry()==rule.get("COUNTRY_CODE") && security.getSecuritySubType()==rule.get("SECURITY_SUBTYPE")){
deal.addEligibleSecurity(security, reason);
break;
}
else{
count++;
}
if(count==rows.size()){
deal.addNonEligibleSecurity(security, reason);
}

}


}


return null;

}
}

我将其作为 java 应用程序运行(作为 java 应用程序运行)

下面是配置文件:

<?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:sec="http://www.springframework.org/schema/security" xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd ttp://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- property files -->
<ctx:property-placeholder location="classpath*:*.properties" />
<!-- database access -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="pool" class="org.apache.commons.pool.impl.GenericObjectPool">
<property name="minEvictableIdleTimeMillis">
<value>300000</value>
</property>
<property name="timeBetweenEvictionRunsMillis">
<value>60000</value>
</property>
</bean>
<bean id="dsConnectionFactory" class="org.apache.commons.dbcp.DataSourceConnectionFactory">
<constructor-arg>
<ref bean="dataSource" />
</constructor-arg>
</bean>
<bean id="poolableConnectionFactory" class="org.apache.commons.dbcp.PoolableConnectionFactory">
<constructor-arg index="0">
<ref bean="dsConnectionFactory" />
</constructor-arg>
<constructor-arg index="1">
<ref bean="pool" />
</constructor-arg>
<constructor-arg index="2">
<null />
</constructor-arg>
<constructor-arg index="3">
<null />
</constructor-arg>
<!-- read only -->
<constructor-arg index="4">
<value>false</value>
</constructor-arg>
<!-- auto commit -->
<constructor-arg index="5">
<value>true</value>
</constructor-arg>
</bean>
<bean id="pooledDS" class="org.apache.commons.dbcp.PoolingDataSource" depends-on="poolableConnectionFactory">
<constructor-arg>
<ref bean="pool" />
</constructor-arg>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg index="0">
<ref bean="pooledDS" />
</constructor-arg>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg index="0">
<ref bean="pooledDS" />
</constructor-arg>
</bean>

<bean id="commonDao" class="com.bnym.cm.common.dao.CommonDao" />
<bean id="idFactory" class="com.bnym.cm.common.impl.IdFactory" />
<bean id="exceptionHandler" class="com.bnym.cm.common.service.ExceptionHandler" />
<bean id="messageListener" class="com.bnym.cm.common.jms.CMMessageListener" />
<bean id="messageSender" class="com.bnym.cm.common.jms.CMMessageSender" />

<bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName" value="${jms.hostname.local}" />
<property name="port" value="${jms.port.local}" />
<property name="queueManager" value="${jms.queue.manager.local}" />
</bean>

<bean id="jmsQueueConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="mqConnectionFactory" />
</bean>

<bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.DynamicDestinationResolver" />

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsQueueConnectionFactory" />
<property name="destinationResolver" ref="jmsDestinationResolver" />
<property name="defaultDestinationName" value="${jms.queue.rep.local}" />
<property name="pubSubDomain" value="false" />
<property name="receiveTimeout" value="20000" />
</bean>

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsQueueConnectionFactory"/>
<property name="destinationName" value="${jms.queue.esb.local}"/>
<property name="messageListener" ref="messageListener" />
</bean>

<beans profile="Dev">
<bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName" value="${jms.hostname.Dev}" />
<property name="port" value="${jms.port.Dev}" />
<property name="queueManager" value="${jms.queue.manager.Dev}" />
<property name="transportType" value="${jms.transport.type.Dev}" />
<property name="channel" value="${jms.client.channel.Dev}" />
</bean>

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsQueueConnectionFactory" />
<property name="destinationResolver" ref="jmsDestinationResolver" />
<property name="defaultDestinationName" value="${jms.queue.rep.Dev}" />
<property name="pubSubDomain" value="false" />
<property name="receiveTimeout" value="20000" />
</bean>

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsQueueConnectionFactory"/>
<property name="destinationName" value="${jms.queue.esb.Dev}"/>
<property name="messageListener" ref="messageListener" />
</bean>
</beans>

<beans profile="QA">
<bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName" value="${jms.hostname.QA}" />
<property name="port" value="${jms.port.QA}" />
<property name="queueManager" value="${jms.queue.manager.QA}" />
<property name="transportType" value="${jms.transport.type.QA}" />
<property name="channel" value="${jms.client.channel.QA}" />
</bean>

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsQueueConnectionFactory" />
<property name="destinationResolver" ref="jmsDestinationResolver" />
<property name="defaultDestinationName" value="${jms.queue.rep.QA}" />
<property name="pubSubDomain" value="false" />
<property name="receiveTimeout" value="20000" />
</bean>

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsQueueConnectionFactory"/>
<property name="destinationName" value="${jms.queue.esb.QA}"/>
<property name="messageListener" ref="messageListener" />
</bean>
</beans>
</beans>

namedParameterJdbcTemplate 为 null

最佳答案

您的 main 方法永远不会创建任何 Spring 上下文,并且不会从 Spring 上下文获取测试 bean。因此 Spring 无法神奇地知道您已经创建了一个测试实例并在其中注入(inject) JDBC 模板。

如果您使用

获取 Spring bean
new Test()

正如您所做的那样,它不是 Spring bean。它只是 Spring 不知道的常规 Java 对象。

阅读the documentation

关于java - 使用注释的 Spring JDBC 模板为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29555079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com