上节课我们学习了基于xml的方式来注入Bean属性
Spring里面有两种类型的Bean
- 普通的bean(Spring配置文件中,定义的class里的类型一定是返回的类型)
- 工厂bean(配置文件中定义的类型可以与返回的类型不一样。)
Bean管理(工厂Bean)
FactoryBean(这是Spring里面一个内置的Bean)
1 创建类,这个类作为工厂bean,实现接口FactoryBean
2 实现接口里面的方法,在实现的方法中定义返回的bean类型。
工厂bean结构
package com.study.lele.Test2;
import com.study.lele.Test1.Course;
import org.springframework.beans.factory.FactoryBean;
public class MyBean implements FactoryBean<Course> {
//定义返回bean
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCname("abc");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
配置文件中的设置
<bean id="myBean" class="com.study.lele.Test2.MyBean"></bean>
下面是获取工厂bean的实例,在设置返回值为Course后实际上我们获取到的已经并不是原本的类路径对应的类型了(MyBean)
String config = "beanCourse21.xml";
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext(config);
Course cs =
applicationContext.getBean("myBean",Course.class);
//第二个参数定义了返回值类型
System.out.println(cs);
Bean的作用域
配置文件中配置bean标签,在测试类中获取。
在Spring里面创建bean实例的时候,可以是单例对象也可以是多例对象
- 单实例:每次获取的都是同一个对象
- 多实例:每次获取的都是新建的一个对象
在Spring里面设置创建的对象是单实例还是多实例
Spring里面在默认情况下创建的bean是一个单实例对象
如何设置bean是单实例还是多实例?
bean标签里面有一个属性用来设置
scope = “singleton” 表示单实例对象(加载配置文件时创建对象)饿汉式?
scope = “ prototype ” 表示多实例对象 (调用getBean时创建对象)懒汉式?
Bean管理XML方式(自动装配)
什么是自动装配?
Spring自动的帮助我们填充属性,根据定义好的一种装配规则。
实现自动装配
bean标签属性autowire
byName 根据属性名称注入 无限制
要注入的beanId的值和类里面属性的名称也要一样
byType 根据属性类型注入 一个全类名只能对应一个bean
根据类型-找到类的全路径-找到bean
但是如果一个全类名写了多个bean,则会报错(无法定位到bean)
Bean管理(引入外部属性文件)
之前的例子中,我们要写一个对象bean并且用标签进行属性配置,但这种方式不是很方便——将相关属性值装配在其他的文件中。然后在配置文件中读取其中的内容。
例:比如操作数据库的对象,将url,等各种配置写到文件中
直接配置数据库信息
1 配置德鲁伊连接池
引入德鲁伊连接池依赖jar包
2 引入外部属性文件对数据库连接池进行配置
数据库连接池配置
:首先我们要创建DruidDataSource对象
<!-- 直接配置数据库连接池 -->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/userDb">
</property>
<property name="username"
value="root"></property>
<property name="password"
value="123456"></property>
</bean>
缺点:值写死了,不好修改
prop.driverClass = com.mysql.cj.jdbc.Driver
prop.url = jdbc:mysql://localhost:3306/userDb
prop.userName = root
prop.password =123456
外部文件,用单词.属性比较容易减少冲突
外部文件引入到Spring配置文件中。
1引入context名称空间。
<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.xsd"
>
<!-- 使用标签引入外部的配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--${}表达式可以通过key取出所需要的值 -->
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.userName}"></property>
<property name="password" value="${prop.password}"></property>
</bean>