IOC学习实验10-14

13 篇文章 0 订阅

实验10:创建带有生命周期方法的bean

实验11:测试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">
	<!-- 实验10:创建带有生命周期方法的bean 
		ioc容器中注册的bean
			1)单例bean:容器启动的时候就会创建好,容器关闭的时候销毁
			2)多实例的bean:获取的时候才创建;
		我们可以为bean自定义一些生命周期方法,Spring在创建和销毁时就会调用指定的方法
		destroy-method="":销毁方法
		init-method="":初始化方法
		The method must have no arguments, but may throw any exception. 
	-->
	<bean id="book01" class="com.yj.bean.Book" 
	destroy-method="myDestory" init-method="myInit" scope="singleton">		
	</bean>
	
	<!-- 实验11:测试bean的后置处理器
	Spring有一个接口,后置处理器,可以在bean的初始化之前和之后调用方法
	 -->
	<bean id="BeanPostProcessor" class="com.yj.bean.MyBeanPostProcessor"></bean>
	<bean id="car01" class="com.yj.bean.car"></bean>


</beans>

实验12:引用外部属性文件★

<?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 http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 实验12:引用外部属性文件★,依赖context名称空间
	数据库连接池单例模式最好,一个项目就一个连接池,连接池里管理很多连接,连接时直接从连接池中拿
	可以让Spring创建连接池对象(管理)

 -->
 	<!-- 加载外部配置文件,classpath:引入类路径下的一个资源 -->
 	<!-- userName时Spring中的一个关键字,为了防止配置文件中的key和Spring的关键字冲突, -->
 	<context:property-placeholder location="classpath:dbconfig.properies"/>
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="user" value="${jdbc.username}"></property>
	<property name="password" value="${jdbc.password}"></property>
	<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
	<property name="driverClass" value="${jdbc.driverClass}"></property>
	
	</bean>


</beans>

实验13:基于XML的自动装配(自动赋值)

实验14:[SpEL测试I](Spring Expression Language)Spring的表达式语言

<?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 id="ca" class="com.yj.bean.car">
	<property name="carName" value="领克"></property>
	<property name="color" value="黑色"></property>
</bean>

<!-- 	实验13:基于XML的自动装配(自动赋值)
		为person里的自定义类型的属性赋值,
		property:手动赋值 
		自动赋值(自动装配):仅对自定义属性有效
		autowire="default",默认关闭
		byName:private Car ca;以属性名作为id去容器中找到这个组件,给它赋值,找不到就是null
			car = ioc.gertBean("ca");
		byType:private Car ca;以属性的类型作为查找依据,有多个同类型的就会报错,没找到就装配null
			car = ioc.getBean("Car.class");
		constructor:按照构造器赋值
			1)先按照有参构造器参数的类型装配,没有就装配null
			2)如果按照类型有多个,参数的名字作为id继续匹配
				永远不会报错
		no:default一样
-->
	
<bean id="person" class="com.yj.bean.person" autowire="byType">
	
</bean>
<!-- List<Book> books:容器会被所有的book封装list赋值给这个属性 -->
<bean id="book01" class="com.yj.bean.Book">
	<property name="bookName" value="01"></property>
</bean>
<bean id="book02" class="com.yj.bean.Book">
	<property name="bookName" value="02"></property>
</bean>
<bean id="book03" class="com.yj.bean.Book">
	<property name="bookName" value="03"></property>
</bean>

<!-- 实验14[SpEL测试I](Spring Expression Language)Spring的表达式语言
    在SpEL中使用字面量、
    引用其他bean、
    引用其他bean的某个属性值、
    调用非静态方法
    调用静态方法、
    使用运算符 -->
<bean id="person04" class="com.yj.bean.person">
	<!-- 字面量:${}   #{} -->
	<property name="age" value="#{12*5}"></property>
	<property name="salary" value="#{123.4*12}"></property>
	<!-- 引用其他bean的属性 -->
	<property name="lastName" value="#{book01.bookName}"></property>
	<property name="ca" value="#{ca}"></property>
	
	<!-- 调用静态方法: #{T(静态类名).静态方法名} -->
	<property name="email" value="#{T(java.util.UUID).randomUUID().toString().substring(0,5)}"></property>
	<!-- 调用非静态方法: -->
	<property name="gender" value="#{book01.getBookName()}"></property>
	
	
</bean>

</beans>

调用

package com.yj.test;

import static org.junit.Assert.*;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;

import com.yj.bean.person;

public class IOCTest {
	ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("applicatonContext.xml");
	ConfigurableApplicationContext ioc2 = new ClassPathXmlApplicationContext("applicationContext02.xml");
	ConfigurableApplicationContext ioc3 = new ClassPathXmlApplicationContext("applicationContext03.xml");
	/**
	 * 单例bean的声明周期:(容器启动)构造器-》初始化方法-》销毁方法
	 * 多实例:获取bean(构造器-》初始化方法)-》容器关闭不会调用销毁方法
	 * 无论bean是否有初始化方法,后置处理器都默认有,还会继续工作
	 */
	@Test
	public void test() {
//		Object bean = ioc.getBean("book01");
//		System.out.println("容器要关闭了");
//		ioc.close();
	}
	@Test
	public void test02() throws SQLException{
		//1.从容器中拿到连接池
		DataSource bean = (DataSource) ioc2.getBean("dataSource");
		//2,按照类型获取,可以获取到这个类型下的所有实现类的字类
		//DataSource bean = ioc.getBean(DataSource.class);
		System.out.println(bean.getConnection());
	
	}
	/**
	 * 基于XML的自动装配
	 */
	@Test
	public void test03(){
		person bean = ioc3.getBean(person.class);
		System.out.println(bean);
	}
	@Test
	public void test04(){
		person bean = (person) ioc3.getBean("person04");
		System.out.println(bean);
	}

}

6SpEL

6.1简介
Spring Expression Language,Spring表达式语言,简称SpEL。支持运行时查询并可以操作对象图。
和JSP页面上的EL表达式、Struts2中用到的OGNL表达式一样,SpEL根据JavaBean风格的getXxx()、setXxx()方法定义的属性访问对象图,完全符合我们熟悉的操作习惯。

6.2基本语法
SpEL使用#{…}作为定界符,所有在大框号中的字符都将被认为是SpEL表达式。

6.3使用字面量
●整数:
●小数:
●科学计数法:
●String类型的字面量可以使用单引号或者双引号作为字符串的定界符号


●Boolean:

6.4引用其他bean

<bean id="emp04" class="com.atguigu.parent.bean.Employee">
	<property name="empId" value="1003"/>
	<property name="empName" value="Kate"/>
	<property name="age" value="21"/>
	<property name="detp" value="#{dept}"/>
</bean>

6.5引用其他bean的属性值作为自己某个属性的值

<bean id="emp05" class="com.atguigu.parent.bean.Employee">
	<property name="empId" value="1003"/>
	<property name="empName" value="Kate"/>
	<property name="age" value="21"/>
	<property name="deptName" value="#{dept.deptName}"/>
</bean>

6.6调用非静态方法

<!-- 创建一个对象,在SpEL表达式中调用这个对象的方法 -->
<bean id="salaryGenerator" class="com.atguigu.spel.bean.SalaryGenerator"/>

<bean id="employee" class="com.atguigu.spel.bean.Employee">
	<!-- 通过对象方法的返回值为属性赋值 -->
	<property name="salayOfYear" value="#{salaryGenerator.getSalaryOfYear(5000)}"/>
</bean>

6.7调用静态方法

<bean id="employee" class="com.atguigu.spel.bean.Employee">
	<!-- 在SpEL表达式中调用类的静态方法 -->
	<property name="circle" value="#{T(java.lang.Math).PI*20}"/>
</bean>

6.8运算符
①算术运算符:+、-、*、/、%、^
②字符串连接:+
③比较运算符:<、>、==、<=、>=、lt、gt、eq、le、ge
④逻辑运算符:and, or, not, |
⑤三目运算符:判断条件?判断结果为true时的取值:判断结果为false时的取值
⑥正则表达式:matches

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值