4

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

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.

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 ?

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?

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Human Being
  • 8,269
  • 28
  • 93
  • 136

4 Answers4

6

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

this context can be used to get the beans created by annotation @Bean and @Service as

context.getBean(className.class);

manoj jangam
  • 271
  • 3
  • 10
2

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. 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. YMMV.

Community
  • 1
  • 1
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
0

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 CallingClass under package com.foo), then this is how it would look like:

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

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

Hope I answered your question.

cRx
  • 39
  • 2
  • I used this already. My question is how to get the instance of this class apart from this way. – Human Being Apr 11 '13 at 10:38
  • Apart from this and what you have mentioned above, I believe there's no other way, that is if you want to leverage Spring's DI. – cRx Apr 11 '13 at 10:53
0

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

@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.

Maksym Demidas
  • 7,707
  • 1
  • 29
  • 36
  • hi Maaksym, i tried using SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); but it does inject the beans which are described in applicationContext.xml file but not the annoted beans. – Amit Sharma Oct 08 '13 at 12:32
  • @Amit Sharma please post your problem as a separate question – Maksym Demidas Oct 08 '13 at 15:25