@Autowired,@ Resource不工作

时间:2016-08-11 09:25:55

标签: java spring dependency-injection inversion-of-control

我正在使用Spring来管理我的bean的生命周期。 我尝试使用 @Autowired @Resource 注释来成功 ioc ,但我总是让名人 的的NullPointerException

我的界面。

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

        public String obtainToken();
    }

实施。

    @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;
        }

对我的对象的调用

    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();
        }

我的 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>

我的 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>

我的 applicationContext-security.xml

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

我不知道问题出在哪里。

0 个答案:

没有答案