转自:https://www.cnblogs.com/kaituorensheng/p/8024199.html
使用AnnotationConfigApplicationContext可以实现基于Java的配置类加载Spring的应用上下文.避免使用application.xml进行配置。在使用spring框架进行服务端开发时,个人感觉注解配置在便捷性,和操作上都优于是使用XML进行配置;

 

很简单的接口output

public interface outPut {

    public void helloworld();
}

实现类

public class outPutImpl implements outPut {

    public void helloworld() {
        System.out.println("hellworld");
    }

}

 

注解的使用 实际上我觉得这个是充当一个容器的角色

@Configuration
public class hello {

    @Bean(name = "helloworl")
    public outPut impl() {
        return new outPutImpl();
    }
}

 

这里的重点在于不是引用ClassPathXmlApplicationContext() 这个是对xml配置spring的使用

public class Client {

    public static void main(String[] args) {



        ApplicationContext context = new AnnotationConfigApplicationContext(hello.class);
        outPut outPut = (com.spring.lzx.interfacer.outPut) context.getBean("helloworl");
        outPut.helloworld();
    }
}
posted on 2017-10-28 22:40  Sharpest  阅读(1498)  评论(0编辑  收藏  举报