【问题标题】:Get bean of @Service annotated class?获取@Service注解类的bean?
【发布时间】:2013-04-11 10:23:13
【问题描述】:

在我的 Web 应用程序中,我没有使用 applicationContext.xml。如何获取@Service注解类的bean?

如果我在我的 Web 应用程序中使用了 applicationContext.xml,我必须每次都加载 applicationContext.xml 以获取 @Service 注释类的 bean。

我是这样用的

WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext);
ServerConnection  con = (ServerConnection ) ctx.getBean("con");

我的服务类将是,

@Service  or @Service("con")
public class ServerConnection {

    private  TServerProtocol tServerProtocol;
    private  URI uri;

    public TServerProtocol getServerConnection(){

        System.out.println("host :"+host+"\nport :"+port);

        try {
            uri = new URI("tcp://" + host + ":" + port);
        } catch (URISyntaxException e) {
            System.out.println("Exception in xreating URI Path");
        }

        tServerProtocol = new TServerProtocol(new Endpoint(uri));
        return tServerProtocol;
    }
}

有没有其他方法可以得到这个类的bean?

或

对于Spring3.x中的核心应用程序和Web应用程序,获取@Service注释类的bean的正确方法是什么?

【问题讨论】:

    标签: java spring web-applications spring-mvc spring-annotations


    【解决方案1】:

    ApplicationContext context=SpringApplication.run(YourProjectApplication.class, args);

    这个上下文可以用来获取注解@Bean和@Service创建的bean 作为

    context.getBean(className.class);

    【讨论】:

      【解决方案2】:

      如果您使用基于注解的配置,则使用@Autowired 按类型获取 bean,或使用@Resource 按名称获取 bean。对任何特定属性只使用其中之一(以减少混淆)。

      @Autowired
      private ServerConnection connection;
      
      @Resource(name = "con")
      private ServerConnection connection;
      

      There's also @Inject,但我不喜欢这样,因为随着 bean 数量的增加,它变得不那么好了。 YMMV。

      【讨论】:

        【解决方案3】:

        由于您正在使用注释,我相信您更喜欢使用 @Autowired。如果你有一个类会调用ServerConnection(比如说CallingClass 在包com.foo 下),那么它会是这样的:

        package com.foo;
        
        @Component
        public class CallingClass{
        
           @Autowired
           private ServerConnection serverConnection
        
            // add your getters and setters
        }
        

        然后在您的 applicationContext.xml 中添加以下内容

        <context:component-scan base-package="com.foo" />
        

        希望我回答了你的问题。

        【讨论】:

        • 我已经用过这个了。我的问题是如何通过这种方式获取此类的实例。
        • 除了这个和你上面提到的,我相信没有别的办法,那就是如果你想利用Spring的DI。
        【解决方案4】:

        如果你在 spring 之外的某个 bean 中工作,那么你可以使用普通的 Spring 注释:

        @Autowired
        private Dependency1 dependency1;
        
        @Autowired
        private Dependency2 dependency2;
        
        @Autowired
        private Dependency2 dependency2;
        

        然后从这个类内部调用一次:

        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        

        注入所有依赖项。如果您有多个依赖项,可能会更方便。

        【讨论】:

        • 嗨 Maaksym,我尝试使用 SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);但它确实注入了 applicationContext.xml 文件中描述的 bean,而不是注释的 bean。
        • @Amit Sharma 请将您的问题作为单独的问题发布
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-17
        • 1970-01-01
        • 2020-02-17
        • 2014-09-20
        • 1970-01-01
        • 1970-01-01
        • 2014-02-21
        相关资源
        最近更新 更多