码迷,mamicode.com
首页 > 编程语言 > 详细

初始Spring框架

时间:2017-10-16 18:01:35      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:tag   XML   blog   反转   private   exp   override   分享   步骤   

Spring入门案例的步骤;
    1.找依赖
       spring-beans.4.2.0.jar
            附带了core核心 和commons-logging
       spring-context.4.2.0.jar
               spring-expression.4.2.0.jar
               spring-aoplple.4.2.0.jar
               spring-aop.4.2.0.jar
    2.HappyService类型
 
    3 beans 根节点下有N个bean节点
      <bean id="service" class="类型的全名">

    4.容器
    ApplicationContext ctx=new  ClassPathXmlApplicationContext("applictionContext.xml");
        HappyService service=  (HappyService)ctx.getBean("service");

下面我们写一个例子:

实体类:

private String info;
private Integer age;

public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

public void setInfo(String info) {
this.info = info;
}
public String getInfo() {
return info;
}

@Override
public String toString() {
return "HappyService{" +
"info=‘" + info + \‘‘ +
", age=" + age +
‘}‘;
}


applicationContext.xml文件:
<!--IOC 控制反转-->
<bean id="happyService" class="cn.happy.day01.HappyService">
<!--DI 依赖-->
<property name="info" value="李四"></property>
<property name="age" value="20"></property>
</bean>

测试类:
@Test
public void Test(){
//Spring容器
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//返回值必须进行强转
HappyService bean = (HappyService)context.getBean("happyService");
System.out.println(bean);
}



结果如下:

技术分享

 





 

初始Spring框架

标签:tag   XML   blog   反转   private   exp   override   分享   步骤   

原文地址:http://www.cnblogs.com/sujulin/p/7607161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!