spring入门(一)

来源:互联网 发布:郑爽接拍网络电影征途 编辑:程序博客网 时间:2024/06/06 13:35

spring 核心功能之 IOC

IoC – Inverse of Control,控制反转,将对象的创建权反转给Spring

步骤一:创建JavaWEB项目,引入Spring的开发包

步骤二: 编写基本结构

  • com.wzy.demo1
    * UserService – 接口
    * UserServiceImpl – 具体的实现类

步骤三:把UserServiceImpl实现类的创建交给Spring框架来管理,需要创建Spring框架的配置文件.

    * 在src目录下创建applicationContext.xml的配置文件,名称是可以任意的,但是一般都会使用默认名称    * 引入spring的约束,需要先找到具体的约束头信息!!        * 具体的约束如下:      
<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"></beans>
    * 完成UserService的配置        <!-- Spring的快速入门 -->
<bean id="userService" class="com.wzy.demo1.UserServiceImpl"/>

步骤四:编写测试程序,采用Spring框架的工厂方式来获取到UserService接口的具体实现类!!

UserService接口:

public interface UserService { public void save(); }

UserServiceImp实现类:

public class UserServiceImp implements UserService {    public void save() {        System.out.println("UserServiceImp已保存");    }}

编写测试类:

public class Demo {@Testpublic void test() {ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = (UserService) applicationContext.getBean("userService");userService.save();}}

Spring框架中 < bean> 标签的配置

1. id属性和name属性的区别

    * id        -- Bean起个名字,在约束中采用ID的约束,唯一        * 取值要求:必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号  id:不能出现特殊字符    * name      -- Bean起个名字,没有采用ID的约束(了解)        * 取值要求:name:出现特殊字符.如果<bean>没有id的话 , name可以当做id使用        * Spring框架在整合Struts1的框架的时候,Struts1的框架的访问路径是以/开头的,例如:/bookAction

2. class属性 – Bean对象的全路径

3. scope属性 – scope属性代表Bean的作用范围

    * singleton         -- 单例(默认值)    * prototype         -- 多例,在Spring框架整合Struts2框架的时候,Action类也需要交给Spring做管理,配置把Action类配置成多例!!    * request           -- 应用在Web项目中,每次HTTP请求都会创建一个新的Bean    * session           -- 应用在Web项目中,同一个HTTP Session 共享一个Bean    * globalsession     -- 应用在Web项目中,多服务器间的session

4. Bean对象的创建和销毁的两个属性配置(了解)

    * 说明:Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法    * init-method       -- 当bean被载入到容器的时候调用init-method属性指定的方法    * destroy-method    -- 当bean从容器中删除的时候调用destroy-method属性指定的方法        * 想查看destroy-method的效果,有如下条件            * scope= singleton有效            * web容器中会自动调用,但是main函数或测试用例需要手动调用(需要使用ClassPathXmlApplicationContext的close()方法)
@Test    public void run5(){        // 创建工厂,加载核心配置文件(实现类)        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");        // 从工厂中获取到对象        UserServiceImpl usi = (UserServiceImpl) ac.getBean("userService");        // 调用对象的方法执行        usi.sayHello();        // 关闭工厂,工厂关闭了,对象都会销毁        ac.close();    }





spring 核心功能之依赖注入(DI)

IOC和DI的概念

    * IOC       -- Inverse of Control,控制反转,将对象的创建权反转给Spring    * DI        -- Dependency Injection,依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中

DI(依赖注入)

    * 例如:如果UserServiceImpl的实现类中有一个属性,那么使用Spring框架的IOC功能时,可以通过依赖注入把该属性的值传入进来!!    * 具体的配置如下

ApplicationContext.xml配置

<bean id="userService" class="com.wzy.demo1.UserServiceImpl">                <property name="uname" value="老王"/>            </bean>

ApplicationContext.xml配置

<!-- 演示的依赖注入 -->    <bean id="customerDao" class="com.wzy.demo3.CustomerDaoImpl"/>    <bean id="customerService" class="com.wzy.demo3.CustomerServiceImpl">        <property name="customerDao" ref="customerDao"/>    </bean>

CustomerDaoImpl类代码:

public class CustomerDaoImpl {    public void save(){        System.out.println("持久层dao....");    }}

CustomerServiceImpl类代码:

public class CustomerServiceImpl {    // 提供成员属性,提供set方法    private CustomerDaoImpl customerDao;    public void setCustomerDao(CustomerDaoImpl customerDao) {        this.customerDao = customerDao;    }    public void save(){        System.out.println("业务层service....");        // 原来编写方式        // new CustomerDaoImpl().save();        // Spring的方式        customerDao.save();    }}

测试类:

@Test    public void run2(){        // 创建工厂,加载配置文件,CustomerDaoImpl创建了,CustomerServiceImpl被创建了,        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");        CustomerServiceImpl cs = (CustomerServiceImpl) ac.getBean("customerService");        cs.save();    }

结果会运行CustomerServiceImpl和CustomerDaoImpl里的save方法


构造方法的注入方式:

两步

1.编写Java的类,提供构造方法

    public class Car {                    private String name;                    private double money;                    public Car(String name, double money) {                        this.name = name;                        this.money = money;                    }                    @Override                    public String toString() {                        return "Car [name=" + name + ", money=" + money + "]";                    }                }

2.编写配置文件:

                <bean id="car" class="com.wzy.demo4.Car">                    <constructor-arg name="name" value="宝马"/>                    <constructor-arg name="money" value="500000"/>                </bean>

如果Java类的属性是另一个Java的类,那么需要怎么来注入值呢?
< property name=”name” rel=”具体的Bean的ID或者name的值”/>

例如:

                <bean id="car1" class="com.wzy.demo4.Car1">                    <constructor-arg index="0" value="宝马"/>                    <constructor-arg index="1" value="500000"/>                </bean><bean id="person" class="com.wzy.demo4.Person">                    <constructor-arg name="pname" value="美美"/>                    <constructor-arg name="car1" ref="car1"/>                </bean></beans>

person类的代码:

public class Person {    private String pname;    private Car1 car1;    public Person(String pname, Car1 car1) {        this.pname = pname;        this.car1 = car1;    }    @Override    public String toString() {        return "Person [pname=" + pname + ", car1=" + car1 + "]";    }}

car1代码:

public class Car1 {    private String cname;    private Double price;    public Car1(String cname, Double price) {        this.cname = cname;        this.price = price;    }    @Override    public String toString() {        return "Car1 [cname=" + cname + ", price=" + price + "]";    }}







集合(List,Set,Map),Properties等的注入

数组或者List集合注入配置文件的方式是一样的

        <bean id="user" class="com.wzy.demo4.User">        <property name="arrs">            <list>                <value>哈哈</value>                <value>呵呵</value>                <value>嘿嘿</value>            </list>        </property>        </bean>

Set集合注入的配置文件方式:

        <property name="list">            <set>                <value>哈哈</value>                <value>呵呵</value>            </set>        </property>

Map集合注入的配置方式:

<property name="map">            <map>                <entry key="aaa" value="小苍"/>                <entry key="bbb" value="小泽"/>            </map>        </property>

properties属性文件的方式:

        <property name="pro">            <props>                <prop key="username">root</prop>                <prop key="password">1234</prop>            </props>        </property>

User类代码:

public class User {    private String [] arrs;    public void setArrs(String[] arrs) {        this.arrs = arrs;    }    private List<String> list;    public void setList(List<String> list) {        this.list = list;    }    private Map<String, String> map;    public void setMap(Map<String, String> map) {        this.map = map;    }    private Properties pro;    public void setPro(Properties pro) {        this.pro = pro;    }    @Override    public String toString() {        return "User [arrs=" + Arrays.toString(arrs) + ", list=" + list + ", map=" + map + ", pro=" + pro + "]";    }}

引入其他的配置文件

    <import resource="applicationContext2.xml"/>
原创粉丝点击