Skip to content
gqlxj1987's Blog
Go back

今日阅读

Edit page

restcontroller

@restController的注解,相当于@Controller和@ResponseBody的集合,相当在类上加上@ResponseBody的注解。同时,在RestController的注解中,还包含@Async的部分。

security

关于spring-boot-security部分,采用SecurityProperites的方式,来进行configure,实现HttpSecurity和AuthenticationManagerBuilder

default

java8中针对接口部分,可以利用default来实现具体方法,类似于不用abstract类的方式

jpaReposity

jpa的注解方式,在注解中写sql,

redisTemplate

关于redis部分,一般的使用

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(20);

jedisPool = new JedisPool(jedisPoolConfig,
                        NightConfig.getConfigValueByKey("nightingale.query.redis.host"),
                        NightConfig.getConfigIntValueByKey("nightingale.query.redis.port", 6379));


  Jedis jedis = jedisPool.getResource();
  String value = jedis.get(key);
  jedisPool.returnResourceObject(jedis);

这样的使用,导致每次需要显式地释放redis链接,代码的倾入性太高。

这时候,可以使用RedisTemplate封装相关的redis,避免每次显式释放链接的痛苦。原因在于每次redis执行时,相当于redisTemplate执行一个叫做RedisCallback的东西,

try {

			if (enableTransactionSupport) {
				// only bind resources in case of potential transaction synchronization
				conn = RedisConnectionUtils.bindConnection(factory, enableTransactionSupport);
			} else {
				conn = RedisConnectionUtils.getConnection(factory);
			}

			boolean existingConnection = TransactionSynchronizationManager.hasResource(factory);

			RedisConnection connToUse = preProcessConnection(conn, existingConnection);

			boolean pipelineStatus = connToUse.isPipelined();
			if (pipeline && !pipelineStatus) {
				connToUse.openPipeline();
			}

			RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));
			T result = action.doInRedis(connToExpose);

			// close pipeline
			if (pipeline && !pipelineStatus) {
				connToUse.closePipeline();
			}

			// TODO: any other connection processing?
			return postProcessResult(result, connToUse, existingConnection);
		} finally {

			if (!enableTransactionSupport) {
				RedisConnectionUtils.releaseConnection(conn, factory);
			}
		}

从上面的代码可以看到,运行完action后,最后都会有finally,通过RedisConnectionUtils去releaseConnection,从而避免我们写的代码里去显式地调用释放链接

json部分

json这块可以使用Jackson或者Gson

jackson的注解为JsonAutoDectect,同时辅以JsonIgnoreProperites来使用,有一个mapWrapper来写

gson部分,使用New GsonBuilder.create().toJson的方式,同时辅以@Expose的注解,表明序列化哪一个field,从而起跟上面一样的反效果。不过需要使用

new GsonBuilder.excluedFieldWithoutExposeFields().create()

注解相关

@Configuration

@SpringBootApplication 表明是一个spring-boot的程序

@Cached redis相关?

@EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.

@PropertySource 跟autoConfiguration配对?

@ComponentScan 相关controller和service的扫描

@Bean


Edit page
Share this post on:

Previous Post
何处是归处
Next Post
今日阅读列表