`
y806839048
  • 浏览: 1107743 次
  • 性别: 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;

    }

}

 

分享到:
评论

相关推荐

    springmvc与hibernate结合实例

    1. **配置 SpringMVC**:在 `web.xml` 中配置 DispatcherServlet,同时定义 Spring 的上下文配置文件,如 `servlet-context.xml`,在这里配置 MVC 的相关组件,如视图解析器、拦截器等。 2. **配置 Hibernate**:在...

    mybatis-3.4.5、springMVC-5.0.4、mybatis-spring-1.3.2整合包

    然后,配置Spring的上下文和SpringMVC的DispatcherServlet,设置数据源、事务管理器以及MyBatis的相关配置。接下来,定义MyBatis的Mapper接口和Mapper XML文件,编写SQL语句。最后,通过Spring的依赖注入,可以在...

    springmvc+spring

    6. **Web应用上下文**:Spring WebApplicationContext为Web应用提供了特定的上下文,能够加载与Web相关的bean,并且方便地与其他Web组件(如Servlet、Filter)进行交互。 7. **Spring Boot**:Spring Boot是基于...

    springMVC和Maven包

    "springMvc-jars"可能包含SpringMVC的各个组件,如spring-webmvc.jar提供SpringMVC的核心功能,spring-context.jar包含上下文和依赖注入支持,spring-aop.jar支持面向切面编程,以及spring-expression.jar用于表达式...

    java之hibernate和spring技术难点及其要点总结

    - **上下文配置**:如何配置Spring MVC的核心组件以及它们之间的协作关系。 6. **数据库连接池配置**:合理配置数据库连接池可以显著提高系统的吞吐量和响应速度。 7. **Spring AOP图解**:通过图解的方式理解...

    SpringMVC上传与下载

    在Spring MVC框架中,处理...在Spring MVC的应用上下文配置文件(如`springmvc.xml`)中,需要配置一个`multipartResolver` bean,它负责处理多部分请求,即包含文件的表单提交。以下是一个基本的配置示例: ```xml ...

    spring mvc hibernate集成

    2. 配置Spring的上下文和数据源,导入Hibernate和C3P0的相关依赖。 3. 定义Hibernate的实体类,建立与数据库表的映射。 4. 创建SessionFactory,并在Spring中声明为bean,以便在Controller中注入使用。 5. 编写...

    springmvcjar包4.3.0/3.2.0

    "context" 模块扩展了 "beans" 模块,提供了对整个应用上下文的控制,包括bean的生命周期管理、事件传播、国际化支持等功能。在 Spring MVC 中,ApplicationContext 是WebApplicationContext 的父类,用于处理 Web ...

    springboot中thymeleaf模板使用详解

    2. 开箱即用的特性:Thymeleaf 提供标准和 Spring 标准两种方言,可以直接套用模板实现 JSTL、OGNL 表达式效果,避免每天套模板、该 JSTL、改标签的困扰。 3. 提供 Spring 标准方言:Thymeleaf 提供 Spring 标准方言...

Global site tag (gtag.js) - Google Analytics