@Autowired没有工作(依赖未注入)

时间:2015-07-24 22:21:42

标签: java spring dependency-injection

今天我遇到了春天的问题。 这是我的代码:

SpringConfig.java

package com.berrigan.addonsserver.springconfig;

import com.berrigan.addonsserver.EndPoint;
import org.glassfish.tyrus.server.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages={"com.berrigan.addonsserver","com.berrigan.addonsserver.modules"})
public class SpringConfig {
    @Bean
    public Server server(EndPoint end){
        return new Server("localhost",8025, "/margonem",null,end.getClass());
    }
}

EndPoint.java

package com.berrigan.addonsserver;

import com.berrigan.addonsserver.modules.ResponseGenerator;
import com.berrigan.addonsserver.util.JSONOutputTemplate;
import com.google.gson.Gson;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("endPoint")
@ServerEndpoint(value="/server")
public class EndPoint {

    @Autowired
    public ResponseGenerator rg;

    public EndPoint(){  System.out.println("Sucks");}
    public EndPoint(ResponseGenerator rg){ this.rg = rg; System.out.println("Rg is set");}

    @OnMessage
    public String onMessage(String message, Session s){
        Gson g = new Gson();
        if(rg == null) System.out.println("DI DOESN'T WORK");
        else{
        JSONOutputTemplate output = rg.generateResponse(null);//new JSONOutputTemplate();//
        g.toJson(output);
        }
        return message;
    }

    public void setResponseGenerator(ResponseGenerator rg){
        System.out.println("This works");
        this.rg = rg;
    }
}

ResponseGenerator.java

package com.berrigan.addonsserver.modules;

import com.berrigan.addonsserver.annotations.AuthorizationRequired;
import com.berrigan.addonsserver.util.JSONInputTemplate;
import com.berrigan.addonsserver.util.JSONOutputTemplate;
import org.springframework.stereotype.Component;

@Component
public class ResponseGeneratorImpl implements ResponseGenerator{

    @Override
    @AuthorizationRequired
    public JSONOutputTemplate generateResponse(JSONInputTemplate data) {
        return new JSONOutputTemplate();
    }
}

我的输出说"糟糕"和" DI DOESN'工作"。当我试图从ApplicationConfig获取ResponseGenerator bean时,它没有空。我不知道出了什么问题。提前谢谢。

0 个答案:

没有答案