@Inject for manually instantiated object?

Asked

Viewed 281 times

1

I have a class called Application which is a listener, it instantiates objects of ServidorRadius where it needs to access the repositories that are injected with CDI, however radiusRepository is returning null.

Application:

@ApplicationScoped
public class Application implements ServletContextListener {

    @Inject
    private Logger log;

    @Inject
    private  RadiusRepository radiusRepository;

    @Inject
    private  RadiusRegistration radiusRegistration;

    private  static List<Radius> radius;
    private  static List<ServidorRadius> servidor = new ArrayList<ServidorRadius>();

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try{
            radius = radiusRepository.findAllRadius();
            for(Radius r : radius){
                ServidorRadius radiusS = new ServidorRadius(r.getMetodoDeAutenticacao(), r.getAcctPort(), r.getAuthPort());
                if(r.getStart())
                    radiusS.start(true, true);
                servidor.add(radiusS);
            }
        } catch (NoResultException ne){
            //
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try{
            for(ServidorRadius sr : servidor){
                sr.stop();
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void addServidorRadius(Radius radius){
        servidor.add(new ServidorRadius(radius.getMetodoDeAutenticacao(), radius.getAcctPort(), radius.getAuthPort()));
    }

    public static void stopRadius(Radius radius){
        try{
            for(ServidorRadius sr : servidor){
                if(sr.getAcctPort() == radius.getAcctPort() && sr.getAuthPort() == radius.getAuthPort())
                    sr.stop();
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void startRadius(Radius radius){
        try{
            Boolean started = false;
            for(ServidorRadius sr : servidor){
                if(sr.getAcctPort() == radius.getAcctPort() && sr.getAuthPort() == radius.getAuthPort()){
                    sr.start(true, true);
                    started = true;
                }
            }
            if(!started){
                addServidorRadius(radius);
                startRadius(radius);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

}

ServidorRadius:

public class ServidorRadius extends RadiusServer {

    private MetodoDeAutenticacao metodoDeAutenticacao;

    @Inject
    private RadiusRepository radiusRepository;

    @Inject
    private LoginRepository loginRepository;

    @Inject
    private RadiusRegistration radiusRegistration;

    private Login login;

    private AccessRequest accessRequest;

    private Concentrador concentrador;

    public ServidorRadius(MetodoDeAutenticacao metodoDeAutenticacao, Integer acctPort, Integer authPort){
        this.metodoDeAutenticacao = metodoDeAutenticacao;
        this.setAcctPort(acctPort);
        this.setAuthPort(authPort);
    }

    @Override
    public String getSharedSecret(InetSocketAddress client) {
        if(radiusRepository == null)
            System.out.println("Is null");
        try{
            concentrador = radiusRepository.findConcentradorByIp(client.getAddress().getHostAddress());
            if(concentrador.getIp().equals(client.getAddress().getHostAddress()))
                return concentrador.getSenha();
        } catch (NoResultException ne){
            // TODO
        } catch (Exception e){
            //TODO

            e.printStackTrace();
        }

        return null;
    }

}

Output

18:46:21,741 INFO  [stdout] (Radius Auth Listener) Is null
18:46:21,742 ERROR [stderr] (Radius Auth Listener)    java.lang.NullPointerException
18:46:21,742 ERROR [stderr] (Radius Auth Listener)  at com.authnet.util.ServidorRadius.getSharedSecret(ServidorRadius.java:60)
18:46:21,742 ERROR [stderr] (Radius Auth Listener)  at org.tinyradius.util.RadiusServer.listen(RadiusServer.java:332)
18:46:21,742 ERROR [stderr] (Radius Auth Listener)  at org.tinyradius.util.RadiusServer.listenAuth(RadiusServer.java:289)
18:46:21,742 ERROR [stderr] (Radius Auth Listener)  at org.tinyradius.util.RadiusServer$1.run(RadiusServer.java:108)

I believe the problem is when I urge the ServidorRadius with the command new, but I don’t know any other alternative.

  • No, there’s no way. If you are explicitly instantiating the CDI container does not know this instance, does not manage it, then does not act on the injections you put in them. Solution to use CDI: refactor your class to be a managed bean, also adjusting its constructor, go through methods, then you’ll get it without problems.

1 answer

1


You need to identify the root scopeRepository or create a product:

@ApplicationScoped
public class RadiusRepository {
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.