相关疑难解决方法(0)

如何使用@ComponentScan注释扫描多个路径?

我正在使用Spring 3.1并使用@Configuration@ComponentScan属性引导应用程序.

实际开始是完成的

new AnnotationConfigApplicationContext(MyRootConfigurationClass.class);
Run Code Online (Sandbox Code Playgroud)

此Configuration类使用注释

@Configuration
@ComponentScan("com.my.package")
public class MyRootConfigurationClass
Run Code Online (Sandbox Code Playgroud)

这很好用.但是我想更具体地说明我扫描的软件包,所以我试过了.

@Configuration
@ComponentScan("com.my.package.first,com.my.package.second")
public class MyRootConfigurationClass
Run Code Online (Sandbox Code Playgroud)

但是,这会失败,并告诉我无法找到使用@Component注释指定的组件.

做我正在做的事情的正确方法是什么?

谢谢

java spring annotations

76
推荐指数
5
解决办法
14万
查看次数

如何在Spring中自动装配泛型类型<T>的Bean?

我有一个bean Item<T>需要在@Configuration课堂上自动装配.

@Configuration
public class AppConfig {

    @Bean
    public Item<String> stringItem() {
        return new StringItem();
    }

    @Bean
    public Item<Integer> integerItem() {
        return new IntegerItem();
    }

}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试时@Autowire Item<String>,我得到以下异常.

"No qualifying bean of type [Item] is defined: expected single matching bean but found 2: stringItem, integerItem"
Run Code Online (Sandbox Code Playgroud)

我应该如何Item<T>在Spring中自动加载通用类型?

java generics spring autowired spring-annotations

66
推荐指数
2
解决办法
6万
查看次数

我不能@Autowire存在于依赖的Library Jar中的Bean吗?

我有一个Spring Boot应用程序(Y),它依赖于一组打包为x.jar的库文件,并在应用程序Y的pom.xml中作为依赖项提到.

x.jar有一个名为(User.java)的bean应用程序Y有一个名为(Department.java)的java类

当我尝试在Department.java中自动装配User.java的实例时,我收到以下错误

我不能@Autowire存在于依赖的Library Jar中的Bean吗?

无法自动装配字段:private com.User user; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型[com.User]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

没有找到[com.User]类型的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}**

这是Spring Boot应用程序'Y'中的代码

package myapp;

@Component
public class Department {

    @Autowired
    private com.User user;

    //has getter setters for user

}
Run Code Online (Sandbox Code Playgroud)

这是Library x.jar中User.java的代码

 package com;

@Component
@ConfigurationProperties(prefix = "test.userproperties")
public class User {

  private String name;
  //has getter setters for name    
}
Run Code Online (Sandbox Code Playgroud)

这是应用程序Y的pom.xml中x.jar的依赖项

      <groupId>com.Lib</groupId>
      <artifactId>x</artifactId>
      <version>001</version>
    </dependency>   
Run Code Online (Sandbox Code Playgroud)

这是应用程序'Y'中的Main类

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableZuulProxy
@EnableGemfireSession(maxInactiveIntervalInSeconds=60)
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableDiscoveryClient
public class ZuulApplication {

    public static void main(String[] args) …
Run Code Online (Sandbox Code Playgroud)

java spring autowired

15
推荐指数
1
解决办法
2万
查看次数

春季4.2.4.自动装配扩展通用接口列表

@Autowired
private List<WalletService<Wallet>> walletServices; //Doesn't work

@Autowired
private List<WalletService> walletServices; //Everything is fine
Run Code Online (Sandbox Code Playgroud)

假设我们有:

interface A<T extends W>;
interface B extends A<W1>;
interface C extends A<W2> ;
class W1 extends W;
class W2 extends W;
Run Code Online (Sandbox Code Playgroud)

我知道可以注入A或特定A的列表.我可以注入一个A列表以避免显式转换List<A> to List<A<W>>吗?现在,当我尝试一些时,我得到了org.springframework.beans.factory.NoSuchBeanDefinitionException

我认为这个功能对于实现这样的类层次结构是必要的:

interface WalletService<T exends Wallet>
interface TradeWalletService extends WalletService<TradeWallet>
interface PersonalWalletService extends WalletService<PersonalWallet>
Run Code Online (Sandbox Code Playgroud)

也许我错过了什么.在此先感谢您的回复!

java spring

6
推荐指数
1
解决办法
1612
查看次数