概述:
①Spring是一个开源框架
②Spring为简化企业级开发而生,使用Spring,JavaBean就可以实现很多以前要靠EJB才能实现的功能。同样的功能,在EJB中要通过繁琐的配置和复杂的代码才能够实现,而在Spring中却非常的优雅和简洁。
③Spring是一个IOC(DI)和AOP容器框架。
④Spring的优良特性
[1]非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API
[2]依赖注入:DI——Dependency Injection,反转控制(IOC)最经典的实现。
[3]面向切面编程:Aspect Oriented Programming——AOP
[4]容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期
[5]组件化:Spring实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用XML和Java注解组合这些对象。
[6]一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring 自身也提供了表述层的SpringMVC和持久层的Spring JDBC)。
运行环境的搭建:
引入jar包:
spring-framework-4.0.0.RELEASElibs目录下
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
简单例子:
package com.soyoungboy.bean; public class Person { private String lastName; private String firstName; private Integer age; private String gender; public Person() { super(); } public Person(String lastName, String firstName, Integer age, String gender) { super(); this.lastName = lastName; this.firstName = firstName; this.age = age; this.gender = gender; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Person [lastName=" + lastName + ", firstName=" + firstName + ", age=" + age + ", gender=" + gender + "]"; } }
person.xml文件配置:
<?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.xsd"> <bean id="person" class="com.soyoungboy.bean.Person"> <property name="lastName" value="wang"></property> <property name="firstName" value="fubin"></property> <property name="age" value="18"></property> <property name="gender" value="man"></property> </bean> <!-- p标签 --> <bean id="person04" class="com.soyoungboy.bean.Person" p:age="18" p:lastName="Li" p:firstName="qiongqiong" p:gender="woman" > </bean> <!-- 通过构造器为bean的属性赋值 --> <bean id="person05" class="com.soyoungboy.bean.Person"> <constructor-arg name="firstName" value="heng"></constructor-arg> <constructor-arg name="lastName" value="luo"></constructor-arg> <constructor-arg name="age" value="18"></constructor-arg> <constructor-arg name="gender" value="man"></constructor-arg> </bean> </beans>
测试类:
package com.soyoungboy.test; import static org.junit.Assert.*; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.soyoungboy.bean.Person; public class PersonTest { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("person.xml"); @Test public void test() { Person bean = (Person) applicationContext.getBean("person"); System.out.println(bean); } @Test public void test4() { Person person04 = (Person) applicationContext.getBean("person04"); System.out.println(person04); } /** * 通过构造器获取对象内容 */ @Test public void test5() { Person person05 = (Person) applicationContext.getBean("person05"); System.out.println(person05); } }
运行结果分别是:
Person [lastName=wang, firstName=fubin, age=18, gender=man]
Person [lastName=Li, firstName=qiongqiong, age=18, gender=woman]
Person [lastName=luo, firstName=heng, age=18, gender=man]