spring

栏目: Java · 发布时间: 7年前

内容简介:控制反转(Inversion ofControl):将一些对象的创建权交给Spring需要的jar包如果底层实现切换,需要修改源码,Spring IOC就是用来在不修改源码的情况下对程序进行扩展.

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--


以上所述就是小编给大家介绍的《spring》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

人工智能+:AI与IA如何重塑未来

人工智能+:AI与IA如何重塑未来

[美]韩德尔·琼斯(Handel Jones) [中]张臣雄 / 机械工业出版社 / 2018-10 / 55.00

当深度学习模型引发了全世界对人工智能的再次关注时,人工智能迎来第三次高速增长,人工智能(AI)、增强现实(AR)和虚拟现实(VR)正把人类带向新的“智能增强时代”(IA),我们将在不知不觉中接纳机器智能。 针对人类社会长期存在的众多复杂的动态的难题,人机融合智能将会提供全新的解决方案,谷歌、Facebook、微软、亚马逊、腾讯、阿里巴巴、百度等平台巨头纷纷斥千亿巨资布局人工智能的尖端技术;智......一起来看看 《人工智能+:AI与IA如何重塑未来》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具