[Spring 02] Spring 常用属性注入方式

    xiaoxiao2025-05-15  54

    1.XML的提示配置

    以beans的约束配置为例:

    复制Scheme的路径:http://www.springframework.org/schema/beans/spring-beans.xsd进入Eclipse - -> Window --> preference --> XML Catalog界面 点击Add按钮,引入本地约束文件(Spring解压包下) 点击OK完成在XML文件中有关Bean的提示设置。

    2.Bean的相关属性配置

    2.1 标签的id和name配置

    bean标签的 id 和 name 属性都能够标识这个标签,但是两者在使用上存在着差别:

    id :使用了约束中的的唯一约束,里面不能出现特殊字符name :未使用约束中的的唯一约束(理论上可以重复,但实际开发不允许重复),里面可以出现特殊字符

    2.2 bean的生命周期配置

    init-method :Bean被初始化的时候执行的方法destroy-method :Bean被销毁的时候执行的方法(Bean是单例创建,工厂关闭)

    2.3 Bean的作用范围的配置(重点)

    scope :Bean的作用范围属性 singleton :默认的,Spring会采用单例模式创建这个对象;prototype :多例模式创建对象(Struts2 和 spring整合时会用到)request :应用在Web项目中,Spring创建这个类以后,将这个类存入request范围中;session :应用在Web项目中,Spring创建这个类以后,将这个类存入session范围中;globalsession :应用在Web项目中,必须在 porlet 环境下使用,如果没有 porlet 环境配置 globalsession相当于session;

    2.4 Spring 的属性注入(DI)

    下图介绍了传统的三种方式为类中的属性注入值,Spring底层采用构造方法和Set()方法的方式为属性注入值:

    构造方法的方式属性注入 xml文件信息: <!-- Spring属性注入 --> <!-- 1.构造方法方式 --> <bean id="car" class="com.ahoo.spring.demo2.Car"> <constructor-arg name="name" value="mini Cooper" /> <constructor-arg name="price" value="480000" /> </bean> 测试类: @Test /** * 构造方法方式的属性注入 */ public void demo1() { //创建Spring工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Car car = (Car) applicationContext.getBean("car"); System.out.println(car); } set()方法的方式属性注入 <!-- 2.set方法方式 --> <bean id="car2" class="com.ahoo.spring.demo2.Car2"> <property name="name" value="奔驰" /> <property name="price" value="1000000" /> </bean> <!-- 2.set方法方式注入对象的属性 --> <bean id="emploee" class="com.ahoo.spring.demo2.Emploee"> <property name="name" value="Ahoo" /> <property name="car" ref="car" /> </bean> 测试类: @Test /** * set方法方式的属性注入 */ public void demo2() { //创建Spring工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Car2 car2 = (Car2) applicationContext.getBean("car2"); System.out.println(car2); } @Test /** * set方法方式的属性注入引用类型的属性 */ public void demo3() { //创建Spring工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Emploee emploee = (Emploee) applicationContext.getBean("emploee"); System.out.println(emploee); }

    2.5 P 名称空间完成属性注入

    在Spring2.5版本以后通过引入p 名称空间完成属性的注入写法: 普通属性:p:属性名="属性值"对象属性:p:属性名-ref="属性值" 1.引入p名称空间 xmlns:p="http://www.springframework.org/schema/p" 2.属性注入 <bean id="car2" class="com.ahoo.spring.demo2.Car2" p:name="奇瑞QQ" p:price="50000"></bean> <bean id="emploee" class="com.ahoo.spring.demo2.Emploee" p:name="张三" p:car-ref="car"></bean>

    2.6 SpEL的属性注入

    SpEL:Spring Expression Language ,Spring表达式语。在Spring3.0及以上版本引入表达式语言来完成属性注入:

    <bean id="carinfo" class="com.ahoo.spring.demo2.CarInfo"></bean> <!-- SpEl方式注入 --> <bean id="car2" class="com.ahoo.spring.demo2.Car2"> <property name="name" value=#{carinfo.name} /> <property name="price" value=#{carinfo.calculator()} /> </bean>

    2.7 复杂类型(集合)的属性注入

    <!-- Spring的集合属性的注入 --> <bean id="collectionBean" class="com.ahoo.spring.demo3.CollectionBean"> <!-- 数组注入 --> <property name="arr"> <list> <value>张三</value> <value>李四</value> <value>王五</value> </list> </property> <!-- 列表注入 --> <property name="list"> <list> <value>张3</value> <value>李4</value> <value>王5</value> </list> </property> <!-- 集合注入 --> <property name="set"> <set> <value>Z三</value> <value>L四</value> <value>W五</value> </set> </property> <!-- map注入 --> <property name="map"> <map> <entry key="aaa" value="111" /> <entry key="bbb" value="222" /> <entry key="ccc" value="333" /> </map> </property> </bean>
    最新回复(0)