将Spring bean注入使用new()创建的外部类

时间:2014-08-20 10:30:03

标签: java spring spring-mvc

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

我的情景如下:

ServiceOneImpl调用MyClassMyClass使用另一个名为MyService的bean(自动装配)

//-------------------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没有获得自动装配,它返回空值

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

3 个答案:

答案 0 :(得分:1)

除非您有充分的理由这样做,否则当您希望Spring将bean连接到对象时,请不要使用new。并避免将域类绑定到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。

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

答案 1 :(得分:0)

请添加:

<context:spring-configured />

进入弹簧配置,

然后在MyClass类中添加@Configurable而不是@Component!

答案 2 :(得分:0)

我通过将@Lazy注释添加到自动装配属性来解决了这个问题。

未来参考的完整代码:

//-------------------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
   @Lazy
   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_BY_NAME,true);
       myDTO = myService.getConfiguration(); 
       mailhost = myDTO.getMailhost();

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

希望它会帮助某人:)