MyEclipse配置Spring框架(基础篇)
2020-12-13 03:13
标签:设置 pack property print 基础 schema pre odi student 一、新建项目,添加spring的相关jar包等 二、创建相关类以及属性和方法 Student.java 三、配置xml文件 自动装配方法一:设置autowire(这里为byName) 装配方法:Student类的成员变量名对应bean的id。 自动装配方法二: 装配方法:name对应Student类中名为course的成员变量,ref对应当前xml文件中id为course的bean。 四、编写测试类 MyEclipse配置Spring框架(基础篇) 标签:设置 pack property print 基础 schema pre odi student 原文地址:https://www.cnblogs.com/YeHuan/p/11071915.htmlpackage com.yh;
public class Student implements People {
private Course course;
@Override
public void breath() {
// TODO Auto-generated method stub
System.out.println("呼吸");
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
xml version="1.0" encoding="UTF-8"?>
beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
bean id="student" class="com.yh.Student" autowire="byName">bean>
bean id="course" class="com.yh.Course">bean>
beans>
xml version="1.0" encoding="UTF-8"?>
beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
bean id="student" class="com.yh.Student">
property name="course" ref="course">property>
bean>
bean id="course" class="com.yh.Course">bean>
beans>
package com.yh;
import org.junit.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemoTest {
@Test
public void demo01(){
String xmlPath="applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
Student stu = (Student)context.getBean("student");
stu.breath();
stu.getCourse().showCourse();
}
}
上一篇:Unity-动态显示窗口制作思路
下一篇:在win7下安装VC6.0