2019年4月8日
一、ApplicationContext.xml配置及從SpringIOC容器中獲取對象
springIOC容器:創建對象,給對象的屬性賦值
必須jar包:可在mvn倉庫查找
spring-aop: 開發AOP
spring-beans:處理bean
spring-context:處理spring的上下文
spring-core:spring核心
spring-expression:spring表達式
commo-logging.jar:三方提供的日誌jar
創建一個Student類:
public class Student {
private int stuNo;
private String stuName;
private int stuAge;
}
創建ApplicationContext.xml配置文件
<bean id="student"class="Student"> //此處的class: Student指定一個已經創建的Java類,id:唯一標識此對象 <property name="stuNo"value="2"></property> //property:屬性 <property name="stuAge"value="21"></property> <property name="stuName"value="lisi"></property> </bean> |
從springIOC中獲取id爲"student"的student對象
ApplicationContext context=newClassPathXmlApplicationContext("ApplicationContext.xml");
Studentstu=(Student)context.getBean("student");
System.out.println(stu);
二、SpringIOC(是一種超級工廠)
IOC:控制反轉,反轉的是獲取對象的方式,後來方便理解,改名爲DI:依賴注入。
IOC使用實例:
Student類
package com.springTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Student {
private int stuNo;
private String stuName;
private int stuAge;
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
@Override
public String toString() {
return "Student{" +
"stuNo=" + stuNo +
", stuName='" + stuName + '\'' +
", stuAge=" + stuAge +
'}';
}
public void learn(String name) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Course course = (Course)context.getBean(name);
course.learn();
}
}
Course接口
package com.springTest;
public interface Course {
void learn();
}
HtmlCourse和JavaCourse
package com.springTest;
public class HtmlCourse implements Course {
public void learn() {
System.out.println("learn Html!");
}
}
package com.springTest;
public class JavaCourse implements Course {
public void learn() {
System.out.println("learn Java!");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.springTest.Student">
<property name="stuName" value="lisi"></property>
<property name="stuAge" value="24"></property>
<property name="stuNo" value="7"></property>
</bean>
<bean id="javaCourse" class="com.springTest.JavaCourse">
</bean>
<bean id="htmlCourse" class="com.springTest.HtmlCourse"></bean>
</beans>
StudentTest調用
package com.Test;
import com.springTest.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Student stu = (Student)context.getBean("student");
System.out.println(stu);
stu.learn("javaCourse");
}
}