Spring是什么
Spring是Rod Johnson创建的一个开源框架。使用Sprint,可以用简单的JavaBeans来实现那些以前在EJB才能实现的功能。
官网地址:http://spring.io/
术语
- Bean 传统的JavaBeans
- EJB Enterprise JavaBeans
- POJO Plain Old Java Object
Spring归纳为几个基本部件。Spring是一个轻量级的DI和AOP容器框架。其特性:
- 轻量级
- 依赖注入
- 面向切面
- 容器
- 框架
Spring模块
Spring由7个模块组成,所有模块都是建立在核心容器上。容器规定如何创建、配置和管理Bean,以及很多Spring的具体细节。当你配置应用的时候,就隐式地使用了这些类。
核心容器
在核心容器里可以找到BeanFactory这个类,它是最基本的Spring容器和Spring的DI所依赖的基础。
开始Spring之旅
示例1:使用MyEclipse创建Spring项目(set注入方式)
程序结构 :
MyTest1.java
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.bean.Person;
public class MyTest1 {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
Person p = ctx.getBean("person",Person.class); //创建bean的引用对象
p.info();
}
}
Person.java
package com.test.bean;
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;
}
public void info(){
System.out.println("My name is:"+getName()+" ; Age:"+getAge());
}
}
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="person" class="com.test.bean.Person">
<property name="name" value="xingoo"/>
<property name="age" value="12"/>
</bean>
</beans>
运行效果:
示例2:创建
依赖注入(DI)是Spring所做的最基本的事情。
也可以使用maven创建一个Eclipse项目
mvn archetype:create DgroupId=com.xundh -DartifactId=test DpackageName=com.xundh.test eclipse:eclipse
引入Spring
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
项目结构:
GreetingService.java
package com.xundh.test;
public interface GreetingService{
void sayGreeting();
}
GreetingServiceImpl.java
package com.xundh.test;
public class GreetingServiceImpl implements GreetingService{
private String greeting;
public GreetingServiceImpl(){}
public GreetingServiceImpl(String greeting){
this.greeting=greeting;
}
public void sayGreeting(){
System.out.println(greeting);
}
public void setGreeting(String greeting){
this.greeting=greeting;
}
}
HelloApp.java
package com.xundh.hello;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import com.xundh.test.GreetingService;
public class HelloApp {
public static void main(String[] args)throws Exception{
BeanFactory factory=
new XmlBeanFactory(new FileSystemResource("hello.xml"));
GreetingService greetingService =
(GreetingService)factory.getBean("greetingService");
greetingService.sayGreeting();
}
}
hello.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/springbeans-2.0.xsd">
<bean id="greetingService"
class="com.xundh.test.GreetingServiceImpl">
<property name="greeting" value="Buenos Dias!" />
</bean>
</beans>
是这个XML文件的根元素,它也是任何 Spring配置文件的根元素。元素用来在Spring容器中定义一个类以及它的配置信息。属性id是greetingService Bean的名字,class属性表示Bean的全路径类名。
是表示设置属性值。
也可以让Spring通过GreetingServiceImpl的单参数构造方法来设置greeting属性的值,例如:
<?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-3.1.xsd">
<bean id="greetingService"
class="com.xundh.test.GreetingServiceImpl">
<constructor-arg value="Hello World!" />
</bean>
</beans>
运行结果是一样的。