English 中文(简体)
在春天形成一个物体的方案?
原标题:Programmatically creating an object in Spring?

在春天,如果用Xml文档注射,那么在方案上是否有办法。

这里指的是

我有这个类别称为安全奖。 总是在春天才出现这种情况。

<bean id="securityDelegate" class="securityBusinessDelegate" lazy-init="true">
    <property name="securityServiceEJB" ref="securityServiceEJB"/>
    <property name="securityService" ref="securityService"/>
  </bean>

我需要在“生命”会议中使用这一类别的例子,而且我的理解是,由于这属于保守派,我可以举一个安全的例子。 出席我的<代码>的代表 HttpSessionListener 执行。

我想做的是,在我执行过程中,通过java守则找回一个例子,以做像这样的事情。

public class SessionListener implements HttpSessionListener {

 //other methods
 @Override
 public void sessionDestroyed(HttpSessionEvent se) {
    //get an instance of securityBusinessDelegate here

    securityBusinessDelegate.doSomething();
 }
}

我似乎记得,在我使用春天(3年多前)时,这是可能的,但我可能感到困惑。 可否建立工厂?

最佳回答
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(se.getServletContext.());
ctx.getBean("securityDelegate");
问题回答

完整性:

import org.springframework.context.ApplicationContext;    
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SessionListener implements HttpSessionListener {

 @Override
 public void sessionDestroyed(HttpSessionEvent se) {
    ServletContext servletCtx = se.getSession().getServletContext();
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletCtx);
    YourClass securityBusinessDelegate = ctx.getBean(YourClass.class);

    securityBusinessDelegate.doSomething();
 }
}

Yep使用工厂。 更复杂得多。 DI/IoC Geniuses.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签