发表于: 2018-03-17 21:45:37

1 575


今天完成的事情:spring这个东西,有点意思

之前有个问题,在XML里写一堆,真的比直接new方便吗,

今天学到了一种用法:

放这么一句进XML里面

<context:component-scan base-package="spring.pojo"/>

然后再class里定义,如:

@Component("a")

public class A{

@Autowired

private B b;

Name = "a";

}


@Component("b")

public class B{

Name = "b";

}

然后在class C中

public class C {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext

        (new String[] { "applicationContext.xml" });

         A a = (A) context.getBean("a");        

        System.out.println(a.getName());

        System.out.println(a.getB().getName());

    }

}

这样,只需要用几个@Conponent 来讲class定义为组件

通过一个

        ApplicationContext context = new ClassPathXmlApplicationContext

        (new String[] { "applicationContext.xml" });

方法就可以轻松调用,妈妈再也不用操心接口的吻合了

明天计划的事情:Debug模式,看看云服务器行情

遇到的问题:emmm,spring里好多关键字,欺负我这英语不好的人,还要背单词

收获:学到了一种整合class的方法






返回列表 返回列表
评论

    分享到