I'm used to working with Java EE where you can simply inject objects with the @Inject annotation however I do not know how to do it without using Java ee.
What I want is that the same object with the same instance can be used in different classes, for example for the following classes:
public class Main {
Foo foo;
public static void main(String[] args) {
foo.setMensaje("hola");
Bar bar = new Bar();
bar.imprimirFoo();
}
}
Bar:
public class Bar {
private Foo foo;
public void imprimirFoo(){
System.out.println(foo.getMensaje());
}
}
Foo:
public class Foo {
private String mensaje;
public String getMensaje() {
return mensaje;
}
public void setMensaje(String mensaje) {
this.mensaje = mensaje;
}
}
I would like you to print me twice hello, but I do not know how to do it without using the @Inject annotation