zoukankan      html  css  js  c++  java
  • spring 注解注入bean

    通过注解方式注入bean,需要在配置类下注入bean

    第一步,配置扫描文件夹

    首先要在spring.xml中配置需要扫描的配置类

    <context:componenet-scan base-package="com.kylin.config" />

    第二步,新建注解配置类

    @Configuration
    public class RedisConfiguration {
        @Value("${redis.host}")
         private String redisHost;   
         @Value("${redis.port}")
         private String redisPort; 
        
    
        @Bean(name="jedisPoolConfig")
        public JedisPoolConfig createJedisConfig() {
            JedisPoolConfig jpc = new JedisPoolConfig();
            return jpc;
        }
        
           @DependsOn("jedisPoolConfig")
         @Bean(name="jedisPoolConfig")
        public JedisCluster createJedisCluster(JedisPoolConfig jedisPoolConfig) {
            HostAndPort hap = new HostAndPort(redisHost,redisPort);
            JedisCluster jc = new JedisCluster(hap,jedisPoolConfig);
            return jc;
        }
        
    }
            

    以redis为例,其中某个bean需要依赖另一个bean的话,需要通过@DependsOn注解,让需要依赖的bean先加载,需要依赖的bean通过方法参数,传入jedisCluster中。

  • 相关阅读:
    软件工程 2016.6.28 日报
    软件工程课程总结
    工大助手--项目总结
    工大助手--加权平均分计算
    工大助手--数据查询
    7.5
    7月4日日报
    7.3日报
    6.30日报
    6.29.日报
  • 原文地址:https://www.cnblogs.com/uoar/p/13025760.html
Copyright © 2011-2022 走看看