Spring复习1—— spring框架的概述以及spring中基于XML的I0C配置

本文介绍了Spring框架的基本概念,如控制反转和面向切面编程,并通过一个简单的DAO、Service层实例展示了Spring的IOC功能。通过在Maven工程中引入Spring-context依赖,创建bean.xml配置文件,管理对象的生命周期,演示了如何降低组件间的耦合。此外,还讨论了bean的生命周期和不同方式的bean创建。
摘要由CSDN通过智能技术生成

 

一,spring的概述


1,spring是什么?

 

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。

2,spring的两大核心,

 

控制反转--Spring通过一种称作控制反转(IOC)的技术促进了低耦合,对象不是直接new出来,而是交给第三方创建,从而降低类之间的依赖关系

面向切面--Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的--完成业务逻辑--仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。

 

3,入门案例

 

创建一个maven工程

(1)在pomxml中导入spring的拓展

<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
</dependencies>

(2)简单建立一个例子,包括dao层的接口和 实现类,service层的接口和实现类,ui层的测试调用类,来测试spring的IOC功能,spring创建实体类对象降低程序间的耦合关系

DAO层:IAccount接口

package com.itcast.dao;

import com.itcast.dao.domin.Account;

import java.util.List;

/**学生的持久层接口*/
public interface IAccount {
    List<Account> findAll();
    List<Account> findAccount();


}

DAO层:IAccount的实现类AcountDao

package com.itcast.dao.impl;

import com.itcast.dao.IAccount;
import com.itcast.dao.domin.Account;

import java.util.List;

public class AccountDao implements IAccount {
    @Override
    public List<Account> findAll() {
        return null;
    }

    @Override
    public List<Account> findAccount() {
        System.out.println("查询所有用户");
        return null;
    }
}

Service层:IAccountService接口

package com.itcast.service;

public interface IAccountService {
    public void findAccount();
}

Service层:IAccountService实现类AccountService

package com.itcast.service.impl;

import com.itcast.dao.IAccount;
import com.itcast.service.IAccountService;

public class AccountService implements IAccountService {
    private IAccount accountDao;
    @Override
    public void findAccount() {
        accountDao.findAccount();
    }
}

UI层的Client测试类

package com.itcast.ui;

import com.itcast.dao.IAccount;
import com.itcast.service.IAccountService;
import com.itcast.service.impl.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    /**
     * 核心容器的两个接口引发出的问题:
     * ApplicationContext:    单例对象使适用,只创建一次对象
     * 它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一 读取完配置文件 马上就创建配置文件中配置的对象。
     * BeanFactory            多例对象使适用,多次对象
     * 它在构建刻心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。
     * @param args
     */
    public static void main(String[] args) {
        //创建容器bean.xml的对象
        ApplicationContext ac= new ClassPathXmlApplicationContext("bean.xml");
        //根据容器对象和bean标签来创建对应的类,实现工厂模式
        IAccountService accountService = (IAccountService)ac.getBean("accountService");
        //IAccount iAccountDao = (IAccount) ac.getBean("accountDao",IAccount.class);
        System.out.println(accountService);
        //System.out.println(iAccountDao);
    }
}

实现创建实体类的xml文件,bean.xml

<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-3.0.xsd">
<!--把对象的创建交给spring来管理,根据id可以获取到对应的类-->
    <!--第二种万式:使用工厂中的静态万法创建对冢(使用某个类中的静态万法创建对家,开仔人spring谷器)
<bean id=" accountService" class="com. itheima. factory. StaticFactory" factory- method=" getAccountServe> </bean>
bean的作用范围调整
bean标签的scope属性:
作用:用于指定bean的作用范围
取值:
常用的就是单例的和多例的
singleton:单例的(默认值)
prototype:多例的
request:
作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
-->

<!--一,常见bean的三种方式
    1,第一种方式:使用默认构造函数创建。
    在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
    采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
    <bean id="accountService" class="com.itcast.service.impl.AccountService"></bean>

    2,使用FactoryInJar类中的getAccountDao方法创建对象,并注入spring容器
     <bean id="factoryInJar" class="com.itcast.dao.domin.FactoryInJar"></bean>
    <bean id="accountDao" factory-bean="factoryInJar" factory-method="getAccountDao"></bean>

    3,使用FactoryInJar类中的静态方法创建对象,并注入spring容器
    <bean id="factoryInJar" class="com.itcast.dao.domin.FactoryInJar" factory-method="getAccountDao1"></bean>

    -->
    <bean id="accountService" class="com.itcast.service.impl.AccountService"></bean>
<!--    <bean id="factoryInJar" class="com.itcast.dao.domin.FactoryInJar"></bean>-->
<!--    <bean id="accountDao" factory-bean="factoryInJar" factory-method="getAccountDao"></bean>-->

    <bean id="factoryInJar" class="com.itcast.dao.domin.FactoryInJar" factory-method="getAccountDao1"></bean>



</beans>

补充:

一,常见创建bean的三种方式
    1,第一种方式:使用默认构造函数创建。
    在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
    采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
    <bean id="accountService" class="com.itcast.service.impl.AccountService"></bean>

    2,使用FactoryInJar类中的getAccountDao方法创建对象,并注入spring容器
     <bean id="factoryInJar" class="com.itcast.dao.domin.FactoryInJar"></bean>
    <bean id="accountDao" factory-bean="factoryInJar" factory-method="getAccountDao"></bean>

    3,使用FactoryInJar类中的静态方法创建对象,并注入spring容器
    <bean id="factoryInJar" class="com.itcast.dao.domin.FactoryInJar" factory-method="getAccountDao1"></bean>

 二,bean的生命周期
    1,选择对象scope的取值为singleton:单例的(默认值)  prototype:多例的
    2,单例对象,容器创建的时候对象创建,容器销毁的时候对象销毁
    3,多例对象,spring为我们创建,调用的时候创建,使用对象的过程中一直活着,当对象长时间不用时,垃圾回收
三,创建对象accountService,给属性变量赋值
1,常用属性
<bean id="accountService" class="com.itcast.service.impl.AccountService">
    <property name="name" value="建江"></property>
    <property name="age" value="18"></property>
    <property name="birthday" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"></bean>

   2,集合属性赋值

<bean id="accountService" class="com.itcast.service.impl.AccountService">
    <property name="myStr">
        <array>
            <value>AAA</value>
            <value>BBB</value>
            <value>CCC</value>
        </array>
    </property>
    <property name="myList">
        <list>
            <value>aaa</value>
            <value>bbb</value>
        </list>
    </property>
    <property name="mySet">
        <set>
            <value>ccc</value>
            <value>ddd</value>
        </set>
    </property>
    <property name="myMap">
        <map>
            <entry key="testA" value="AAA"></entry>
            <entry key="testB">
                <value>BBB</value>
            </entry>
        </map>
    </property>
    <property name="myProperties">
        <props>
            <prop key="testC">ccc</prop>
        </props>
    </property>
</bean>

 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值