Spring boot的配置类
@Configuration
指明当前类是一个配置类
来替代之前的Spring配置文件
Spring boot的配置类
相当于Spring的配置文件
容器添加组件
Spring,通过配置文件添加组件
Spring boot,通过配置类的方式添加组件
@Bean
将方法的返回值添加到容器中容器中,这个组件默认的id,就是方法名
package com.atguigu.springboot.config; import com.atguigu.springboot.service.HelloService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件 在配置文件中用<bean><bean/>标签添加组件 */ @Configuration public class MyAppConfig { //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名 @Bean(name="jedisPoolConfig") /* * @Bean将方法的返回值添加到容器中 容器中,这个组件默认的id,就是方法名*/ public JedisPoolConfig createJedisPoolConfig(){ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); /*控制一个pool可以分配多少个jedis实例*/ jedisPoolConfig.setMaxTotal(maxTotal); /* 连接池中最多可空闲的连接数,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,随时处于待命状态*/ jedisPoolConfig.setMaxIdle(maxIdle); /*最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒)超过时间则抛出异常*/ jedisPoolConfig.setMaxWaitMillis(maxWait); /*在获取连接的时候检查有效性*/ jedisPoolConfig.setTestOnBorrow(testOnBorrow); return jedisPoolConfig; //返回值相当于 class=“” } /*创建Redis连接池,并做相关配置*/ @Bean(name="jedisPoolWriper") public JedisPoolWriper createJedisPoolWriper(){ JedisPoolWriper jedisPoolWriper = new JedisPoolWriper(jedisPoolConfig, hostname, port); return jedisPoolWriper; } }
在配置类重,可以直接使用由@Bean定义的id=jedisPoolConfig,
原文链接:https://blog.csdn.net/nangeali/article/details/82142719

浙公网安备 33010602011771号