使用java反射调用Springbean

使用java反射调用Springbean,java,dynamic,Java,Dynamic,动态调用spring,而不是静态调用 我按照下面的方法动态调用bean,但是我得到了下面的错误 org.springframework.beans.factory.nosuchbeansdefinitionexception:没有名为 源代码: 一,。主要服务类别 @Component public class ContentService { @Autowired private ApplicationContext _appContext; public void

动态调用spring,而不是静态调用

我按照下面的方法动态调用bean,但是我得到了下面的错误

org.springframework.beans.factory.nosuchbeansdefinitionexception:没有名为

源代码:

一,。主要服务类别

@Component
public class ContentService
{
    @Autowired
    private ApplicationContext _appContext;

    public void process()
    {
        try {
                Object bean = _appContext.getBean("com.content.partner.Movie");
                Method method = bean.getClass().getMethod("parseContents");
                method.invoke(bean);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
} 
二,。电影服务班

@Component
public class Movie
{
               private static final Logger _log = LoggerFactory.getLogger(Movie.class);

    @Autowired
    private Environment _env; 

    public void parseContents()
    {
          _log.info("enter into parsecontents");
    }
}
错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.content.partner.Movie' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:568)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117)
at com.apalya.myplex.partner.ContentPartnerGenericImpl.process(ContentPartnerGenericImpl.java:112)
at com.apalya.myplex.partner.ContentPartnerGenericImpl.partneGeneric(ContentPartnerGenericImpl.java:96)
at com.apalya.myplex.partner.ContentPartner.ingestContent(ContentPartner.java:187)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)

据我所知,你只需要改变

objectbean=\u appContext.getBean(“com.content.partner.Movie”)

进入

objectbean=\u appContext.getBean(“电影”)


假设您已经扫描了所有这些“组件”。

使用对象bean=\u appContext.getBean(“电影”);&@电影类上的组件(“电影”)