java 加载 Spring bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7063821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:30:05  来源:igfitidea点击:

Load Spring bean

javaspring

提问by Joe

Is there any way in Spring to load a bean specifically.

Spring 有什么方法可以专门加载 bean。

I've a appContext file having lots of beans. When loading it using the following code, it loads all beans again.

我有一个包含大量 bean 的 appContext 文件。使用以下代码加载它时,它会再次加载所有 bean。

BeanFactory factory = new ClassPathXmlApplicationContext("appContext.xml");

回答by tojofo

What about using ApplicationContextAware?

怎么用ApplicationContextAware

Bean mapping

Bean映射

<bean id="springApplicationContext" class="SpringApplicationContext"/>

<bean id="springApplicationContext" class="SpringApplicationContext"/>

Java implementation

Java实现

public class SpringApplicationContext implements ApplicationContextAware {

    private static ApplicationContext CONTEXT;

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        CONTEXT = ctx;
    }

    public static Object getBean(String name) {
        return CONTEXT.getBean(name);
    }
}

Then use it like this;

然后像这样使用它;

SpringApplicationContext.getBean("myBean");

SpringApplicationContext.getBean("myBean");

回答by Roman

By default, spring creates instances of all singleton-scoped beans at startup.

默认情况下,springsingleton在启动时创建所有作用域 bean 的实例。

I would recommend you to split you spring configuration in several distinct files. In that case you'll be able to load only that group of beans which is required for your task.

我建议您将 spring 配置拆分为几个不同的文件。在这种情况下,您将只能加载您的任务所需的那组 bean。

Another way is to declare your beans with default-lazy-initattribute:

另一种方法是用default-lazy-init属性声明你的bean :

<beans default-lazy-init="true">
    <!-- no beans will be pre-instantiated... -->
</beans>

回答by user12384512

You can use ApplicationContextAware interface. example

您可以使用 ApplicationContextAware 接口。 例子

When you get instance of this bean, you can load whatever bean you want.

当你得到这个 bean 的实例时,你可以加载任何你想要的 bean。

回答by pap

One method is to migrate to using springs abstraction of quartz. That way, your quartz "jobs" are spring beans from the start.

一种方法是迁移到使用石英的弹簧抽象。这样,您的石英“工作”从一开始就是春豆。

Read more here.

在这里阅读更多。

回答by beny23

This may help in order to avoid having to recreate a spring context.

这可能有助于避免重新创建 spring 上下文。

If you use Spring to configure the quartz jobthen you can reference spring beans directly from your job.

如果您使用Spring 配置石英作业,那么您可以直接从您的作业中引用 spring bean。

For example, if you use the MethodInvokingJobDetailFactoryBean, then you could create a bean that executes the code, which in turn calls your DAO.

例如,如果您使用MethodInvokingJobDetailFactoryBean,那么您可以创建一个执行代码的 bean,该 bean 又调用您的 DAO。

<bean id="exampleBusinessObject" class="my.pkg.BusinessObject">
    <property name="dao" ref="myDao" />
</bean>

<bean id="exampleJob"  
      class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="exampleBusinessObject"/>
    <property name="targetMethod" value="doIt"/>
</bean>

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="exampleJob" />
    <!-- run every 30 min -->
    <property name="cronExpression" value="0 0/30 * * * ?" />
</bean>