简体   繁体   English

获取@Service注释类的bean?

[英]Get bean of @Service annotated class?

In my web application, I am not using applicationContext.xml . 在我的Web应用程序中,我没有使用applicationContext.xml How can I get the bean of @Service annotated class? 如何获取@Service注释类的bean?

If I used the applicationContext.xml in my web application, I have to load the applicationContext.xml every time to get the bean of the @Service annotated class. 如果我在我的Web应用applicationContext.xml中使用了applicationContext.xml ,则每次都必须加载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 bean of this class ? 没有其他方法来获得这个类的bean

or 要么

What is the proper way to get a bean of @Service annotated class in case of core application and web application in Spring3.x? 在Spring3.x中核心应用程序和Web应用程序的情况下,获取@Service注释类的bean的正确方法是什么?

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

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

context.getBean(className.class);

If you are using annotation-based configuration, you use @Autowired to get the bean by its type or @Resource to get the bean by its name. 如果使用基于注释的配置,则使用@Autowired按类型获取bean,或使用@Resource以获取bean的名称。 Use only one of these for any particular property (to keep confusion down). 对于任何特定属性,只使用其中一个(以避免混淆)。

@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. 还有@Inject ,但我不喜欢它,因为随着bean数量的增加,它变得不那么好了。 YMMV. 因人而异。

Since you are making use of annotation, I believe you would prefer the use of @Autowired . 由于您正在使用注释,我相信您更愿意使用@Autowired If you have a class that will call ServerConnection (lets say CallingClass under package com.foo ), then this is how it would look like: 如果你有一个将调用ServerConnection的类(比如com.foo包下的CallingClass ),那么它就是这样的:

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. 希望我回答你的问题。

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. 如果您有多个依赖项,可能会更方便。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 私有静态类Spring bean注释@Service是否可以自动装配? - Could private static class Spring bean annotated @Service be autowired? Spring 没有为 @service 注释的 class 创建 bean,当它的方法之一用 @transactional 注释时 - Spring is not creating the bean for @service annotated class when one of its method is annotated with @transactional 将带有@Component 注释的类更改为带有@Bean 注释的方法 - Changing a class annotated @Component to @Bean annotated method Hibernate Annoration:如何获取标注为@Repository的类的Bean - Hibernate Annoration: how to get the bean of class annotated as @Repository 如何在另一个类中使用@Service注释类作为@Autowired bean,该类位于Service类的组件扫描包之外? - How to use @Service annotated class as an @Autowired bean in another class which is outside the component scan package of the Service class? 在@Service注释的类中使用@Value注入时获取空值,但能够在@Controller注释的类中获取值 - getting null value when using @Value injection in class annotated by @Service but able to get values in class annotated by @Controller Spring Boot - 如何通过名称获取用@Service 注释的类 - Spring Boot - how to get a class annotated with @Service by it's name Spring 3.2:无法将 @Service 注释 bean 自动装配到 @Bean 注释 bean - 发生 BeanNotFoundException - Spring 3.2: Cannot autowire @Service annotated bean into a @Bean annotated bean - BeanNotFoundException occurring 自动连线的带注释的Bean在侦听器类中为null - Autowired annotated bean gets null in Listener class SpringBootApplication没有使用@Service注释的bean - @Service annotated bean wasn't picked up by SpringBootApplication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM