1

I'm working with Spring to manage the life cycle of my beans. I'm trying to use @Autowired or @Resource annotations to make successful ioc but i'm always getting the famous NullPointerException.

My interface.

    /**
     * @author Soufiane NAJAH
     *
     * 
     */
    public interface ITokenService {

        public String obtainToken();
    }

The implementation.

    @Service
    public class ITokenServiceImpl implements ITokenService {

        // TODO NAJ : Déplacer l'url vers le fichier properties
        private static String tokenRequestParameters = "http://localhost:8080/****/****/*****?grant_type=******&client_id=*****&"
                + "client_secret=*****&username=******&password=*******";

        private static String access_token = "";

        public String obtainToken() {

            try {
                URL url = new URL(tokenRequestParameters);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                if (conn.getResponseCode() != 200) {
                    throw new RuntimeException("Failed: HTTP error code: "
                            + conn.getResponseCode());
                }
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        (conn.getInputStream())));
                // System.out.println("OutPut from the server ......");
                JSONObject myObject = new JSONObject(br.readLine());
                // System.out.println("access_token " + myObject.get("value"));
                access_token = (String) myObject.get("value");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // System.out.println(access_token);
            return access_token;
        }

The call to my object

    public abstract class GenericWebServiceClientImpl implements
            GenericWebServiceClientInterface {

        private static final long serialVersionUID = 1L;

        protected Client client;
        protected WebTarget baseTarget;
        private boolean initialized = false;

        @Autowired
        private ITokenService tokenService;

        public GenericWebServiceClientImpl() {
            init();
        }

My web.xml

            <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
           </listener>



    <context-param>
        <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
      <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
      <!-- valeur initiale "server" changee en "client" car server provoque erreur ViewExpiredException -->
      <param-value>client</param-value>
     </context-param>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                WEB-INF/web-context.xml
            </param-value>
        </context-param>

My webContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

        <import resource="classpath:spring/applicationContext-security.xml"/>

    </beans>

My applicationContext-security.xml

    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="ma.net.s2m.service.rs.client.token"></context:component-scan>

I have no idea where the problem is.

Soufiane Rabii
  • 427
  • 2
  • 8
  • 29
  • 1
    my question is not about the **NullPointerException**, it's about the error i made to make spring unable to inject my beans. – Soufiane Rabii Aug 11 '16 at 09:32

0 Answers0