gpt4 book ai didi

java - 将 Spring bean 注入(inject)到使用 new() 创建的外部类中

转载 作者:行者123 更新时间:2023-12-01 12:37:56 25 4
gpt4 key购买 nike

我正在尝试在类中使用 spring bean。我正在从另一个 spring bean 访问这个类。我尝试使用 AutoWireCapableBeanFacory 以及 @Configurable + AspectJ LTW,但一次又一次地收到 java.lang.UnsupportedOperationException

我的场景如下:

ServiceOneImpl 调用 MyClass,并且 MyClass 使用另一个名为 MyService 的 bean( Autowiring )

//-------------------ServiceOne--------------
@Service("serviceOne")
public class ServiceOneImpl implements ServiceOne {
@Override
public void myMethod() {
ClassA obj = new ClassA(arg1,arg2);
obj.doSometing();

}
}

//-------------------MyService-------------
@Service("myService")
public class MyServiceImpl implements MyService {
@Override
public MyDTO getConfiguration(String arg) {
return myDao.getConfiguration(arg);
}
}

//-------------------MyClass--------------
@Component
public Class MyClass {

@Autowired
private MyService myService;

private MyDTO myDTO;

private String mailhost;
private String arg1;
private String arg2;

public A() {
}

public A(String param1, String param2) {
ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);

applicationContext.getAutowireCapableBeanFactory().
autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT,
true);
myDTO = myService.getConfiguration();
mailhost = myDTO.getMailhost();

//Do some process
}
public doSometing() {
//Do some process
}
}

但我收到 java.lang.UnsupportedOperationException MyService 未 Autowiring ,它返回 null 值

javax.faces.el.EvaluationException: java.lang.UnsupportedOperationException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
at com.beo.atlas.common.filter.ClientSideCacheClearingFilter.doFilter(ClientSideCacheClearingFilter.java:34)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersisten

我正在使用SpringFramework 4.0.3.RELEASE

我无法解决此问题。如果有人为我提供一些启发,这对我来说将是巨大的帮助

提前致谢

iMmo

最佳答案

除非您有充分的理由这样做,否则当您希望 Spring 将 bean 连接到对象时,请勿使用 new。并避免将域类绑定(bind)到 Spring。

恕我直言,您最好使用自定义构建器 bean:

public class ABuilder {
@Autowired
private MyService myService;

public A getA(String param1, String param2) {
A a = new A(param1, param2);
a.setMyDTO(myService.getConfiguration());
a.setMailhost(myDTO.getMailhost());
return a;
}
}

并相应地修改ServiceOne:

//-------------------ServiceOne--------------
@Autowired
ABuilder aBuilder;

@Service("serviceOne")
public class ServiceOneImpl implements ServiceOne {
@Override
public void myMethod() {
ClassA obj = aBuilder.getA(arg1, arg2);
obj.doSometing();
}
}

这样,A(或MyClassClassA)就可以没有对Spring的显式依赖,并且将通过其其他bean注入(inject) build 者。

当然,这不是真正的代码(参数和命名不连贯),但你的也不是......

关于java - 将 Spring bean 注入(inject)到使用 new() 创建的外部类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25402441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com