`
y806839048
  • 浏览: 1083312 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

springmvc spring 两套上下文问题

阅读更多

spring  mvc 和spring的两套上下文,

1,spring不可以拿到mvc的bean,mvc可以那spring的bean,因为spring是以监听的方式最早初始化,sevlet 会稍晚,此时mvc还没有bean,

这种需要再用<bean>配置一下,不能直接用注解生成的(这里这个定时任务之前放在controller层,这个controller作为bean注入(拿不到springmvc的这个注解的bean),后面直接放在一个普通的spring扫描的目录,更纯净)

 

另:rabbitMq等这些用监听形式配置的bean会先于dispatch.xml中的bean扫描,后期不会再更新所以,当这个bean中注入其他bean的时候会一直拿不到

 

这种采用java硬编码显示的从spring上下文拿

 

定时任务实例:

 

 

 

<!--与电子商务系统的接口定时从FTP上导出导入-->
<bean id="fInterfaceAccountInfo" class = "com.houbank.incoming.web.util.FInterfaceAccountInfo"></bean>
<!-- 定时任务:客户理财提交接口 -->
<bean id="t1Order"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject">
        <ref bean="fInterfaceAccountInfo" />
    </property>
    <property name="targetMethod">
        <value>getT1Order</value>  <!--  要执行的方法名称  -->
</property>
</bean>
<!-- ======================== 调度触发器         五分钟更新一次上行表数据======================== -->
<bean id="cronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="t1Order"></property>
    <!-- 每5分钟一次调度 -->
<property name="cronExpression" value="0 0/5 * * * ?" />

</bean>

<!-- ======================== 调度工厂 ======================== -->
<bean id="SpringJobSchedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="cronTriggerBean" />
        </list>
    </property>
</bean>
package com.houbank.incoming.web.util;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.houbank.basic.util.page.Pagination;
import com.houbank.basic.util.response.BaseResponse;
import com.houbank.incoming.api.FinancialSalesSMSMqMessageFacade;
import com.houbank.incoming.api.FinancialSalesUserFacade;
import com.houbank.incoming.common.BaseResponseInterface;
import com.houbank.incoming.model.condition.*;
import com.houbank.incoming.model.domain.*;
import com.houbank.incoming.web.redis.RedisTemplateDelegate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
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.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: yy  Date: 2018/3/26 Time: 14:26
 */

@Slf4j
public class FInterfaceAccountInfo {

    @Autowired
    private RestTemplate restTemplate1;

    @Autowired(required=false)
    private RedisTemplate redisTemplate;

    @Reference
    private FinancialSalesUserFacade financialSalesUserFacade;

    @Reference
    private FinancialSalesSMSMqMessageFacade financialSalesSMSMqMessageFacade;

    @Value("${accountInfo}")
    private String  accountInfo;
    @Value("${couponInfo}")
    private String couponInfo;
    @Value("${userOrderInfo}")
    private String userOrderInfo;
    @Value("${userRechargeInfo}")
    private String userRechargeInfo;
    @Value("${userInfo}")
    private String userInfo;
    @Value("${userInvest}")
    private String userInvest;
    @Value("${userWithdraw}")
    private String userWithdraw;
    @Value("${userTransFlow}")
    private String userTransFlow;
    @Value("${userPlan}")
    private String userPlan;



    public BaseResponseInterface<List<FinancialInterfacePlan>>  getT1Order() {
        RedisLock lock = new RedisLock(redisTemplate, "T1_quarz_key", 600000, 800000);
        BaseResponseInterface<List<FinancialInterfacePlan>> stren=null;
        try {
            if(lock.lock()) {
        ParameterizedTypeReference<BaseResponseInterface<List<FinancialInterfacePlan>>> responseType = new ParameterizedTypeReference<BaseResponseInterface<List<FinancialInterfacePlan>>>() {
            @Override
            public Type getType() {
                return super.getType();
            }
        };
        FinancialInterfaceBaseCondition bc = new FinancialInterfaceBaseCondition();
        ResponseEntity<BaseResponseInterface<List<FinancialInterfacePlan>>> str=null;

        try {
            /**
             * 设置请求头
             */
HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
            headers.add("Accuept", MediaType.APPLICATION_JSON.toString());
//            bc = new FinancialInterfaceCouponCondition();
bc.setAppid("10001");
            bc.setSecret("C34E6AD237D511E8B4670ED5F89F718B");
            long ct=System.currentTimeMillis();
            bc.setTs(ct+29*60*1000);
            String tc= AccountInterfaceUtil.isOk(bc);
            bc.setTicket(tc);
            SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date date=sDateFormat.parse("2019-11-11 00:00:00");
//            bc.setEndTime(date);
//            Date date1=sDateFormat.parse("2010-11-11 00:00:00");
//            bc.setExpiryTimeFrom(date1);
//            Date date2=sDateFormat.parse("2019-11-11 00:00:00");
//            bc.setExpiryTimeTo(date2);
//            bc.setPageIndex("1");
//            bc.setPageSize("20");
//            Date date3=sDateFormat.parse("2010-11-11 00:00:00");
//            bc.setStartTime(date3);
//            bc.setUserId("2210983");
            /**
             * POST请求参数,根据需要进行封装
             */
String bodyData = new String(Base64Utils.encode(JSON.toJSONString(bc).getBytes("UTF-8")));

            /**
             * 查看HttpEntity的构造方法,包含只有请求头和只有请求体的情况---请求实体参数
             */
HttpEntity<FinancialInterfaceBaseCondition> httpEntity = new HttpEntity<FinancialInterfaceBaseCondition>(bc, headers);
//            str = restTemplate1.exchange(
//                    "http://localhost:8080/hb_phonebank_web/FinancialInterfaceAccountInfo/RateQuery/resultAc",
//                    HttpMethod.POST, httpEntity, responseType);
str = restTemplate1.exchange(userPlan,HttpMethod.POST, httpEntity, responseType);
            System.out.println(str);
            stren = str.getBody();
        }catch (Exception e) {
            log.error("错误"+e);
        }

            }

        }catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

        return stren;

    }

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics