简体   繁体   English

在春季,是否可以自动接线第一个bean?

[英]In spring, is there a way to autowire the first bean?

In the example below, is there a way to avoid doing a context.getBean()? 在下面的示例中,有没有一种方法可以避免执行context.getBean()? All the other beans subsequently used by the testService get autowired. 随后由testService使用的所有其他bean将自动装配。 (It is a console application) (这是一个控制台应用程序)

public class Test { 


        private static ITestService testService;

        private static ApplicationContext context;

           public static void main(String[] args) { 

            context = new ClassPathXmlApplicationContext(
                    new String[]{"/META-INF/spring/app-context.xml"});
            ITestService testService = context.getBean(ITestService.class); 

        }

}

I tried adding autowire annotation to ApplicationContext, but it didnt work. 我尝试将自动装配注释添加到ApplicationContext,但这没有用。 Besides how does it know where my app-context.xml is located if I autowire it? 此外,如果我将其自动布线,它如何知道我的app-context.xml所在的位置?

Update : I found what I needed over here 更新 :我在这里找到了需要的东西

Right, you're missing out a few details here. 是的,您在这里遗漏了一些细节。

Below is a short explanation of how Spring works. 以下是有关Spring如何工作的简短说明。

1- The application context is loaded somehow (we will get there soon). 1-应用程序上下文以某种方式加载(我们将很快到达那里)。 2- After loaded, app context will initialize/create all beans defined. 2-加载后,应用上下文将初始化/创建所有定义的bean。 Here is when beans get injected as dependencies. 这是将bean作为依赖项注入的时间。 After this Whenever you get a bean back from the app context, that bean is all initialized and ready to go with all the dependencies in place (considering everything went fine). 此后,只要您从应用程序上下文中获取了一个Bean,该Bean就会全部初始化并准备好使用所有依赖项(考虑到一切都很好)。

RE the first step, there are a few way to automate the Spring initialization. RE的第一步,有几种方法可以使Spring初始化自动化。 One way is what you are doing, explicitly instantiating one. 一种方法是您在做什么,显式实例化一种。 Other way could be via a context listener in case you're in a web environment, or maybe with the @RunWith . 其他方式可能是通过上下文侦听器 (如果您在Web环境中),或者使用@RunWith (You can find more here ) (您可以在此处找到更多信息

In your case, I believe you are looking for using Spring in a (Unit?!?) test environment so you are looking for something like 就您而言,我相信您正在寻找在(Unit?!?)测试环境中使用Spring的想法,因此您正在寻找类似的东西

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class MyTest {

    @Autowired
    private ApplicationContext applicationContext;

    // class body...
}

further details here 此处有更多详细信息

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#testing http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#testing

You cannot call beans without initializing the application context first. 您必须先初始化应用程序上下文才能调用bean。 Secondly in your case Test class should be bean itself to be managed by spring then to autowire ITestService . 其次,在您的情况下, Test类应该是bean本身,由spring管理,然后自动装配ITestService The purpose of Application context as a container is to manage the bean lifecycle so u need to initialize it first by ClassPathXmlApplicationContext and then it will initialize all beans declared by you in ur xml file. 应用程序上下文作为容器的目的是管理bean的生命周期,因此您需要首先通过ClassPathXmlApplicationContext对其进行初始化,然后它将初始化您在xml文件中声明的所有bean。 About avoiding the getBean method if you are using servlets for creating web app you can avoid getBean. 关于在使用servlet创建Web应用程序时避免使用getBean方法,可以避免使用getBean。 If not you should handle it manually. 如果没有,您应该手动处理。

I agree with what @Desorder has said. 我同意@Desorder所说的话。 When I started working with @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration, I used to get my test cases working. 当我开始使用@RunWith(SpringJUnit4ClassRunner.class)和@ContextConfiguration时,我曾经使我的测试用例正常工作。 But it took me some time to understand how these two are working internally and their default configurations. 但是我花了一些时间来了解这两者的内部工作方式及其默认配置。

If you would like to take some different approach and would like to try without @RunWith and @ContextConfiguration, take a look at the link - TUTORIAL: JUNIT @RULE. 如果您想采用其他方法,并且想在没有@RunWith和@ContextConfiguration的情况下尝试使用,请查看链接- 教程:JUNIT @RULE。 With this, you will be very clear which spring xml file locations are provided. 这样,您将非常清楚提供了哪些spring xml文件位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM