剑者~ 2018-01-24 06:02 采纳率: 33.3%
浏览 1154
已结题

spring task xml起了作用可以用,但注解就不行,ehcache缓存没起作用,

applicationContext-task.xml ,注释部分解除了是可以使用的,但我想用注解

头部删除
    <task:annotation-driven/>
    <!-- <bean id="task" class="task.TestTask"></bean>
    <task:scheduled-tasks>
        <task:scheduled ref="task" method="test" cron="0 * 13 * * ?"/>
    </task:scheduled-tasks> -->
    <!-- 
     -->
</beans>

TestTask.java 这是测试 定时器的, 测试类

 package task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TestTask {

    @Scheduled(cron = "0/5 * 11 * * ?")
    public void test() {
        System.out.println("-----执行------");
    }

}

这是 CityCache.java 缓存 测试类

 package cache;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import dao.AreaDao;
import dao.CityDao;
import dao.ProvinceDao;
import entity.Area;
import entity.City;
import entity.Province;

@Controller
public class CityCache {

    @Resource
    private ProvinceDao provinceDao;
    @Resource
    private CityDao cityDao;
    @Resource
    private AreaDao areaDao;

    @Cacheable(value = "province")
    public List<Province> listProvince() {
        // TODO Auto-generated method stub
        return provinceDao.list();
    }

    @Cacheable(value = "province")
    public List<Province> listProvince1() {
        // TODO Auto-generated method stub
        return null;
    }

    @Cacheable(value = "city")
    public List<City> listCityByProvinceId(String id) {
        // TODO Auto-generated method stub
        return cityDao.listCityByProvinceId(id);
    }

    @Cacheable(value = "area")
    public List<Area> listAreaByAreaId(String id) {
        // TODO Auto-generated method stub
        return areaDao.listAreaByCityId(id);
    }


}

applicationContext.xml

头部占位置 删了
    <!-- dataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/city" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
    <!-- DataSource JNDI -->
    <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" p:jndiName="java:comp/env/jdbc/house"></bean> -->

    <!-- AOP 实现事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 
        p:dataSource-ref="dataSource">
    </bean>
    <!-- 配置事务相关属性 -->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(public * service.impl.*.*(..))" id="servicePointcut"/>
        <aop:advisor advice-ref="advice"  pointcut-ref="servicePointcut"/>
    </aop:config>

    <!-- sqlSessionFactory 会话工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 注入mybatis配置文件路径 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <!-- sqlSession 注:SqlSessionTemplate 没有提供set方法注入-->
    <!-- <bean id="session" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean> -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="dao"></property>
    </bean>

    <!-- 启用缓存注解功能 -->  
    <!-- <cache:annotation-driven cache-manager="springCacheManager" proxy-target-class="false"/> -->
    <cache:annotation-driven cache-manager="cacheManager"/> 
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>  
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
        <property name="cacheManager" ref="cacheManagerFactory"/>  
    </bean> 

    <import resource="applicationContext-task.xml"/>
</beans>

springmvc.xml

 头部删除

    <!-- 使用 annotation 方式完成映射 -->
    <!-- 让spring扫描报下的所有类,让标注spring注解的类生效 -->
    <context:component-scan base-package="controller,dao,service,cache,task"/>
    <mvc:annotation-driven>
        <!-- 消息转换器 -->  
        <mvc:message-converters register-defaults="true">  
          <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
            <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>  
          </bean>  
        </mvc:message-converters>  
     </mvc:annotation-driven>
    <!--  -->
    <mvc:resources location="/statics/" mapping="/**"/>
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

mybatis-config.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration 
    PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" 
    "ibatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="LOG4J"/>
        <setting name="cacheEnabled" value="true" />
    </settings>
    <typeAliases>
        <package name="entity"/>
    </typeAliases>
</configuration>

在缓存哪方面我看了下 感觉是@Cacheable(value = "province")没起作用,我把province写错运行也没有报错,按说,我的ehcache。xml中没有配置名字,在@Cacheable(value = "province")中使用会 抛出异常。
如有大神看出问题请告知 ,谢谢

  • 写回答

4条回答 默认 最新

  • lzgc_du 2018-01-24 07:03
    关注

    我用spring task注解方式配置 也没有起作用,暂时用xml配置的,后面时间在研究研究吧

    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面