0

我有一个爱好项目,我想迁移到 Spring。

例如,我有以下类:

public class OtherBean {
    public void printMessage() {
        System.out.println("Message from OtherBean");
    }
}


public class InjectInMe {
    @Inject OtherBean otherBean;

    public void callMethodInOtherBean() {
        otherBean.printMessage();
    }
}

但是,当我阅读文档时,我必须使用 @Component 之类的注释(或其他类似的注释)来注释所有要由 Spring 管理的类。

使用以下代码运行它:

public class SpringTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.refresh();

        InjectInMe bean = context.getBean(InjectInMe.class);
        bean.callMethodInOtherBean();
    }
}

给我错误:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [somepackage.InjectInMe] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
    at somepackage.SpringTest.main(SpringTest.java:10)

我的问题是:有没有办法让 Spring 管理我要求 ApplicationContext 实例化的任何在 Annotated Config(或 XML 配置)中注册它们?

在 Guice 中,我可以注入课堂

public class GuiceTest {
    static public class GuiceConfig extends AbstractModule {

        @Override
        protected void configure() {}

    }
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new GuiceConfig());
        InjectInMe bean = injector.getInstance(InjectInMe.class);
        bean.callMethodInOtherBean();
    }
}

给我输出:

Message from OtherBean

无论如何我可以让Spring像Guice一样工作吗?就像让 Spring 注入我的 bean 一样,我不必注册或扫描带有 @Component 类注释的类的包?

任何 Spring 大师有办法解决这个问题吗?

4

3 回答 3

2

我的问题是:有没有办法让 Spring 管理我要求 ApplicationContext 实例化的任何类,而无需在 Annotated Config(或 XML 配置)中注册它们?

不,这是不可能的。这根本不是 Spring 的设计方式。您必须隐式(例如扫描 + 注释)或显式(例如 XML bean 定义)注册您的 bean。

你至少可以做的是:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(OtherBean.class);
context.register(InjectInMe.class);
context.refresh();
于 2014-11-21T21:24:25.073 回答
0

You must either declare the class as a @Component, or whatever it may be, or you must provide an xml definition. If you don't define the bean, how is Spring supposed to know what to do? For the properties of a class, e.g. MyController has a private service MyService. If you put the @Autowired tag above the service instantiation, Spring will automatically inject this server into your controller, which is what your comment about @Inject seems to hint at. However, in order to autowire, you must define a bean for that class, which means you must use either the @Component/@Controller, etc annotation configuration or an xml bean configuration.

于 2014-11-21T18:55:19.700 回答
0

有几种方法可以为 Spring 提供 bean 定义ApplicationContext

  1. 使用组件扫描并使用@Component元注释或其中一个元注释(@Service、、@Controller等)来注释您的类型
  2. 定义一个<bean>in XML 配置或一个@Beanin Java 配置(或任何其他类型的显式配置)。
  3. 使用 aBeanDefinitionRegistryPostProcessor直接在 a 上注册 bean BeanDefinitionRegistry
  4. 一些ApplicationContext子类型实现BeanDefinitionRegistry,因此您可以直接向它们注册 bean 定义。

当需要注入时,从这些 bean 定义生成的 bean 将可用。您不希望(至少在 IMO)您的容器实例化一个类型,而无需您以某种方式告诉它它是可以的。

于 2014-11-21T18:48:41.287 回答