千家信息网

spring IOC

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,Spring概述以下内容仅讲解spring IOC基本使用方法spring IOC: 依赖注入spring AOP:管理组件对象,维护对象关系。目的:降低组件耦合度Spring web MVC:MVC
千家信息网最后更新 2025年02月01日spring IOC

Spring概述

以下内容仅讲解spring IOC基本使用方法


spring IOC: 依赖注入

spring AOP:管理组件对象,维护对象关系。目的:降低组件耦合度

Spring web MVCMVC设计:架构一个MVC结构的WEB程序
Spring
整合其他技术:JDBCMybatisHibernateStruts等。




Spring IOC应用:

以注入的方式应用对象,实现组件解耦

a.管理对象:创建,初始化,释放资源,销毁

b.维护对象关系:采用注入方式建立对象关系 Dependency Injection(DI)依赖注入(依赖注入:set方法注入,构造器注入)

c.搭建SpringIOC开发环境:引入相关jar包;在src中添加ApplicationContext.xml


一.Spring容器->管理组件及对象关系

1.创建ApplicationContext对象

2.向applicationContext.xml配置

3.利用ApplicationContext对象getBean()


实例化ApplicationContext容器对象-> applicationContext.xml



二.Spring创建Bean对象的控制

1.控制对象创建方式(使用范围)

在元素中使用scope属性控制,scope可以支持singleton和prototype,默认值是singleton。

该组件在Spring容器中只有一个bean对象,ac.getBean("id")无论调用getBean();多少次都返回同一个对象;

scope为prototype则每次ac.getBean("id");返回一个新的对象。

2.指定对象初始化方法

利用元素的init-method指定,当创建bean对象后自动执行init-method方法。

3.指定对象销毁方法

利用元素的destroy-method指定。

满足下面条件才有效:

-组件对象为单例模式

-调用AbstractApplicationContext容器对象的close()方法

4.控制单例对象创建时机

在默认情况下,单例对象是Spring容器创建时实例化;可以使用元素的lazy-init=true属性将创建时机推迟到getBean()方法调用时。

public class ExampleBean {    public ExampleBean()    {        System.out.println("--创建ExampleBean--");    }    public void init()    {        System.out.println("init..");    }    public void destroy()    {        System.out.println("destory object...");    }    public void executor(){        System.out.println("调用executor方法");    }}
public class TestBean {    public static void main(String[] args)    {        //创建Spring容器对象        String conf = "applicationContext.xml";        AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf);        //从spring容器获取e1,e2        ExampleBean e1 = ac.getBean("e1", ExampleBean.class);        e1.executor();        ExampleBean e2 = ac.getBean("e1", ExampleBean.class);        System.out.println(e1 == e2);//true        ExampleBean e3 = ac.getBean("e2", ExampleBean.class);        ExampleBean e4 = ac.getBean("e2", ExampleBean.class);        System.out.println(e3 == e4);//false        ac.close();//释放Spring容器对象,自动调用单例的destory-method    }}

IOC: Inversion of Control控制反转

改变了对象获取方式,之前编码方式采用new构造方式获取对象;IOC中采用了由容器创建对象之后注入进来使用。只要修改配置就可以改变对象关系。




三.自动装配(自动注入)

1.自动注入(autowire)

用于指定自动注入规则。可以使用byType,byName,constructor等。用于简化注入配置。

使用byType类型匹配注入需要注意,有2个及其以上匹配会出现异常。

2.各种类型信息的注入配置格式

a).注入字符串,数值单个数值

*b).注入bean对象

                                             

c).注入集合list,set,map,properties

                                                                        tom            jack                                                hongkong            shanghai                                                                                    root            123456            com.mysql.jdbc.Driver            

d).spring表达式注入

#{表达式}

#{id名.属性}或#{id名.key}

如果时对象属性,需要有getXXX方法

#{List对象id[0]}

    小树    静汪    hongkong    shanghai            #{dbParams.user}    #{dbParams.password}    #{dbParams.databases}                                                

db.properties:

=====


3.利用注解配置应用IOC

在JDK5.0时追加一些新特性

注解:在类定义,方法定义,成员变量定义前面使用,格式为@注解标记名。

a).组件自动扫描

可以按指定的包路径,将包下所有组件扫描,如果发现组件类定义前有以下标记,会将组件扫面倒Soring容器。

@Component //其他组件

@Controller //控制层组件

@Service //业务层组件xxxService

@Repository //数据访问层组件xxxDao

@Named(需要引入第三方标准包)


@Scope控制对象创建,默认单例

@PostConstruct指定init-method

@PreDestroy指定destroy-method

b).注入注解

@Resource:可以在变量定义前或setXXX方法前应用

@AutoWired:可以在变量定义前或setXXX方法前应用

一般使用时,功能等价,都可以实现注入。

如果不存在多个匹配类型,使用@Resurce或@Autowired都可以。

如果存在多个匹配类型,建议按名称注入

@Resource(name="指定名称")或

@Autowired

@Qualifier("p")

使用建议:set注入建议用@Resource,构造器建议用@Autowired

自己编写的组件建议使用注解配置;框架API只能用XML配置。



spring配置:

<context:component-scan base-package="springIOC"/>
代码:@Component    //扫描ExampleBean组件,默认id=exampleBean//@Scope("prototype")//等价于@Scope("singleton")//等价于public class ExampleBean {    @PostConstruct //等价于    public void init(){        System.out.println("初始化逻辑");    }    public void execute(){        System.out.println("---执行execute处理方法---");    }    @PreDestroy //scope为singleton并释放容器资源时,等价于    public void destroy(){        System.out.println("释放资源");    }}
@Component("exam1")public class ExampleBean1 {    public void execute(){        System.out.println("---执行exampleBean1的execute---");    }}

test

public class ExampleBeanTest {        public static void main(String[] args)        {            String conf = "applicationContext.xml";            AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf);            ExampleBean e1 = ac.getBean("exampleBean", ExampleBean.class);            e1.execute();            ExampleBean1 e2 = ac.getBean("exam1", ExampleBean1.class);            e2.execute();            ExampleBean ee = ac.getBean("exampleBean", ExampleBean.class);            System.out.println(e1.toString());            System.out.println(ee.toString());            ac.close();        }}

运行结果:

---执行execute处理方法---

---执行exampleBean1的execute---

springIOC.ExampleBean@548a102f

springIOC.ExampleBean@548a102f

释放资源




0