转载

spring

Spring

IOC

控制反转(Inversion ofControl):将一些对象的创建权交给Spring

需要的jar包

//日志接口
com.springsource.org.apache.commons.logging-1.1.1.jar
//日志实现
com.springsource.org.apache.log4j-1.2.15.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar

接口和类

public interface UserDao {
    public void save();
}
public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
        System.out.println("UserDaoImpl...save...");
    }
}

传统方式调用

UserDao userDao = new UserDaoImpl();
	userDao.save();

如果底层实现切换,需要修改源码,Spring IOC就是用来在不修改源码的情况下对程序进行扩展.

IOC实现

配置文件
<?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="userDao" class="com.swifter.demo1.UserDaoImpl"></bean>
</beans>
调用
	ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
   UserDao userDao = (UserDao) classPathXmlApplicationContext.getBean("userDao");
  	userDao.save();

只需要业务代码,配置文件,反射和工厂不需要实现.

IOC和DI

  • IOC: 控制反转
  • ID: 依赖注入,前提是必须有IOC环境,Spring管理这个类的时候将类的依赖属性设置进来

Spring工厂类

  • BeanFactory
  • ApplicationContext 实现类 FileSystemXmlApplicationContext, ClassPathXmlApplicationContext

Bean相关配置

  • id 使用了约束中的唯一约束,不能包含特殊字符
  • name 可以包含特殊字符

Bean声明周期

<bean id="customerDao" class="com.swifter.demo2.CustomerDaoImpl" init-method="init" destroy-method="destroy">

    </bean>
public class CustomerDaoImpl implements CustomerDao {

    public void init() {
        System.out.println("CustomerDaoImpl...init...");
    }

    public void destroy() {
        System.out.println("CustomerDaoImpl...destroy...");
    }

    @Override
    public void save() {
        System.out.println("CustomerDaoImpl...save...");
    }
}

调用

ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");       
	CustomerDao customerDao = (CustomerDao) classPathXmlApplicationContext.getBean("customerDao");
	customerDao.save();
	classPathXmlApplicationContext.close();

作用范围

//scope是singleton会调用destroy方法,但是prototype不会调用, 默认是singleton

属性注入方式

  • 构造方法
  • set方法

构造方法

<bean id="car" class="com.swifter.demo4.Car">
        <constructor-arg name="name" value="QQ"/>
        <constructor-arg name="money" value="20000"/>
    </bean>
public class Car {
    private String name;
    private Double money;

    public Car() {
    }

    public Car(String name, Double money) {
        this.name = name;
        this.money = money;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '/'' +
                ", money=" + money +
                '}';
    }
}

set方法

<bean id="car2" class="com.swifter.demo4.Car">
    <property name="name" value="奔腾"/>
    <property name="money" value="2000"/>
</bean>
public class Car {
    private String name;
    private Double money;

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

    public void setMoney(Double money) {
        this.money = money;
    }

    public Car() {
    }

    public Car(String name, Double money) {
        this.name = name;
        this.money = money;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '/'' +
                ", money=" + money +
                '}';
    }
}

设置对象类型属性

<bean id="employee" class="com.swifter.demo4.Employee">
    <property name="name" value="王五"/>
    <!--value设置普通类型, ref设置对象类型-->
    <property name="car" ref="car"/>
</bean>
public class Employee {
    private String name;
    private Car car;

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

    public void setCar(Car car) {
        this.car = car;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '/'' +
                ", car=" + car +
                '}';
    }
}

p名称空间

  • 普通属性: p:属性名="值"
  • 对象属性: p:属性名-ref="值"
xmlns:p="http://www.springframework.org/schema/p"
 要加上
 <!--p名称空间方式-->
<bean id="car3" class="com.swifter.demo4.Car" p:name="qq" p:money="200" />

--EOF--

原文  https://jingxuetao.com/spring-1/
正文到此结束
Loading...