目录
3.6.1 SpEL(spring表达式语言,可以节省大量Java代码)
3.7.5 自动装配的注解@AutoWired、@Resource
3.5
3.5.1、spring创建第三方bean对象(方式一)
在Spring中,很多对象都是单实例的,在日常的开发中,我们经常需要使用某些外部的单实例对象,例如数据库连接池,下面讲解如何在spring中创建第三方bean实例
导包:
xml文件:ioc
//spring管理第三方bean:
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="url" value="jdbc:mysql://localhost:3306/demo">
</property>
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
</bean>
java文件:MyText(测试)
DruidDataSource dataSource = context.getBean("dataSource", DruidDataSource.class);
System.out.println(dataSource);
System.out.println(dataSource.getConnection());
3.5.2 spring引用外部配置文件(方式二)
db.properties文件:
jdbc.username=root
jdbc.password=123456
url=jdbc:mysql://localhost:3306/demo
driverClassName=com.mysql.jdbc.Driver
xml文件:ioc
↓
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
·
·
·
//当需要引入外部的配置文件的时候,需要导入一些context的命名空间
<context:property-placeholder
location(位置)="classpath:db.properties">
</context:property-placeholder>
在配置文件编写属性的时候需要注意:
spring容器在进行启动的时候,会读取当前系统的某些环境变量的配置,
当前系统的用户名是用username来表示的,所以最好的方式是添加前缀来做区分(如jdbc.)
============================== 1.1、 ================================
<bean id="person" class="com.mashibing.bean.Person">
<property name="name" value="${jdbc.username}"></property>
</bean>
============================== 1.2、 ================================
<bean id="dataSource2"class="com.alibaba.druid.pool.DruidDataSource">
<property name="username"value="${username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${url}"></property>
<property name="driverClassName"value="${driverClassName}">
</property>
</bean>
============================== 2、 ==================================
<bean id="person" class="com.mashibing.bean.Person">
<property name="name" value="${jdbc.username}"></property>
</bean>
java文件:MyText(测试)
============================== 1、 ==================================
DruidDataSource dataSource = context.getBean("dataSource2", DruidDataSource.class);
System.out.println(dataSource);
System.out.println(dataSource.getConnection());
============================== 2、 ==================================
Person person = context.getBean("person2", Person.class);
System.out.println(person);
执行后:
(没改之前,最开始执行了一次,后面默认就root,而不是root123)
解决:2、中加jdbc.
3.5.3 spring基于xml文件的自动装配
xml文件:ioc
//spring中的自动装配
/*
*<bean id="address1" class="com.mashibing.bean.Address">
* <property name="province" value="河北"></property>
* <property name="city" value="邯郸"></property>
* <property name="town" value="武安"></property>
*</bean>
*/
<bean id="address2" class="com.mashibing.bean.Address">
<property name="province" value="河北2"></property>
<property name="city" value="邯郸2"></property>
<property name="town" value="武安2"></property>
</bean>
<bean id="person" class="com.mashibing.bean.Person"autowire="byType">
</bean>
java文件:MyText(测试)
Person person = context.getBean("person", Person.class);
System.out.println(person);
(某些情况address1也能行)
在spring中,可以使用自动装配的功能(spring会把某些bean注入到另外bean中)
可以使用autowire属性来实现自动装配,有以下几种情况
=====================================================================
default/no: 不自动装配
=====================================================================
byName:按照id来进行装配,根据set方法后面的名称首字母小写决定的,不是参数列表的名称,如果找不到则装配null
java文件:Person(个人)
改:在set方法中,将setAddress()名改setAaddress()
(为null找不到,所以只能改有参函数名不能改方法名)
=====================================================================
byType:按照bean的类型来进行装配,如果有多个类型,就会报错,不知道选择哪一个具体的类型,如果找不到则装配null
(加载所有的属性)
=============================================