spring注解开发AnnotationConfigApplicationContext的使用

来源:互联网 发布:淘宝上钻要多少销量 编辑:程序博客网 时间:2024/05/01 14:58
使用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");    }}

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

@Configurationpublic 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();    }}

“`

0 0
原创粉丝点击