Spring学习笔记(六) Bean的作用域 & Spel 表达式

Bean的作用域

Bean的作用域,通过scope属性来控制。
默认为singleton,意味着使用的是单例对象,容器初始化的时候,即创建实例,全程就使用这个对象
设置为prototype,则在容器初始化的时候,不会创建该bean对象。而是每个请求的时候,才创建一个新的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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- singleton:默认值,容器初始化的时候创建这个bean,单例的bean -->
	<!-- prototype:原型,容器初始化的时候不创建bean实例,而是每次请求的时候才创建一个新的bean实例并返回 -->
	<bean id ="person"  class="com.harry.spring.scope.Person"
		p:age="20"	p:name="harry" scope="prototype"></bean>
	

</beans>

Main文件代码:
public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-scope.xml");
		Person person1 = (Person) ctx.getBean("person");
		Person person2 = (Person) ctx.getBean("person");
		System.out.println(person1);
		System.out.println(person2);
		person1.setAge(21);
		System.out.println(person1);
		System.out.println(person2);
		
		
	}

当scope设置为prototype时,输出结果为:

Person [name=harry, age=20]
Person [name=harry, age=20]
Person [name=harry, age=21]
Person [name=harry, age=20]


当scope设置为默认值singleton时,输出结果为:

Person [name=harry, age=20]
Person [name=harry, age=20]
Person [name=harry, age=21]
Person [name=harry, age=21]


以上。


Spel表达式

Spel 和EL表达式很想。

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


	<!-- 引用其他bean实例
		<property name = "prefix" value="#{prefixGenerator}"></property>
	 -->
	 
	 <!-- 引用其他bean实例的属性
		<property name = "suffix" value="#{sequenceGenerator.suffix}"></property>
	 -->
	 
	 <!-- 使用spel为属性赋值 -->
	 <bean id = "address" class="com.harry.spring.spel.Address"
	  	p:city=" #{'ShenZhen'} " p:street="baoan"
	 ></bean>
	 
	 <!-- 使用spel引用类的静态属性 -->
	 <bean id="car" class="com.harry.spring.spel.Car"
	 	p:brand="audi" p:price="300000" 
	 	
	 	p:typePerimeter="#{T(java.lang.Math).PI * 70}"
	 ></bean>
	 
	 <!-- 使用sped来引用其他的bean实例 -->
	 <bean id="person"  class="com.harry.spring.spel.Person"
	 p:car="#{car}" p:name="nucky" p:city="#{address.city}"
	 p:info="#{car.price > 100 ?'金领':'白领'}"
	 ></bean>
</beans>

Main函数为:
public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-spel.xml");
		Address address = (Address) ctx.getBean(Address.class);
		System.out.println(address);
		
		Car car = ctx.getBean(Car.class);
		System.out.println(car);
		
		Person person = ctx.getBean(Person.class);
		System.out.println(person);
	}

输出结果是:
address [street=baoan, city= ShenZhen ]
Car [brand=audi, price=300000.0, typePerimeter=219.9114857512855]
Person [name=nucky, car=Car [brand=audi, price=300000.0, typePerimeter=219.9114857512855], city= ShenZhen , info=金领]

在配置文件里,用到赋值,引用其他对象bean实例,引用其他对象bean实例的属性,设置用到了三目运算法进行判断。
其中Math,PI是一个数值。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值