有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

春季组织。springframework。aop。支持奥普提尔。invokeJoinpointUsingReflection(AopUtils.java:317)

我正在从文件导入数据,它成功地获取了数据。我的流程是从主类调用ruleimplservice,在主类中我从文件中获取值。bt当我调用jdbclogdatarepository来保存数据时,它显示以下错误。数据在impl bt中成功读取,而不是在jdbc类中

错误

java.lang.NullPointerException
    at com.heavymeddlellc.heavymeddle.metrics.service.RuleServiceImpl.insertlogcontent(RuleServiceImpl.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at $Proxy6.insertlogcontent(Unknown Source)
    at com.heavymeddlellc.heavymeddle.utils.readingdata.main(readingdata.java:97)

Java代码如下所示:

 package com.heavymeddlellc.heavymeddle.utils;

        import java.io.BufferedReader;
        import java.io.FileReader;
        import java.util.ArrayList;
        import java.util.List;
        import java.util.regex.Pattern;
       import org.springframework.beans.factory.BeanFactory;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.xml.XmlBeanFactory;
        import org.springframework.core.io.ClassPathResource;
        import org.springframework.core.io.Resource;
        import org.springframework.test.context.ContextConfiguration;

        import com.heavymeddlellc.heavymeddle.metrics.domain.LogData;
        import com.heavymeddlellc.heavymeddle.metrics.repository.jdbc.JdbcLogDataRepository;
        import com.heavymeddlellc.heavymeddle.metrics.service.RuleService;
        import com.heavymeddlellc.heavymeddle.metrics.service.RuleServiceImpl;

        @ContextConfiguration(locations = "/hm-metrics-context.xml")
        public class readingdata 
        {



            public static void main(String args[])
            {
              //RuleService ruleService=new RuleService();


                     List<LogData> dataList = new ArrayList<LogData>();  
                Resource r= new ClassPathResource("/hm-metrics-context.xml");
                BeanFactory factory=new XmlBeanFactory((org.springframework.core.io.Resource) r);
                // LogData logData=(LogData) factory.getBean("ruleService");
                RuleService ruleService=(RuleService) factory.getBean("ruleService");
                // JdbcLogDataRepository dataRepository= (JdbcLogDataRepository) factory.getBean("ruleService");

                     LogData logData=new LogData();

                // public List<LogData> inserttable(LogData logData2)
                 //{
                 try
                 {
                  BufferedReader reader=new BufferedReader(new FileReader("E://tracker.log"));
                  String lines;
                  String[] contentid;
                  while((lines=reader.readLine())!=null)
                  {
                      String[] datas=lines.split(Pattern.quote("|"));

                      logData.setUserId(datas[0]);

                      logData.setRequestDate(datas[1]);
                      System.out.println(datas[1]);
                      logData.setSessionId(datas[2]);
                      System.out.println(datas[2]);
                     // System.out.println(datas[2]);
                     contentid=datas[2].split("html/meta/content/");
                    // System.out.println(contentid[0]);
                    // System.out.println(datas[2]);  
                    logData.setContentId(contentid[0]);  
                      System.out.println(contentid[0]);
                      logData.setUserAgent(datas[6]);
                      System.out.println(datas[6]);
                      logData.setUserType(datas[4]);

                      logData.setReferer(datas[5]);
                      logData.setRedirectUrl(datas[7]);
                      String sessionId=logData.getSessionId();
                      System.out.println(logData.getSessionId());
                      String contentId=logData.getContentId();
                      System.out.println(contentId);
                      String userAgent=logData.getUserAgent();
                      System.out.println(userAgent);
                      String requestDate=logData.getRequestDate();
                      String userId=logData.getUserId();
                      String userType=logData.getUserType();
                      String referer=logData.getReferer();
                      String redirectUrl=logData.getRedirectUrl();
                     //Saystem.out.println(datas[4]);
                     // dataList.add(logData);
                     // System.out.print(logData.getSessionId());
                      //ruleService.insertlogcontent(logData.setSessionId(datas[2]),logData.setContentId(contentid[0]), logData.setRequestDate(datas[1]), logData.setUserId(datas[0]),logData.setUserType(datas[4]),logData.setReferer(datas[5]),logData.setRedirectUrl(datas[7]));
                      ruleService.insertlogcontent(sessionId, contentId, userAgent, requestDate, userId, userType, referer, redirectUrl);
                  }



                 }
                catch(Exception e)
                {
                  e.printStackTrace();
                }
            //  return dataList;


            // }

            }}
        /**
     * 
     */



ruleServiceimpls
     package com.heavymeddlellc.heavymeddle.metrics.service;

        import java.util.List;

        import org.apache.log4j.Logger;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.*;
        import org.springframework.transaction.annotation.Transactional;

        import com.heavymeddlellc.heavymeddle.metrics.domain.LogData;
        import com.heavymeddlellc.heavymeddle.metrics.repository.LogDataRepository;


        @Repository
        public class RuleServiceImpl implements RuleService {
            private static final Logger log = Logger.getLogger(RuleServiceImpl.class);

            @Autowired
            private LogDataRepository logDataRepository;



        @Override
        public void insertlogcontent(String sessionId, String contentId, String userAgent,
                String requestDate, String userId,
                String userType, String referer,
                String redirectUrl) {

            //logDataRepository.insertlogcontent(sessionId, contentId,userAgent, requestDate,  userId, userType, referer, redirectUrl);
            System.out.println(sessionId);
            System.out.println(requestDate);
            System.out.println(userType);
            logDataRepository.insertlogcontent(sessionId, contentId, userAgent, 
                    requestDate, userId, userType, referer, redirectUrl);
        }




}











/**
 * 
 */


 package com.heavymeddlellc.heavymeddle.metrics.repository.jdbc;

    import java.sql.Connection;
    import java.sql.Date;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;

    import com.heavymeddlellc.heavymeddle.metrics.MetricsException;
    import com.heavymeddlellc.heavymeddle.metrics.domain.LogData;
    import com.heavymeddlellc.heavymeddle.metrics.repository.LogDataRepository;
    import com.heavymeddlellc.heavymeddle.utils.readingdata;


    @Transactional
    @Repository("logDataRepository")
    public class JdbcLogDataRepository implements LogDataRepository {

        @Autowired
        private JdbcTemplate jdbcTemplate;

        @Autowired


        private final static String LOG_REDIRECT_VIEW_TABLENAME = "hm_log_redirect_view";
        private final static String LOG_REDIRECT_URL_LOOKUP_TABLENAME = "hm_log_redirect_url_lookup";
        public final static String LOG_ALL_CONTENTS_VIEW_TABLENAME = "hm_log_all_contents_view";
        private final static String LOG_ALL_SN_CONTENTS_VIEW = "hm_log_sn_contents_view";




        @Override
        public void insertlogcontent(String sessionId, String contentId, String userAgent,
                String requestDate, String userId,
                String userType, String referer,
                String redirectUrl) {

            int values=0;
            System.out.println(userType);
            StringBuffer sqlQuery = new StringBuffer(
                    "insert into  "+JdbcLogDataRepository.LOG_ALL_CONTENTS_VIEW_TABLENAME
                    +"(uri,content_id,content_owner_id,request_date,user_id,user_type,referer,redirect_url) values (?,?,?,?,?,?,?,?)");
            values=jdbcTemplate.update(sqlQuery.toString(),sessionId,contentId,userAgent,requestDate,userId,userType,referer,redirectUrl);
        }
    }

Spring xml

<!-- Activates scanning of @Autowired -->
    <context:annotation-config />

    <context:component-scan base-package="com.heavymeddlellc.heavymeddle.metrics" />

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>

    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- (this dependency is defined somewhere else) -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="metricsProcessor" class="com.heavymeddlellc.heavymeddle.metrics.processor.MetricsProcessorImpl">
        <constructor-arg ref="taskExecutor" />
        <property name="ruleService" ref="ruleService"/>
    </bean>

    <bean id="ruleService" scope="prototype" class="com.heavymeddlellc.heavymeddle.metrics.service.RuleServiceImpl">
        <!-- This instructs the container to proxy the current bean-->
        <aop:scoped-proxy proxy-target-class="false"/>
    </bean>

    <!-- <bean id="logDataRepository" scope="prototype" class="com.heavymeddlellc.heavymeddle.metrics.repository.JdbcLogDataRepository">
        This instructs the container to proxy the current bean
        <aop:scoped-proxy proxy-target-class="false"/>
        <property name="ruleService" ref="ruleService"></property>
    </bean>
     -->
    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="50" />
        <property name="maxPoolSize" value="100" />
        <property name="queueCapacity" value="150" />
    </bean>

</beans> 

共 (1) 个答案

  1. # 1 楼答案

    更换以下两行:

    Resource r = new ClassPathResource("/hm-metrics-context.xml");
    BeanFactory factory = new XmlBeanFactory((org.springframework.core.io.Resource) r);
    

    有了这个:

    ApplicationContext factory = new ClassPathXmlApplicationContext("/hm-metrics-context.xml");
    

    阅读参考文档中关于BeanFactoryApplicationContext之间差异的this section。 您的自动连线无法工作,因为<context:annotation-config/>被处理为BeanPostProcessor,而BeanFactory缺少使用BeanPostProcessor扩展点的功能,如上面文档中的链接所示