zoukankan      html  css  js  c++  java
  • Spring 使用xml文件、注解配置Bean

    bean常用的配置方式有2种:

    • xml文件
    • 注解

    使用xml文件配置bean

    <?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 name="user" class="com.chy.bean.User" />
        <bean name="goods" class="com.chy.bean.Goods" />
    </beans>

    <beans>是根元素。一个<bean>配置一个bean:

     <bean name="" class="" scope="" />

    name指定bean的名称,也可以使用id指定。如果要指定多个名称,用逗号或分号隔开即可,比如name="grade,score"。

    scope指定Bean的实例的作用域,可选,缺省时默认为singleton(单例)。

    id和name的区别:

    • id必须唯一,name可以重复
    • id不能含有特殊字符,name可以
    • id只能有一个值,name可以有多个值,逗号或分号隔开即可

    Bean的作用域

    作用域 说明
    singleton(单例)

    默认值。

    该Bean(类)在Spring容器中只有一个实例,无论引用/获取这个Bean多少次,都指向同一个对象。

    singleton适用于无会话状态的Bean(如Dao组建、Service组件)。

    在spring容器启动时就创建实例,且程序运行过程中,这个Bean只创建、使用一个实例。

    由spring容器负责管理生命周期,实例的创建、销毁均由spring容器负责。

    prototype(多例)  

    每次获取该Bean的实例时,都会创建一个新的实例。

    在需要时(从容器中获取该bean的时)才创建该bean的一个实例。

    由spring容器负责创建实例,创建好之后交给调用者,由调用者负责后续处理,spring容器不再管理这个实例。

    request

    web中使用。

    在一次HTTP请求中,获取的是该Bean的同一个实例,该实例只在此次HTTP请求中有效。

    新的HTTP请求,会创建新的实例。

    session

     web中使用。

    在一次HTTP session中获取的是该Bean的同一个实例(一个session中只创建此bean的一个实例),创建实例只在本次HTTP session中有效。

    新的session,会创建新的实例

    globalSession

     在一个全局的HTTP session中,获取到的是该Bean的同一个实例。

    只在使用portlet上下文时有效。

    application

     为每个ServletContext对象创建一个实例。

    只在web相关的ApplicationContext中有效。

    websocket

     为每个websocket对象创建一个实例。

    只在web相关的ApplicationContext中有效。

    Student student1=applicationContext.getBean("student",Student.class);   
    Student student2=applicationContext.getBean("student",Student.class);

    如果Student的作用域是singleton,则student1、student2都指向内存中的同一个Student对象。

    如果Student的作用域是prototype,则student1、student2指向不同的Student对象。


    Spring加载xml配置文件的常用方式有2种

            // 从类盘符加载,写相对路径
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
            
            // 从文件系统加载,写绝对路径,不利于移植,一般都是用上面那种。
            // ApplicationContext applicationContext=new FileSystemXmlApplicationContext("E:\spring\src\spring-config.xml");

    Spring的模块化配置

    spring的模块化配置,也叫spring的模块化开发。

    • 把配置写在一个xml文件中,xml文件会很臃肿,可以拆分为多个xml文件。
    • 有时候需要多人协作,你写一个模块,我写一个模块,你那儿有一个xml配置,我这儿有一个xml配置

    使用时需要同时引入多个xml配置。

    有2种方式:

    • 在xml种使用<import />导入其它xml配置(常用)
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        <import resource="spring-config.xml" />
    </beans>
    • 在程序中使用多个xml配置构建spring容器
    ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config-controller.xml","spring-config-service.xml" );

    参数个数可变。


    使用注解配置Bean

    使用xml进行配置,如果<bean>很多,xml文件会很臃肿。所以通常使用注解来配置bean。

    使用spring的注解,需要引入spring-aop-RELEASE.jar。

    spring常用的类注解:

    • @Service       将业务层(Service层)的类标识为Spring容器中的Bean
    • @Controller   将控制层的类标识为Spring容器中的Bean
    • @Repository    将数据访问层(Dao层)的类标识为Spring容器中的Bean
    • @Component   将一个类标识为Spring容器中的Bean。

    前3个是专用的,@Component是通用的,能用专用的就尽量用专用的。

    这4个注解都是类注解,只能对类使用,只能写在类上面。

    使用示例:

    @Component("dog")
    public class Dog{
        public void shout(){
            System.out.println("汪汪汪");
        }
    }

    小括号中写Bean的name。

    @Component
    public class Dog{
        public void shout(){
            System.out.println("汪汪汪");
        }
    }

    如果缺省name,默认为类名的camel写法。

    需要在xml文件中开启包扫描:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="com.chy.bean" />
    </beans>

    会自动扫描指定的包,如果类上面有这4个注解中的一个,就把这个类放到Spring容器中,由Spring容器管理这个类的实例。

    如果要扫描多个包,比如bean、service、dao,可以使用多个<context:component-scan />,也可以直接扫描父包com.chy。

    使用spring的注解时,需要在xml中启用注解:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
        
        <context:annotation-config />
    </beans>

    因为包扫描<context:component-scan  base-package="" />是先启用注解,再扫描包,所以使用包扫描时不必再写  <context:annotation-config />  。


    使用注解配置scope

    @Component
    @Scope("prototype")
    public class A {
        //......
    }
  • 相关阅读:
    同步、异步、阻塞和非阻塞区别
    SpringMVC文件上传实现
    Java注解入门
    SpringMVC实现一个controller写多个方法
    提高tomcat的并发能力
    人一生追求的是什么
    python class对象转换成json/字典
    python可分组字典
    python enumerate函数用法
    Python特殊语法--filter、map、reduce、lambda
  • 原文地址:https://www.cnblogs.com/chy18883701161/p/11112004.html
Copyright © 2011-2022 走看看