1.Spring框架的描述
1.Spring是一个开源框架
2.Spring为简化企业级应用开发而生
3.Spring是一个IoC/DI和AOP容器框架
2.具体描述Spring
1.轻量级:Spring是非侵入式的-基于Spring开发的应用中的对象可以不依赖于Spring的API
2.依赖注入/控制反转 DI/IoC
3.面向切面编程AOP aspect oriented programming
4.容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期
5.框架:Spring实现了使用简单的组件配置组合成一个复杂的应用,在Spring中可以使用XML和Java注解组合这些对象
6.一站式:在IoC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了展现层SpringMVC和持久层SpringJDBC)
3.搭建Spring开发环境导入jar包
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.3.jar
spring-aop-4.2.2.RELEASE.jar
spring-beans-4.2.2.RELEASE.jar
spring-context-4.2.2.RELEASE.jar
spring-core-4.2.2.RELEASE.jar
spring-expression-4.2.2.RELEASE.jar
4.DIP、IoC、DI以及IoC容器等名词
1.依赖倒置原则(DIP):一种软件架构设计的原则(抽象概念)。
依赖倒置原则,它转换了依赖,高层模块不依赖于低层模块的实现,而低层模块依赖于高层模块定义的接口。通俗的讲,就是高层模块定义接口,低层模块负责实现。
DIP的优点:
系统更柔韧:可以修改一部分代码而不影响其他模块。
系统更健壮:可以修改一部分代码而不会让系统崩溃。
系统更高效:组件松耦合,且可复用,提高开发效率。
2.控制反转(IoC):一种反转流、依赖和接口的方式(DIP的具体实现方式)。
依赖对象不在被依赖模块的类中直接通过new来获取
3.依赖注入(DI):IoC的一种实现方式,用来反转依赖(IoC的具体实现方式)。
1.构造函数注入
2.属性注入setter方法
3.接口注入
4.IoC容器(例:SpringIoC):依赖注入的框架,用来映射依赖,管理对象创建和生存周期(DI框架)。
示例代码IoC/DI
1 import java.util.Arrays; 2 import java.util.List; 3 import java.util.Map; 4 import java.util.Properties; 5 import java.util.Set; 6 7 public class User { 8 private String name; 9 private int age; 10 private Address address; 11 private List<String> list; 12 private Map<String,Object> map; 13 private Set<String> set; 14 private int [] num; 15 private Properties prop; 16 public String getName() { 17 return name; 18 } 19 public void setName(String name) { 20 this.name = name; 21 } 22 public int getAge() { 23 return age; 24 } 25 public void setAge(int age) { 26 this.age = age; 27 } 28 public Address getAddress() { 29 return address; 30 } 31 public void setAddress(Address address) { 32 this.address = address; 33 } 34 public List<String> getList() { 35 return list; 36 } 37 public void setList(List<String> list) { 38 this.list = list; 39 } 40 public Map<String, Object> getMap() { 41 return map; 42 } 43 public void setMap(Map<String, Object> map) { 44 this.map = map; 45 } 46 public Set<String> getSet() { 47 return set; 48 } 49 public void setSet(Set<String> set) { 50 this.set = set; 51 } 52 public int[] getNum() { 53 return num; 54 } 55 public void setNum(int[] num) { 56 this.num = num; 57 } 58 public Properties getProp() { 59 return prop; 60 } 61 public void setProp(Properties prop) { 62 this.prop = prop; 63 } 64 public User() { 65 super(); 66 // TODO Auto-generated constructor stub 67 } 68 public User(String name, int age, Address address, List<String> list, Map<String, Object> map, Set<String> set, 69 int[] num, Properties prop) { 70 super(); 71 this.name = name; 72 this.age = age; 73 this.address = address; 74 this.list = list; 75 this.map = map; 76 this.set = set; 77 this.num = num; 78 this.prop = prop; 79 } 80 @Override 81 public String toString() { 82 return "User [name=" + name + ", age=" + age + ", address=" + address + ", list=" + list + ", map=" + map 83 + ", set=" + set + ", num=" + Arrays.toString(num) + ", prop=" + prop + "]"; 84 } 85 86 }
1 public class Address { 2 private String city; 3 private String street; 4 public String getCity() { 5 return city; 6 } 7 public void setCity(String city) { 8 this.city = city; 9 } 10 public String getStreet() { 11 return street; 12 } 13 public void setStreet(String street) { 14 this.street = street; 15 } 16 public Address(String city, String street) { 17 super(); 18 this.city = city; 19 this.street = street; 20 } 21 public Address() { 22 super(); 23 // TODO Auto-generated constructor stub 24 } 25 @Override 26 public String toString() { 27 return "Address [city=" + city + ", street=" + street + "]"; 28 } 29 30 }
1 public interface HelloInter { 2 3 public abstract void show(); 4 5 }
1 public class HelloSpring implements HelloInter { 2 private User user; 3 4 public User getUser() { 5 return user; 6 } 7 8 public void setUser(User user) { 9 this.user = user; 10 } 11 12 public void show(){ 13 System.out.println(user); 14 15 } 16 17 public HelloSpring(User user) { 18 super(); 19 this.user = user; 20 } 21 22 public HelloSpring() { 23 super(); 24 System.out.println("HelloSpring"); 25 } 26 27 }
1 import org.springframework.context.ApplicationContext; 2 import org.springframework.context.support.ClassPathXmlApplicationContext; 3 4 /** 5 * 依赖注入DI/控制反转 6 * 注入类型:对象/数组/集合/基本数据类型和字符串/属性类 7 * 注入方式:setter方法/构造器 8 * 实现形式:xml文件 9 * @author Administrator 10 * 11 */ 12 public class Test { 13 14 public static void main(String[] args) { 15 16 ApplicationContext app = new ClassPathXmlApplicationContext( 17 "applicationContext-hello.xml"); 18 19 HelloInter hello = (HelloInter) app.getBean("hello"); 20 21 hello.show(); 22 } 23 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <bean id="hello" class="com.eduask.liusheng.hello.HelloSpring"> 8 <property name="user" ref="user"></property> 9 </bean> 10 <bean id="user" class="com.eduask.liusheng.hello.User"> 11 <property name="name" value="张三"></property> 12 <property name="age" value="18"></property> 13 <property name="address" > 14 <bean class="com.eduask.liusheng.hello.Address"> 15 <property name="city" value="杭州"></property> 16 <property name="street" value="文三路"></property> 17 </bean> 18 </property> 19 <property name="list"> 20 <list> 21 <value>1</value> 22 <value>2</value> 23 <value>3</value> 24 <value>4</value> 25 </list> 26 </property> 27 <property name="set"> 28 <set> 29 <value>a</value> 30 <value>b</value> 31 <value>c</value> 32 <value>d</value> 33 </set> 34 </property> 35 <property name="map"> 36 <map> 37 <entry key="m1" value="一" /> 38 <entry key="m2" value="二" /> 39 </map> 40 </property> 41 <property name="num"> 42 <array> 43 <value>5</value> 44 <value>6</value> 45 <value>7</value> 46 <value>8</value> 47 </array> 48 </property> 49 <property name="prop"> 50 <props> 51 <prop key="username">root</prop> 52 <prop key="password">root</prop> 53 </props> 54 </property> 55 </bean> 56 57 <bean id="address1" class="com.eduask.liusheng.hello.Address"> 58 <constructor-arg name="city" value="杭州"></constructor-arg> 59 <constructor-arg name="street" value="保俶路"></constructor-arg> 60 </bean> 61 </beans>
测试结果:
User [name=张三, age=18,
address=Address [city=杭州, street=文三路],
list=[1, 2, 3, 4],
map={m1=一, m2=二},
set=[a, b, c, d],
num=[5, 6, 7, 8],
prop={password=root, username=root}]