Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package edu.educacionit;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.ComponentScan;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
  10. import org.springframework.stereotype.Component;
  11.  
  12. @Configuration
  13. @ComponentScan({"edu.educacionit"})
  14. class AppConfig {
  15.  
  16. @Bean
  17. public static PropertySourcesPlaceholderConfigurer get() {
  18. return new PropertySourcesPlaceholderConfigurer();
  19. }
  20. }
  21.  
  22. @Component
  23. class Dependencia {
  24. public Dependencia() {
  25. System.out.println("Dependencia ...");
  26. }
  27. public void mostrarFuncionamiento() {
  28. System.out.println("Dependencia funcionando");
  29. }
  30. }
  31.  
  32. @Component("AI")
  33. class AutoInstanciable {
  34.  
  35. private Dependencia dependencia;
  36.  
  37. @Autowired
  38. public AutoInstanciable(Dependencia dependencia) {
  39. this.dependencia = dependencia;
  40. }
  41.  
  42. public AutoInstanciable() {
  43. System.out.println("Se instancia a traves del mecanismo de spring");
  44. }
  45.  
  46. public void probar() {
  47. System.out.println("Probando...");
  48. this.dependencia.mostrarFuncionamiento();
  49. }
  50. }
  51.  
  52. public class App
  53. {
  54. public static void main( String[] args )
  55. {
  56. ApplicationContext appContext =
  57. new AnnotationConfigApplicationContext(AppConfig.class);
  58.  
  59. AutoInstanciable ai = appContext.getBean("AI", AutoInstanciable.class);
  60. ai.probar();
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement