1.3 Spring的优势(理解)
1.3.1 方便解耦,简化开发
通过Spring 提供的IOC容器,可以将对象间的依赖关系交由Spring进行控制,避免硬编码所造成的过度耦合用户也不必再为单例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用
1.3.2 AOP 编程的支持
通过 Spring的AOP功能,方便进行面向切面编程,许多不容易用传统OOP实现的功能可以通过AOP轻松实现
1.3.3 声明式事务的支持
可以将我们从单调烦闷的事务管理代码中解脱出来,通讨声明式方式灵活的进行事务管理,提高开发效率和质量
1.3.4 方便程序的测试
可以用非容器依赖的编程方式进行几乎所有的测试工作,测试不再是昂贵的操作,而是随手可做的事情
1.3.5 方便集成各种优秀框架
Spring对各种优秀框架(Struts、Hibemate、Hessian、Quartz等)的支持
1.3.6 隆低JavaEE API的使用难度
Spring对JaveEE API(如JDBC、JavaMail、远程调用等)进行了薄薄的封装层,使这些API的使用难度大为降低
1.3.7 Java 源码是经典学习范例
Spring的源代码设计精妙、结构清晰、匠心独用,处处体现着大师对Java 设计模式灵活运用以及对Java技术的高深造诣。它的源代码无意是Java技术的最佳实践的范例
使用Spring
package cn.hdc;
public class Student {
private String name;
private String school;
public void setName(String name) {
this.name = name;
}
public void setSchool(String school) {
this.school = school;
}
public void introduce(){
System.out.println("大家好!我叫"+name+",来自"+school);
}
}
package cn.hdc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentTest {
public static void main(String[] args) {
//获取Spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//从Spring容器中获取Student对象
Student student = (Student) applicationContext.getBean("Student");
//测试学生对象方法:introduce
student.introduce();
}
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Student" class="cn.hdc.Student">
<property name="name" value="田文杰"></property>
<property name="school" value="邯郸学院"></property>
</bean>
依赖注入的三种方式:
对于Person类
package Btest;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
1 构造方法实例化
1.1Setter方法注入
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Person" class="Btest.Person">
<property name="name" value="田文杰"></property>
<property name="age" value="20"></property>
</bean>
</beans>
1.2构造方法注入
构造方法实例化比较常用
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Person" class="Btest.Person">
<constructor-arg name="name" value="田文杰"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
</bean>
</bean>
2静态工厂实例化
package Btest;
public class Bean2 {
//静态工厂实例化
public static Person createBean(){
return new Person();
}
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "Person2" class = "Btest.Bean2" factory-method="createBean">
<property name="name" value="田文杰"></property>
<property name="age" value="20"></property>
</bean>
</beans>
3动态工厂实例化
package Btest;
public class Bean3 {
public Person createBean(){
return new Person();
}
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Bean3" class="Btest.Bean3" ></bean>
<bean id = "Person3" class = "Btest.Bean3" factory-bean="Bean3" factory-method="createBean">
<property name="name" value="田文杰"></property>
<property name="age" value="20"></property>
</bean>
</beans>
测试类:
package Btest;
import cn.hdc.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) applicationContext.getBean("Person");
Person person2 = (Person) applicationContext.getBean("Person2");
Person person3 = (Person) applicationContext.getBean("Person3");
System.out.println(person);
System.out.println(person2);
System.out.println(person3);
}
}