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")中使用会 抛出异常。
如有大神看出问题请告知 ,谢谢