spring5-配外部文件-spEL-工厂bean-FactoryBean-注解配bean

spring配外部文件

我们先在Spring里配置一个数据源

1.导c3p0包,这里我们先学一下hibernate持久化框架,以后用mybites.

<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-core</artifactId>
  		<version>5.2.17.Final</version>
  	</dependency>
  	<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-c3p0</artifactId>
  		<version>5.3.0.Final</version>
  	</dependency>

2.安装mysql 8.x数据库(参考相关资料),导入驱动包

<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>8.0.16</version>
  	</dependency>

3.看具体代码:

    <!-- 配置c3p0,ComboPooledDataSource对象,它指向一个数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="user" value="root"></property>
       <property name="password" value="xiong"></property>
       <property name="jdbcUrl" value="jdbc:mysql://124.220.60.104:3306/spring5"></property>
       <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
    </bean>

测试数据库连接

	@Test
	public void dataSourcetest() throws SQLException{
   
   
		ApplicationContext ct = new ClassPathXmlApplicationContext("dataSource.xml");
		DataSource dataSource = (DataSource)ct.getBean("dataSource");
		System.out.println(dataSource.getConnection());
	}
-----------------------------------------------------------------
com.mchange.v2.c3p0.impl.NewProxyConnection@45c7e403 [wrapping: com.mysql.cj.jdbc.ConnectionImpl@2925bf5b]   //连接成功,产生一个代理数据库服务器

              上面这样配置,写死了数据是相对以后转生产环境中修改是十分不方便的,生产环境中,我们极有可能会把dao层打jar包,又分成多模块开发,所以修改起来是十分麻烦,所以我们会把值写成键值对的形式单独保存到一个文件中

使用外部文件保存数据值,再配spring配置文件

在这里插入图片描述
jdbc.properties

jdbc.user=root
jdbc.password=xiong
jdbc.url=jdbc:mysql://124.220.60.104:3306/spring5
jdbc.driverClass=com.mysql.cj.jdbc.Driver

dataSouce.xml–spring配置文件

    <!-- 配置c3p0,ComboPooledDataSource对象,它指向一个数据源 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="user" value="${jdbc.user}"></property>
       <property name="password" value="${jdbc.password}"></property>
       <property name="jdbcUrl" value="${jdbc.url}"></property>
       <property name="driverClass" value="${jdbc.driverClass}"></property>
    </bean>

Spring的表达式语言spEL

       Spring表达式语言(SpEL):是一个支持运行时查询和操作对象图的强大表示是语言,是一种可以与一个基于spring的应用程序中的运行时对象交互的东西。总得来说SpEL表达式是一种简化开发的表达式,通过使用表达式来简化开发,减少一些逻辑、配置的编写。

       语法类似于 EL:SpEL 使用 #{…} 作为定界符 , 所有在大括号中的字符都将被认为是 SpEL , SpEL 为 bean 的属性进行动态赋值提供了便利。
通过 SpEL 可以实现:

通过 bean 的 id 对 bean 进行引用,用了SpEL在bean标签中可以用value代替ref。
可以像EL一样用点运算符调用方法以及对象中的属性。
计算表达式的值
正则表达式的匹配。

SpEL 字面量,意义不大,spring内部本身有数据类型的自动转换机制,直接写值就好了,不必用SqEL,了解:

整数:#{8}   实际上,直接写成“8”即可,如前面讲的:p:id="1",就表示1
小数:#{8.8}
科学计数法:#{1e4}
String:可以使用单引号或者双引号作为字符串的定界符号。
Boolean:#{true}

SpEL引用bean , 调用它属性和方法:

引用其他对象:#{car}
引用其他对象的属性:#{car.price}
调用其它方法 , 还可以链式操作:#{person.pet.toString()}
调用静态方法静态属性:#{T(java.lang.Math).PI}
Spring EL 操作List、Map集合取值

//SpEL支持的运算符号:

算术运算符:+,-,*,/,%,^(加号还可以用作字符串连接)
比较运算符:< , > , == , >= , <= , lt , gt , eg , le , ge
逻辑运算符:and , or , not , |
if-else 运算符(类似三目运算符):?:(temary), ?:(Elvis)
正则表达式:#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}'}

例:引用对象举例
       为了测试,我在Pet类中,添加成员属性private double price及get/set方法,在Person类中添属性成员private double petPrice,private double pi及get/set方法,petPrice属性在spring创建对象时,引用Pet对象中定义的值,pi属性引用lang包中的静态成员常量。

   <!-- springEL,测试 -->
     <bean id="cat1" class="cn.ybzy.springdemo.model.Pet" p:id="1" p:type="cat" p:price="1000.0"></bean>
     
     <bean id="person" class="cn.ybzy.springdemo.model.Person">
        <property name="id" value="1"></property>
        <property name="pet" value="#{cat1}"></property>
        <property name="petPrice" value="#{cat1.price}"></property>
        <property name="pi" value="#{T(java.lang.Math).PI}"></property>
     </bean>
	
	/*
	 * spring EL 测试
	 */
	@Test
	public void springEltest() {
   
   
		ApplicationContext ct = new ClassPathXmlApplicationContext("springEL.xml");
		Person person=(Person)ct.getBean("person");
		System.out.println(person);
		System.out.println(person.getPi());
	}
--------------------------------------------------------------------
Person [id=1, pet=Pet [id=1, type=cat, price=1000.0], cars=null, petPrice=1000.0]
3.141592653589793	

例:springEL运算符演示,这里我只举,三目运算会和正则表达式匹配运算
       为了测试,我在Person类中添加test属性,private String test及get/set方法,它会根根价格来作两个判 断,大于800元,是土豪,小于100元是普通人养宠物。

 <property name="test" value="#{cat1.price > 800 ? '土豪' : '普通人'}"></property>
   System.out.println(person.getTest());
------------------------------------------------
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊少文

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值