java 获取@Service 注释类的bean?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15946371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:23:32  来源:igfitidea点击:

Get bean of @Service annotated class?

javaspringweb-applicationsspring-mvcspring-annotations

提问by Human Being

In my web application, I am not using applicationContext.xml. How can I get the bean of @Serviceannotated class?

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

If I used the applicationContext.xmlin my web application, I have to load the applicationContext.xmlevery time to get the bean of the @Serviceannotated class.

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

I used this way

我用这种方式

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

My Service class will be as ,

我的服务类将是,

@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;
    }
}

Is there any other way to get a beanof this class ?

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

or

或者

What is the proper way to get a bean of @Serviceannotated class in case of core application and web application in Spring3.x?

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

采纳答案by Donal Fellows

If you are using annotation-based configuration, you use @Autowiredto get the bean by its type or @Resourceto get the bean by its name. Use only one of these for any particular property (to keep confusion down).

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

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

There's also @Inject, but I don't like that as it gets less nice as the number of beans goes up. YMMV.

还有@Inject,但我不喜欢那样,因为随着豆子数量的增加它变得不那么好。天啊。

回答by manoj jangam

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

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

this context can be used to get the beans created by annotation @Beanand @Serviceas

此上下文可用于获取通过注释创建的 bean@Bean@Service作为

context.getBean(className.class);

context.getBean(className.class);

回答by Maksym Demidas

If you working in some bean outside spring then you can use normal Spring annotations:

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

@Autowired
private Dependency1 dependency1;

@Autowired
private Dependency2 dependency2;

@Autowired
private Dependency2 dependency2;

then call one time from inside of this class:

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

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

to inject all dependencies. It may be more convinient if you have multiple dependencies.

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

回答by cRx

Since you are making use of annotation, I believe you would prefer the use of @Autowired. If you have a class that will call ServerConnection(lets say CallingClassunder package com.foo), then this is how it would look like:

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

package com.foo;

@Component
public class CallingClass{

   @Autowired
   private ServerConnection serverConnection

    // add your getters and setters
}

Then on your applicationContext.xml just add the following

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

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

Hope I answered your question.

希望我回答了你的问题。