由于配置不同,我编写了两个LettuceConnectionFactory:
ItemFactoryConfig
ActivityFactoryConfig
代码如下:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg">
<RelativeLayout
android:id="@+id/prank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<RelativeLayout
android:id="@+id/rel_1"
android:layout_width="match_parent"
android:layout_height="278dp"
android:background="@drawable/bg_white">
<TextView
android:id="@+id/tv_setTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp"
android:gravity="center"
android:text="@string/set_time"
android:textColor="#6651d4"
android:textSize="25sp" />
<RelativeLayout
android:id="@+id/rl_btnSec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_setTime"
android:gravity="center"
android:layout_centerInParent="true">
<Button
android:id="@+id/btn_fiveSec"
android:layout_width="65sp"
android:layout_height="65sp"
android:layout_marginTop="10sp"
android:background="@drawable/time"
android:text="@string/fiveSecond"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#6651d4"
android:paddingBottom="15sp"/>
<Button
android:id="@+id/btn_tenSec"
android:layout_width="65sp"
android:layout_height="65sp"
android:layout_marginTop="10sp"
android:layout_marginLeft="5sp"
android:background="@drawable/time"
android:layout_toEndOf="@+id/btn_fiveSec"
android:layout_toRightOf="@+id/btn_fiveSec"
android:text="@string/tenSecond"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#6651d4"
android:paddingBottom="15sp"
android:layout_marginStart="5sp" />
<Button
android:id="@+id/btn_fifteenSec"
android:layout_width="65sp"
android:layout_height="65sp"
android:layout_marginTop="10sp"
android:background="@drawable/time"
android:layout_marginLeft="5sp"
android:layout_toEndOf="@+id/btn_tenSec"
android:layout_toRightOf="@+id/btn_tenSec"
android:text="@string/fifteenSecond"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#6651d4"
android:paddingBottom="15sp"
android:layout_marginStart="5sp" />
<Button
android:id="@+id/btn_thirtySec"
android:layout_width="65sp"
android:layout_height="65sp"
android:layout_marginTop="10sp"
android:layout_marginLeft="5sp"
android:background="@drawable/time"
android:layout_toEndOf="@+id/btn_fifteenSec"
android:layout_toRightOf="@+id/btn_fifteenSec"
android:text="@string/thirtySecond"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#6651d4"
android:paddingBottom="15sp"
android:layout_marginStart="5sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_btnMin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_btnSec"
android:gravity="center"
android:layout_centerInParent="true">
<Button
android:id="@+id/btn_oneMin"
android:layout_width="65sp"
android:layout_height="65sp"
android:layout_marginTop="10sp"
android:background="@drawable/time"
android:text="@string/oneMinute"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#6651d4"
android:paddingBottom="15sp"/>
<Button
android:id="@+id/btn_fiveMin"
android:layout_width="65sp"
android:layout_height="65sp"
android:layout_marginTop="10sp"
android:layout_marginLeft="5sp"
android:background="@drawable/time"
android:layout_toEndOf="@+id/btn_oneMin"
android:layout_toRightOf="@+id/btn_oneMin"
android:layout_marginStart="5sp"
android:text="@string/fiveMinute"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#6651d4"
android:paddingBottom="15sp"/>
<Button
android:id="@+id/btn_tenMin"
android:layout_width="65sp"
android:layout_height="65sp"
android:layout_marginTop="10sp"
android:layout_marginLeft="5sp"
android:background="@drawable/time"
android:layout_toEndOf="@+id/btn_fiveMin"
android:layout_toRightOf="@+id/btn_fiveMin"
android:layout_marginStart="5sp"
android:text="@string/tenMinute"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#6651d4"
android:paddingBottom="15sp"/>
<Button
android:id="@+id/btn_thirtyMin"
android:layout_width="65sp"
android:layout_height="65sp"
android:layout_marginTop="10sp"
android:layout_marginLeft="5sp"
android:background="@drawable/time"
android:layout_toEndOf="@+id/btn_tenMin"
android:layout_toRightOf="@+id/btn_tenMin"
android:layout_marginStart="5sp"
android:text="@string/thirtyMinute"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#6651d4"
android:paddingBottom="15sp"/>
</RelativeLayout>
<TextView
android:id="@+id/tv_viewTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_btnMin"
android:layout_marginTop="10sp"
android:gravity="center"
android:textColor="#6651d4"
android:textSize="15sp" />
</RelativeLayout>
<Button
android:id="@+id/btn_startPrank"
android:layout_width="200sp"
android:layout_height="60sp"
android:layout_below="@+id/rel_1"
android:text="@string/start_pranking"
android:textColor="@android:color/white"
android:layout_centerHorizontal="true"
android:background="@drawable/start"/>
<Button
android:id="@+id/btn_stopPrank"
android:layout_width="200sp"
android:layout_height="60sp"
android:layout_below="@+id/btn_startPrank"
android:text="@string/stop_pranking"
android:textColor="@android:color/white"
android:layout_centerHorizontal="true"
android:background="@drawable/off"/>
</RelativeLayout>
当这些LettuceConnectionFactories自动连线时,我添加了@Qualifier。
@Configuration
@PropertySource(value = "classpath:config/redis.properties", encoding = "UTF-8")
public class ItemFactoryConfig {
@Value("${item.host}")
private String host;
@Value("${item.port}")
private int port;
@Bean
public LettuceConnectionFactory itemFactory(){
RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration();
redisConfiguration.setHostName(host);
redisConfiguration.setPort(port);
return new LettuceConnectionFactory(redisConfiguration);
}
}
@Configuration
@PropertySource(value = "classpath:config/redis.properties", encoding = "UTF-8")
public class ActivityFactoryConfig {
@Value("${activity.host}")
private String host;
@Value("${activity.port}")
private int port;
@Bean
public LettuceConnectionFactory activityFactory(){
RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration();
redisConfiguration.setHostName(host);
redisConfiguration.setPort(port);
return new LettuceConnectionFactory(redisConfiguration);
}
}
但是,spring boot仍然报告NoUniqueBeanDefinitionException。
@Configuration
public class ItemTemplateConfig {
@Autowired
@Qualifier("itemFactory")
private LettuceConnectionFactory itemFactory;
@Bean
public RedisTemplate<String, String> itemTemplate(){...}
}
@Configuration
public class ActivityTemplateConfig {
@Autowired
@Qualifier("activityFactory")
private LettuceConnectionFactory activityFactory;
@Bean
public RedisTemplate<String, String> activityTemplate(){...}
}
Spring boot说明:
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration中的方法redisTemplate的参数0需要单个bean,但是找到了2个: -activityFactory:由类路径资源[com / lyf / redisdemo / config / Activity / ActivityFactoryConfig.class]中的方法'activityFactory'定义 -itemFactory:由类路径资源[com / lyf / redisdemo / config / Item / ItemFactoryConfig.class]中的“ itemFactory”方法定义
操作:
考虑将其中一个bean标记为@Primary,更新使用者以接受多个bean,或使用@Qualifier标识应消耗的bean
弹簧靴:2.1.0。释放