Spring学习笔记-使用-1

bean:

1、bean的定义

<bean id = "car" class="com.robin.spring.beans.Car">
        <property name="brand" value="AODI"></property>
        <constructor-arg name="price" value="120" ></constructor-arg>
        <constructor-arg name="name">
            <value>A6</value>
        </constructor-arg>
    </bean>
<bean id="robin" class="com.robin.spring.beans.Person">
        <property name="name" value="Robin"></property>
        <property name="car" ref="car"></property>
    </bean>

id:bean在spring中管理时的id,在按名称获取bean时需要使用。

ref:引用其他bean

给bean里的属性赋值的两方法:

(1)使用setter(用property,注意这个地方的name不是属性名,而是setter方法的名字)(调用无参构造器创建对象)

(2)使用构造器(用constructor-arg)

2、bean的使用

ApplicationContext cnt = new ClassPathXmlApplicationContext("beans-spel.xml");
//按类型获取,要求唯一
Car car = cnt.getBean(Car.class);
System.out.println(car.toString());
//按名称获取
Person robin = (Person)cnt.getBean("robin");
System.out.println(robin.toString());

ApplicationContext是IOC容器,用于存放bean,在创建IOC容器时,就会创建所有bean。使用的时候,只是从IOC容器里获取bean的引用而已。

3、内部bean:

在bean的内部还可以定义bean,但其作用域仅限于当前bean

<bean id="robin3" class="com.robin.spring.beans.Person">
        <property name="name" value="robin"></property>
        <property name="age" value="26"></property>
        <property name="car">
            <bean id="myCar" class="com.robin.spring.beans.Car">
                <property name="brand" value="BMW"></property>
                <property name="price" value="1300000"></property>
                <property name="maxSpeed" value="250"></property>
            </bean>
        </property>
        <property name="car.brand" value="RBMW"></property>
        <!--给List和Map等类型的属性赋值-->
        <property name="cars">
            <list>
                <ref bean="car"></ref>
                <ref bean="car2"></ref>
                <ref bean="car4"></ref>
            </list>
        </property>
    </bean>

4、bean之间的关系:

继承关系(使用parent关键字),子bean会继承父bean的所有属性值,子bean可以重写继承的属性值。

<bean id="addr" class="com.robin.spring.autowire.Address" abstract="true">
        <property name="city" value="BeiJing"></property>
        <property name="street" value="WuDaoKou"></property>
    </bean>
    <bean id="addr2" class="com.robin.spring.autowire.Address" parent="addr" scope="prototype">
        <!--<property name="city" value="BeiJing"></property>-->
        <property name="street" value="HuangZhuang"></property>
    </bean>

abstract:默认为false,表示是否是模板bean,模板bean不能被实例化,也就是说不会创建模板bean的对象。

scope:控制对象的生成方式(prototype:原型模式,每次获取bean都会单独创建一个对象。singleton:单例模式,只创建一个bean,每次获取的都是同一个bean的引用)

5、自动装配

<bean id="car" class="com.robin.spring.autowire.Car">
        <property name="brand" value="AODI"></property>
        <property name="price" value="120"></property>
    </bean>
    <bean id="addr" class="com.robin.spring.autowire.Address">
        <property name="city" value="BeiJing"></property>
        <property name="street" value="WuDaoKou"></property>
    </bean>
    <bean id="robin" class="com.robin.spring.autowire.Person" autowire="byType">
        <property name="name" value="robin"></property>
    </bean>

autowire:自动装配的关键字,可选择byType(按类型)和byName(按名称)

自动装配是指,类中的某一属性里是对象时,在创建该类的bean时,会自动引用该属性对应的bean

6、使用外部属性文件

1、需要引入命名空间context,并在配置文件中使用context:property-placeholder来制定属性文件的位置

2、使用${ }符号来提取属性文件中的值

<context:property-placeholder location="classpath:Car.properties"></context:property-placeholder>
    <bean id="carProp" class="com.robin.spring.autowire.Car">
        <property name="brand" value="${brand}"></property>
        <property name="price" value="${price}"></property>
    </bean>

7、bean的生命周期

(1)通过构造器或者工厂方法创建bean实例

(2)为bean的属性进行赋值

(3)调用bean后置处理器的postProcessorBeforeInitialization方法

(4)初始化方法(通过init-method关键字配置)

(5)调用bean后置处理器的postProcessorAfterInitialization方法

(6)使用bean

(7)IOC容器关闭时,销毁bean(通过destroy-method关键字配置)

注意:如果要使用bean的后置处理器,需要实现BeanPostProcessor接口。

8、工厂方法创建bean

1、需要先创建工厂类和工厂方法。(静态工厂方法、实例工厂方法)

静态工厂方法:工厂类里包含一个静态属性,通过静态工厂方法获取产品。

        配置时不需为工厂方法创建bean,只需将class指定为工厂类的类名,factory-method需要指明工厂方法,系统会自动识别并获取产品,为产品创建一个bean。

实例工厂方法:没有静态属性,要获取产品需要先创建工厂实例,然后调用工厂方法获取产品。

        配置时需要先创建工厂类的bean,然后创建产品(不需制定class)bean,factory-bean指明工厂bean,factory-method需要指明工厂方法。

<bean id="car1" class="com.robin.spring.factory.StaticFactory"
        factory-method="getCar">
        <constructor-arg name="brand" value="AODI"></constructor-arg>
    </bean>

    <bean id="factory" class="com.robin.spring.factory.InstanceFactory"></bean>

    <bean id="car2" factory-bean="factory" factory-method="getCar">
        <constructor-arg value="BMW"></constructor-arg>
    </bean>

9、FactoryBean的方法来创建bean

需要先实现FactoryBean接口,然后配置的时候直接创建产品的bean,但是class需要指向FactoryBean的实现类,系统会自动识别并调用FactoryBean的getObject方法,返回产品bean。(注意,这里的属性配置的是FactoryBean的属性,并不是产品的属性)

<bean id="car" class="com.robin.spring.factorybean.CarFactoryBean">
        <!--这里的属性是CarFactoryBean里的,并不是Car里的-->
        <property name="brand" value="BMW"></property>
    </bean>

SpEL:

1、运行是动态给bean赋值的表达式语言。使用#{ }作为界定符。

2、可以进行以下一些操作:赋值(字面值、对象、对象的属性)、各种运算表达式、调用静态方法、使用静态属性(使用T())

<bean id="car" class="com.robin.spring.spel.Car">
        <property name="brand" value="#{'AODI'}"></property>
        <property name="price" value="#{120}"></property>
        <property name="tyreParameter" value="#{T(com.robin.spring.spel.Car).calPar(T(com.robin.spring.spel.Car).PI)}"></property>
    </bean>

    <bean id="robin" class="com.robin.spring.spel.Person">
        <property name="name" value="#{'robin'}"></property>
        <property name="city" value="#{car.city}"></property>
        <property name="car" value="#{car}"></property>
        <property name="info" value="#{car.price > 100 ? 'Good' : 'normal'}"></property>
    </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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> 
 
</beans>

猜你喜欢

转载自blog.csdn.net/Na2Co3_Ren/article/details/88952756
今日推荐