Bean管理(2)


FactoryBean

  • 定义的bean类型和返回类型不一样

  • bean.xml

1
<bean id="musics" class="bean.Musics"></bean>
  • 创建Musics类
1
2
3
4
5
6
7
8
public class Musics implements FactoryBean<Music>{
@Override
public Music getObject() throws Exception {
Music music=new Music();
music.setName("云烟成雨");
return music;
}
}
  • 测试
1
2
Music music =context.getBean("musics", Music.class);
System.out.println(music.getName());

Bean作用域

  • singleton:单实例
  • prototype:多实例
  • bean.xml
1
<bean id="music" class="bean.Music" scope="prototype"></bean>
  • 测试
1
2
3
4
Music music1 =context.getBean("music", Music.class);
Music music2 =context.getBean("music", Music.class);
System.out.println(music1);
System.out.println(music2);

Bean生命周期

  1. 构造器创建实例
  2. 设置属性和引用其他bean
  3. 把实例传递 bean 后置处理器的方法 postProcessBeforeInitialization
  4. 调用初始化方法
  5. 把实例传递 bean 后置处理器的方法 postProcessAfterInitialization
  6. 对象获取
  7. 销毁实例
  • 创建后置处理器类
1
2
3
4
5
6
7
8
9
10
11
12
public class MyBeanPost implements BeanPostProcessor {//创建后置处理器实现类
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前执行的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之后执行的方法");
return bean;
}
}
  • bean.xml
1
2
3
4
<bean id="music" class="bean.Music" init-method="initMethod" destroy-method="destroyMethod">
<property name="name" value="云烟成雨"></property>
</bean>
<bean id="myBeanPost" class="bean.MyBeanPost"></bean>
  • 测试
1
2
3
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Music music = context.getBean("music", music.class);
context.close();

XML自动装配

  • byName:根据名字自动装配
  • byType:根据类型自动装配
  • bean.xml
1
2
<bean id="student" class="bean.Student"autowire="byName"></bean>
<bean id="course" class="bean.Course"></bean>
  • 创建student类
1
2
3
4
5
6
7
8
9
public class Student {
Course course;
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}

外部属性文件

直接配置数据库信息

  • 引入Druid连接池依赖jar包
  • 配置Druid连接池
1
2
3
4
5
6
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>

引入外部属性文件配置数据库连接池

1
2
3
4
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userDb
prop.userName=root
prop.password=root
  • bean.xml
1
2
3
4
5
6
7
8
<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<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>