核心容器(Core)

这是Spring框架最基础的部分,它提供了依赖注入(Dependency Injection)特征来实现容器对Bean的管理。这里最基本的概念是BeanFactory,它是任何Spring应用的核心。BeanFactory是工厂模式的一个实现,它使用IoC将应用配置和依赖说明从实际的应用代码中分离出来。
spring的容器中注入各个实例,使得他们在同一个空间,方便。而且是通过xml配置文件new出来的 不依赖类,解耦。
<bean>中给一个scope属性可以控制spring的作用域:

1、singleton单实例

当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。换言之,当把一个bean定义设置为singleton作用域时,Spring IOC容器只会创建该bean定义的唯一实例。这个单一实例会被存储到单例缓存(singleton cache)中,并且所有针对该bean的后续请求和引用都将返回被缓存的对象实例。

2、prototype多实例

每个Spring容器中,一个bean对应多个实例. prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)都会产生一个新的bean实例,相当一个new的操作。

3、request

request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效

4、session

session作用域表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP session内有效.

简单使用Spring框架

1、编写一个普通的Java类(JavaBean)

注意的是:这个类中要用的属性我们都要写好setter-getter函数,这样spring才能封装

package cn.hncu.login;

public class Person {
    private String name;
    private Integer age;

    public Person() {
        System.out.println("执行Person的构造方法...");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

}

2、在Spring配置文件applicationContext.xml。将JavaBean由Spring容器来管理。

<?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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="p" class="cn.hncu.login.Person">
        <property name="name" value="Alice"></property>
        <property name="age" value="25"></property>
    </bean>

</beans>

3、使用Spring容器配置的Bean

注意,在spring2.5版本中是通过XmlBeanFactory来拿beanfactory的。在3.x版本中是通过另一个类:ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");来拿benafactory的,另外2.5版本是不带泛型的,而3.x版本可以带泛型,当然3.x版本也能用2.5版本的方式拿。

package cn.hncu.login;

import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Demo {
    @Test
    public void demo1(){
        XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
        Person p = (Person) factory.getBean("p");
        System.out.println(p);
        Person p2 = (Person) factory.getBean("p");
        System.out.println(p2);
    }
}

至此,一个简单的spring就是这样的了。

使用面向接口编程技术使用spring–3.x版本

1、 Person类 –javabean

package cn.hncu.spring3x.domain;

public class Person {
    private String name;
    private Integer age;

    public Person() {
        System.out.println("执行Person的构造方法...");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

}

2、ILoginDAO–dao接口

package cn.hncu.spring3x.login.dao;

import cn.hncu.spring3x.domain.Person;

public interface ILoginDAO {
    public void login(Person p);
}

3、LoginDAOImpl–dao实现类

package cn.hncu.spring3x.login.dao;

import cn.hncu.spring3x.domain.Person;

public class LoginDAOImpl implements ILoginDAO {

    @Override
    public void login(Person p) {
        System.out.println("dao,到数据库中读取用户信息以进行登录....");     
        System.out.println("dao中获取的用户输入信息:"+p);
    }

}

4、ILoginService –service接口

package cn.hncu.spring3x.login.service;

import cn.hncu.spring3x.domain.Person;

public interface ILoginService {
    public void login(Person p);
}

5、LoginServiceImpl–service实现类

package cn.hncu.spring3x.login.service;

import cn.hncu.spring3x.domain.Person;
import cn.hncu.spring3x.login.dao.ILoginDAO;

public class LoginServiceImpl implements ILoginService{
    private ILoginDAO dao = null;
    public ILoginDAO getDao() {
        return dao;
    }
    public void setDao(ILoginDAO dao) {
        this.dao = dao;
    }

    @Override
    public void login(Person p) {
        dao.login(p);
    }

}

6、LoginAction–Action 相当于servlet

package cn.hncu.spring3x.login;

import cn.hncu.spring3x.domain.Person;
import cn.hncu.spring3x.login.service.ILoginService;

public class LoginAction{
    private ILoginService service = null;
    private Person p = null;
    public ILoginService getService() {
        return service;
    }
    public void setService(ILoginService service) {
        this.service = service;
    }
    public Person getP() {
        return p;
    }
    public void setP(Person p) {
        this.p = p;
    }

    public void execute(){
        service.login(p);
    }
}

7、TestDemo–测试类

package cn.hncu.spring3x;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import cn.hncu.spring3x.domain.Person;
import cn.hncu.spring3x.login.LoginAction;

public class TestDemo {
    @Test
    public void demo1(){
        //用spring3.x版本
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person p = ctx.getBean(Person.class);
        System.out.println(p);
    }
    @Test
    public void demo2(){
        //用spring3.x版本
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        LoginAction action = ctx.getBean(LoginAction.class);
        action.execute();
    }


}

8、applicationContest.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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <bean id="p" class="cn.hncu.spring3x.domain.Person">
        <property name="name" value="WYJ"></property>
        <property name="age" value="22"></property>
    </bean>
    <bean id="dao" class="cn.hncu.spring3x.login.dao.LoginDAOImpl"></bean>
    <bean id="dao2" class="cn.hncu.spring3x.login.dao.LoginDAOImpl2"></bean>
    <bean id="service" class="cn.hncu.spring3x.login.service.LoginServiceImpl">
        <property name="dao" ref="dao2"></property>
    </bean>
    <bean id="login" class="cn.hncu.spring3x.login.LoginAction">
        <property name="service" ref="service"></property>
        <property name="p" ref="p"></property>
    </bean>
</beans>

细节:

我们一定要注意的是,我们要用的类的相关属性一定要写setter-getter函数,然后在xml中配置<property>

Spring IOC 控制反转

IoC(Inversion of Control)中文译为控制反转也可以叫做DI(Dependency Injection,依赖注入)。
控制反转模式的基本概念是:不直接创建对象,但是在xml配置文件中描述创建它们的方式。在工程中使用该Bean时由Spring容器创建Bean的实例。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。
下面我给一个比较复杂的注入来理解IOC

User类

package cn.hncu.spring3x.demo2;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class User {
    private String name;
    private Integer age;
    private List<String> pets;//list集合
    private Map<String, Object> map;//map集合
    private Set<String> set;
    private Object[] objs;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public List<String> getPets() {
        return pets;
    }
    public void setPets(List<String> pets) {
        this.pets = pets;
    }


    public Map<String, Object> getMap() {
        return map;
    }
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }


    public Set<String> getSet() {
        return set;
    }
    public void setSet(Set<String> set) {
        this.set = set;
    }


    public Object[] getObjs() {
        return objs;
    }
    public void setObjs(Object[] objs) {
        this.objs = objs;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + ", pets=" + pets
                + ", map=" + map + ", set=" + set + ", objs="
                + Arrays.toString(objs) + "]";
    }


}

Cat类

package cn.hncu.spring3x.demo2;

public class Cat {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Cat [name=" + name + "]";
    }

}

测试类

package cn.hncu.spring3x.demo2;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo2 {
    /*
     * 演示spring注入的一些细节
     */
    @Test
    public void test(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("app2.xml");
        User user = ctx.getBean(User.class);
        System.out.println(user);
    }
}

这里我们要提的就是这个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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <bean id="tom" class="cn.hncu.spring3x.demo2.Cat">
        <property name="name" value="Tom猫"></property>
    </bean>
    <bean id="user" class="cn.hncu.spring3x.demo2.User">
        <property name="name" value="凡杰"></property>
        <property name="age" value="23"></property>
        <property name="pets">
            <list>
                <value>cat</value>
                <value>dog</value>
                <value>tiger</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="city" value="湖南"></entry>
                <entry key="school" value="湖南城市学院"></entry>
                <entry key="freind" value="小威"></entry>
            </map>
        </property>
        <property name="set">
            <set>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </set>
        </property>
        <property name="objs">
            <array>
                <value>城院教学楼</value>
                <ref bean="tom"/>
                <list>
                    <value>信息院</value>
                    <value>城管院</value>
                </list>
                <bean id="jack" class="cn.hncu.spring3x.demo2.Cat">
                    <property name="name" value="天才宠物猫"></property>
                </bean>

            </array>
        </property>
    </bean>
</beans>

从这个xml中我们可以明显看到如果是复杂类型的数据并且不是类对象的时候我们是打开<property></property>通过在它里面给相关的配置来做到的。