Spring简介
Spring 作者:Rod Johnson;
官方网站:http://spring.io/
最新开发包及文档下载地址:http://repo.springsource.org/libs-release-local/org/springframework/spring/
Spring4 版 Hello World 实现
核心jar包:
HelloWorld.java
package com.java1234.service;
public class HelloWorld {
public void say(){
System.out.println("Spring4大爷你好!");
}
}
Test.java
package com.java1234.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java1234.service.HelloWorld;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
HelloWorld helloWorld=(HelloWorld)ac.getBean("helloWorld");
helloWorld.say();
}
}
Spring IOC详解
IOC(控制反转:Inverse of Control ),又称作 依赖注入,是一种重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的 Spring 框架的核心
Spring IOC实例讲解
src下的beans.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/spring-beans.xsd">
<bean id="zhangsan" class="com.java1234.service.ZhangSan"></bean>
<bean id="lisi" class="com.java1234.service.Lisi"></bean>
<bean id="javaWork" class="com.java1234.service.JavaWork">
<property name="tester" ref="lisi"></property>
</bean>
</beans>
Tester接口 及 接口的两个实现类
package com.java1234.service;
public interface Tester {
public void test();
}
package com.java1234.service;
public class ZhangSan implements Tester{
public void test(){
System.out.println("张三-测试程序");
}
}
package com.java1234.service;
public class Lisi implements Tester{
public void test(){
System.out.println("李四-测试程序");
}
}
JavaWork.java
package com.java1234.service;
public class JavaWork {
private Tester tester;
public void setTester(Tester tester) {
this.tester = tester;
}
public void doTest(){
/*ZhangSan zhangsan=new ZhangSan();
zhangsan.test();*/
tester.test();
}
}
Test
package com.java1234.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java1234.service.JavaWork;
public class Test2 {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
JavaWork javaWork=(JavaWork)ac.getBean("javaWork");
javaWork.doTest();
}
}
装配一个bean
依赖注入
1,属性注入;
2,构造函数注入;(通过类型;通过索引;联合使用)
3,工厂方法注入;(非静态工厂,静态工厂)
4,泛型依赖注入;(Spring4 整合 Hibernate4 的时候再讲)
beans.xml
<bean id="people" class="com.java1234.entity.People"></bean>
<!-- 属性注入 -->
<bean id="people2" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean>
<!-- 构造注入 通过类型-->
<bean id="people3" class="com.java1234.entity.People">
<constructor-arg type="int" value="2"></constructor-arg>
<constructor-arg type="String" value="李四"></constructor-arg>
<constructor-arg type="int" value="22"></constructor-arg>
</bean>
<!-- 构造注入 通过索引-->
<bean id="people4" class="com.java1234.entity.People">
<constructor-arg index="0" value="3"></constructor-arg>
<constructor-arg index="1" value="王五"></constructor-arg>
<constructor-arg index="2" value="55"></constructor-arg>
</bean>
<!-- 构造注入 类型和索引联合使用-->
<bean id="people5" class="com.java1234.entity.People">
<constructor-arg index="0" type="int" value="4"></constructor-arg>
<constructor-arg index="1" type="String" value="招六"></constructor-arg>
<constructor-arg index="2" type="int" value="66"></constructor-arg>
</bean>
<!-- 工厂方法注入 非静态-->
<bean id="peopleFactory" class="com.java1234.factory.PeopleFactory"></bean>
<bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>
<!-- 工厂方法注入 静态-->
<bean id="people8" class="com.java1234.factory.PeopleFactory2" factory-method="createPeople"></bean>
People.java
package com.java1234.entity;
public class People {
private int id;
private String name;
private int age;
public People() {
super();
}
public People(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
/*此处省略 id name age 的get和set方法*/
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
非静态工厂注入用到的类PeopleFactory.java
(静态工厂注入要将createPeople方法设成static)
package com.java1234.factory;
import com.java1234.entity.People;
public class PeopleFactory {
public People createPeople(){
People p=new People();
p.setId(5);
p.setName("小七");
p.setAge(77);
return p;
}
}
注入参数
1,基本类型值;
2,注入 bean;
3,内部 bean;
4,null 值;
5,级联属性;
6,集合类型属性;
导入junit4.4.jar、
beans.xml
<!-- 基本类型值 -->
<bean id="people1" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean>
<!-- 注入bean -->
<bean id="dog1" class="com.java1234.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
<bean id="people2" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog" ref="dog1"></property> <!-- ref指向引入的bean -->
</bean>
<!-- 内部bean -->
<bean id="people3" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog">
<bean class="com.java1234.entity.Dog">
<property name="name" value="Tom"></property>
</bean>
</property>
</bean>
<!-- 注入null值 -->
<bean id="people4" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog">
<null></null>
</property>
</bean>
<!-- 级联属性 此种写法必须在People类中声明dog属性处实例化一个对象 -->
<!-- private Dog dog = new Dog(); -->
<!-- <bean id="people5" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog.name" value="Jack2"></property>
</bean> -->
<!-- 集合类型属性 -->
<bean id="people6" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog" ref="dog1"></property>
<property name="hobbies">
<list>
<value>唱歌</value>
<value>跳舞</value>
</list>
</property>
<property name="loves">
<set>
<value>唱歌2</value>
<value>跳舞2</value>
</set>
</property>
<property name="works">
<map>
<entry>
<key><value>上午</value></key>
<value>写代码</value>
</entry>
<entry>
<key><value>下午</value></key>
<value>测试代码</value>
</entry>
</map>
</property>
<property name="addresses">
<props>
<prop key="address1">aaaaa</prop>
<prop key="address2">bbbbb</prop>
</props>
</property>
</bean>
Dog.java只有private String name;及其get和set方法、
People.java同上一节,新增几个属性及其get和set方法
private Dog dog;
private List<String> hobbies=new ArrayList<String>();
private Set<String> loves=new HashSet<String>();
private Map<String,String> works=new HashMap<String,String>();
private Properties addresses=new Properties();
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age
+ ", dog=" + dog + ", hobbies=" + hobbies + ", loves=" + loves
+ ", works=" + works + ", addresses=" + addresses + "]";
}
测试类
// 基本类型值
@Test
public void test1() {
People people=(People)ac.getBean("people1");
System.out.println(people);
}
/*
注入bean、内部bean、注入null、级联属性、注入集合
同test1方法,将getBean中的参数换成bean.xml中对应的bean的id */
Spring自动装配
通过配置 default-autowire 属性,Spring IOC 容器可以自动为程序注入 bean;默认是 no,不启用自动装配;
default-autowire 的类型有 byName,byType,constructor;
byName:通过名称进行自动匹配;
byType:根据类型进行自动匹配;
constructor:和 byType 类似,只不过它是根据构造方法注入而言的,根据类型,自动注入;
建议:自动装配机制慎用,它屏蔽了装配细节,容易产生潜在的错误;
beans.xml
添加一行 default-autowire="constructor" (可填byName、byType、constructor)
<?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"
default-autowire="constructor">
<bean id="dog2" class="com.java1234.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
<bean id="people1" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean>
</beans>
Dog.java只有private String name;及其get和set方法、
People.java只保留id、name、age、dog、
方法注入(很少用)
Spring bean 作用域默认是 单例 singleton; 可以通过配置 prototype ,实现多例;
方法注入 lookup-method
beans.xml
<bean id="dog" class="com.java1234.entity.Dog" scope="prototype">
<property name="name" value="Jack"></property>
</bean>
<bean id="people1" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<lookup-method name="getDog" bean="dog"/>
</bean>
Dog.java只有private String name;及其get和set方法、
People.java设成抽象类,含有抽象方法getDog()、
测试类
@Test
public void test1() {
People people=(People)ac.getBean("people1");
People people2=(People)ac.getBean("people1");
System.out.println(people.getDog()==people2.getDog());//false
System.out.println(ac.getBean("dog")==ac.getBean("dog"));//false
}
方法替换(很少用)
beans.xml
<bean id="people1" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<replaced-method name="getDog" replacer="people2"></replaced-method>
</bean>
<bean id="people2" class="com.java1234.entity.People2"></bean>
Dog.java只有private String name;及其get和set方法、
People.java
public Dog getDog() {
Dog dog=new Dog();
dog.setName("Jack");
return dog;
}
People2.java
package com.java1234.entity;
import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;
public class People2 implements MethodReplacer {
@Override
public Object reimplement(Object arg0, Method arg1, Object[] arg2)throws Throwable {
Dog dog=new Dog();
dog.setName("Tom");
return dog;
}
}
测试类 输出结果:Tom
@Test
public void test1() {
People people=(People)ac.getBean("people1");
System.out.println(people.getDog().getName());
}
Bean之间的关系:继承 依赖 引用
beans.xml
<bean id="dog" class="com.java1234.entity.Dog">
<property name="name" value="jack"></property>
</bean>
<bean id="abstractPeople" class="com.java1234.entity.People" abstract="true">
<property name="className" value="高三5班"></property>
<property name="age" value="19"></property>
</bean>
<!-- parent="abstractPeople" 继承id为abstractPeople的bean -->
<!-- depends-on="autority" 依赖id为autority的bean -->
<bean id="zhangsan" parent="abstractPeople" depends-on="autority">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="lisi" parent="abstractPeople">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<!-- 重写父bean中的age属性 -->
<property name="age" value="20"></property>
<!-- ref="dog" 引用id为dog的bean-->
<property name="dog" ref="dog"></property>
</bean>
<bean id="autority" class="com.java1234.service.Authority"></bean>
Dog.java只有private String name;及其get和set方法、
People.java中 id、name、age、className、dog、以上五个属性的set和get、
//无参构造方法
public People() {
System.out.println("初始化People");
}
//重写toString方法
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age
+ ", className=" + className + ", dog=" + dog + "]";
}
Authority类(讲解bean的依赖关系,先加载Authority类,再初始化People)、
package com.java1234.service;
public class Authority {
public Authority() {
System.out.println("获取权限");
}
}
测试类
@Test
public void test1() {
People zhangsan=(People)ac.getBean("zhangsan");
System.out.println(zhangsan);
People lisi=(People)ac.getBean("lisi");
System.out.println(lisi);
}
bean的作用范围
1,singleton Spring ioc 容器中仅有一个 Bean 实例,Bean 以单例的方式存在;
2,prototype 每次从容器中调用 Bean 时,都返回一个新的实例;
3,request 每次 HTTP 请求都会创建一个新的 Bean;
4,session 同一个 HTTP Session 共享一个 Bean;
5,global session 同一个全局 Session 共享一个 Bean,一般用于 Portlet 应用环境;
6,application 同一个 Application 共享一个 Bean;
beans.xml
<bean id="dog" class="com.java1234.entity.Dog" scope="prototype">
<property name="name" value="jack"></property>
</bean>
Dog.java只有private String name;及其get和set方法、
测试类
@Test
public void test1() {
Dog dog=(Dog)ac.getBean("dog");
Dog dog2=(Dog)ac.getBean("dog");
System.out.println(dog==dog2);
}