Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8340107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:05:07+00:00 2026-06-09T05:05:07+00:00

I try to use Spring framework with @Autowired annotation to get a Service class.

  • 0

I try to use Spring framework with @Autowired annotation to get a Service class.

Here my class (this class is called by a WebSocketServlet) which need to use the Service (I get NullPointerException on call of it):

public class SignupWebSocketConnection extends MessageInbound {

    private static final Logger log = LoggerFactory.getLogger(SignupWebSocketConnection.class);

    @Autowired
    private UserService userService;

    @Override
    protected void onOpen(WsOutbound outbound) {
        log.info("Connection done");
    }

    @Override
    protected void onClose(int status) {
        log.info("Connection close");
    }

    @Override
    protected void onBinaryMessage(ByteBuffer byteBuffer) throws IOException {
        log.warn("Binary message are not supported");
        throw new UnsupportedOperationException("Binary message are not supported");
    }

    @Override
    protected void onTextMessage(CharBuffer charBuffer) throws IOException {

        User userTest = new User("log", "pass", "ema");
        userService.create(userTest);

    }
}

Here my web.xml :

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

Here my ApplicationContext.xml :

<bean id="userService" class="service.UserService">

If I try to use the Service in the test package (I using maven) with explicit call of the ApplicationContext-test.xml, it’s working fine… The ApplicationContext.xml and ApplicationContext-test.xml are same (just param to database changes)

Thank’s a lot to all of you 😀

EDIT :

I deplace my applicationContext.xml in ressources folder and create a class to test :

   public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) ctx.getBean("userService");
        User user = new User("log","pas","emai");
        userService.create(user);
    }

I get NullPointerException also when calling userService.create() ….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:05:09+00:00Added an answer on June 9, 2026 at 5:05 am

    When not using aspects/code weaving, you need to get your beans from the Springs’ ApplicationContext (if the object is already created by Spring, all the @Autowired fields will get set), as Spring is not aware of you creating objects with new. The usual way of writing web-applications with Spring is using DispatcherServlet and Controllers, not Servlets, for a quick tutorial see for example here. The reasoning behind this is that Spring will take care of creating all the @Controller/@Service/etc-annotated classes, autowire the marked fields without fuss, handle delegating the requests to correct controller methods and more.

    However, it is possible to fetch the application context and the beans in Servlets also, but the code is much less clean, not so easily tested and probably becomes a nightmare to maintain in the long run. For example:

       WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
       MyBeanFromSpring myBean =(MyBeanFromSpring)springContext.getBean("myBeanFromSpring");
       //Do whatever with myBean...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error when I try to use time_ago_in_words : Comparison of String
I try to use Spring to develop liferay portlets (version 6.0.6), could i use
I try to use this mapping : @Entity @Table(name=ecc.\RATE\) @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name=DISCRIMINATOR, discriminatorType= DiscriminatorType.STRING) public
I am trying to use the Spring Framework IoC Container to create an instance
I'm trying to use the @Test(expected = RuntimeException.class) annotation in order to test for
I'm trying to access @Service defined class Interfaces are defined, here is an implementation
I have a problem with Spring and JPA. Basically I try to use JPA
i currently build an application use spring as framework. and i want to test
I am facing an issue while trying to use the spring android framework. I
I'm learning to use Springsource Tool Suite (STS) and the Spring framework for development.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.