使用构造器创建多个bean

时间:2018-11-26 21:34:35

标签: java spring spring-boot

我试图用构造器创建多个bean,但是它一直失败。

  

ServerStater.java

@SpringBootApplication
public class ServerStarter {

    public static void main(String[] args)
    {
        ConfigurableApplicationContext context = SpringApplication.run(ServerStarter.class, args);

        NettyTcpServer server1 = context.getBean(NettyTcpServer.class, (Integer) 10001);
        server1.start();

        NettyTcpServer server2 = context.getBean(NettyTcpServer.class, (Integer) 10002);
        server2.start();
    }
}
  

NettyTcpServer

@Component
public class NettyTcpServer {

    private int port;

    @Autowired
    public NettyTcpServer(int port)
    {
        this.port = port;
    }

   ...
}

因此,基本上,我想在同一个类中制作两个具有不同变量值的bean。我创建了一个Constructor,它将获得一个打开TCP套接字的端口,并且在程序启动时在创建bean时传递了int参数。我想念如何使用这些注释吗?我在运行时遇到了错误的错误消息。

  

申请无法开始

     
     

说明:

     

com.telcuon.korail.netty.NettyTcpServer中的构造函数的参数0   需要找不到的'int'类型的Bean。

     

操作:

     

考虑在您的配置中定义类型为'int'的bean。
  [警告]的java.lang.reflect.InvocationTargetException   sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   org.springframework.boot.maven.AbstractRunMojo $ LaunchRunner.run(AbstractRunMojo.java:558)     在java.lang.Thread.run(Thread.java:748)造成原因:   org.springframework.beans.factory.UnsatisfiedDependencyException:   在文件中定义名称为“ nettyTcpServer”的bean创建时出错   [C:\ Java \ TCO-AppCard-Korail-Branch-Server \ target \ classes \ com \ telcuon \ korail \ netty \ NettyTcpServer.class]:通过构造函数参数0表示的不满意依赖性;   嵌套异常为   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   可用类型为'int'的合格Bean:至少应有1个Bean   符合自动装配候选资格。依赖项注释:{},位于   org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:767)     在   org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)     在   org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:320)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)     在   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)     在   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:316)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)     在com.telcuon.korail.ServerStarter.main(ServerStarter.java:22)...   其他6个原因:   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   可用类型为'int'的合格Bean:至少应有1个Bean   符合自动装配候选资格。依赖项注释:{},位于   org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1646)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1205)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)     在   org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:855)     在   org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:758)     ...还有24个

0 个答案:

没有答案