Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.educacionit;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
- import org.springframework.stereotype.Component;
- @Configuration
- @ComponentScan({"edu.educacionit"})
- class AppConfig {
- @Bean
- public static PropertySourcesPlaceholderConfigurer get() {
- return new PropertySourcesPlaceholderConfigurer();
- }
- }
- @Component
- class Dependencia {
- public Dependencia() {
- System.out.println("Dependencia ...");
- }
- public void mostrarFuncionamiento() {
- System.out.println("Dependencia funcionando");
- }
- }
- @Component("AI")
- class AutoInstanciable {
- private Dependencia dependencia;
- @Autowired
- public AutoInstanciable(Dependencia dependencia) {
- this.dependencia = dependencia;
- }
- public AutoInstanciable() {
- System.out.println("Se instancia a traves del mecanismo de spring");
- }
- public void probar() {
- System.out.println("Probando...");
- this.dependencia.mostrarFuncionamiento();
- }
- }
- public class App
- {
- public static void main( String[] args )
- {
- ApplicationContext appContext =
- new AnnotationConfigApplicationContext(AppConfig.class);
- AutoInstanciable ai = appContext.getBean("AI", AutoInstanciable.class);
- ai.probar();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement