使用注释的Spring JDBCtemplate将为null

时间:2015-04-10 06:47:31

标签: java spring spring-jdbc

我是初学DAO和弹簧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即将为空

1 个答案:

答案 0 :(得分:2)

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

如果您正在使用

获取Spring bean
new Test()

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

阅读the documentation