标签: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);
}
结果如下:
标签:tag XML blog 反转 private exp override 分享 步骤
原文地址:http://www.cnblogs.com/sujulin/p/7607161.html