1

I have some servlets and some websocket servlets in my java application.

I use latest stable spring framework.

what I know is that because the servlets are not called by a different bean so they are not injectable and I need to get the applicationContext and use getBean to get the required objects.

is it true?

or can I use @Autowired in servlets somehow ?

so I want to be able to do the following:

@ServerEndpoint(value="/ServConnect")
public class ServConnect  {
@Autowired UserDb userDb;

Instead of

@ServerEndpoint(value="/ServConnect")
public class ServConnect  {

UserDb userDb;

public void ServConnect() {
  // get application context somehow
 userDb = appCtx.getBean("userDb");
}

thank you

ufk
  • 30,912
  • 70
  • 235
  • 386
  • Please provide your Spring version, "latest stable" can be very different during time. – Eel Lee Nov 05 '13 at 09:20
  • Looks like it has already been answered: http://stackoverflow.com/questions/11843690/autowiring-in-servlet Personally I would use SpringMVC which would allow you to easily autowire objects to your controllers. Though if this is not your decision the link above may help – cowls Nov 05 '13 at 09:22
  • i use spring framework 3.2.4 – ufk Nov 05 '13 at 09:25
  • in a websocket servlet I don't have init and i don't have ServletConfig, so the answer that you posed answers the question if it's a regular servlet. but what about websockets servlets? – ufk Nov 05 '13 at 09:27
  • The answer is this: If Spring isn't managing the instantiation of your `ServConnect` class, it cannot inject anything. – Sotirios Delimanolis Nov 05 '13 at 13:19

0 Answers0