发表于: 2017-07-11 09:44:41

1 1406


今天完成的:

Task6大部分

收获:

1.spring和springMVC父子关系

Spring 是父容器, Spring MVC是子容器, 子容器可以访问父容器的bean,父容器不能访问子容器的bean。 

如果<context:component-scan base-package="com.hafiz" //全局扫描/>此时的Spring容器中就会将@Controller,@Service,@Reposity,@Component全部配置在spring容器下,则springMVC容器中将没有controller,启动项目会报404.

详见:http://labreeze.iteye.com/blog/2359957

springMVC中的<context:annotation-config/>配置开启注解功能。该配置在spring3.1之前等价于

<!--配置注解控制器映射器,它是SpringMVC中用来将Request请求URL到映射到具体Controller-->

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

<!--配置注解控制器映射器,它是SpringMVC中用来将具体请求映射到具体方法-->

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

遇到的问题:

今天用jmeter测试又正常了。怪了???,测jsp:没加缓存

测jsp:没加缓存

测返回List<Developer>的接口:没加缓存

测返回List<Developer>的接口:加缓存

测服务器:服务器性能太差了,没办法

redis测试jsp:这两个测的次数比较多,取的中位数。

未使用redis测试jsp:

遇到的问题:

1.一个类中用@Autowired注解属性后,初始化该类不能使用new的方式,new的话将不通过spring容器创建对象,其中的注解会失效,自动注入也会失败。多亏了志荣师兄,不然这一天都白干了。。。

2.redis启动:redis-server.exe redis.windows.conf 。然后另开窗口redis-cli.exe -h 127.0.0.1 -p 6379

3.redis不提供序列化方法,建议使用谷歌的Gson,性能很好,memcache也不建议直接使用序列化api。

public List<Developer> queryDeveloper() {
String json = cacheUtil.get("DeveloperList");
   if (json != null) {
logger.debug("从缓存中读取开发者列表---------------------json为:"+json);
       List<Developer> developers = new Gson().fromJson(cacheUtil.get("DeveloperList"),new TypeToken<List<Developer
>>(){}.getType());
       return developers;
   } else {
developers = DeveloperDAO.queryDeveloper("%");
       cacheUtil.put("DeveloperList", developers);
       logger.debug("将开发者列表添加到redis缓存中");
       return developers;
   }
}

redis整合spring

<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>

<!-- 读取多个配置文件,要使用ignore-unresolvable -->

<context:component-scan base-package="com.jnshu.serviceImpl"/
<!-- redis连接池 -->
<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
   <property name="maxActive" value="${redis_max_active}"></property>
   <property name="maxIdle" value="${redis_max_idle}"></property>
   <property name="maxWait" value="${redis_max_wait}"></property>
   <property name="testOnBorrow" value="${redis_test_on_borrow}"></property>
</bean>
<!-- redis连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
   <property name="hostName" value="${redis_addr}"></property>
   <property name="port" value="${redis_port}"></property>
   <property name="password" value="${redis_auth}"></property>
   <property name="poolConfig" ref="jedisConfig"></property>
</bean>
<!-- redis操作模板,这里采用尽量面向对象的模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
   <property name="connectionFactory" ref="connectionFactory"/>
   <!--   如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!-->
   <property name="keySerializer">
       <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
   </property>
   <property name="valueSerializer">
       <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
   </property>
</bean>

redis实现类

@Component
public class CacheUtilImpl implements CacheUtil 
@Autowired
   private StringRedisTemplate redisTemplate;//redis操作模板
   public void put(String key, String value) {
if (key == null || "".equals(key)) {
return;
       }
redisTemplate.opsForHash().put(key, key, value);
   
public void put(String key, Object value) {
if (key == null || "".equals(key)) {
return;
       }
redisTemplate.opsForHash().put(key, key, new Gson().toJson(value));

   }
public <T> T get(String key, Class<T> className) {
Object obj = redisTemplate.opsForHash().get(key, key);
       if (obj == null) {
return null;
       }
return new Gson().fromJson("" + obj, className);
   }

public String get(String key) {
if (redisTemplate == null) {
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
       }
Object obj = redisTemplate.opsForHash().get(key, key);
       if (obj == null) {
return null;
       } else {
return String.valueOf(obj);
       }
}
}

明天的计划:

部署第二台服务器,做任务3.

模拟缓存穿透。

开始Task7


返回列表 返回列表
评论

    分享到