1.知识点总结
spring是 如何 装配 bean的 : 实例化 bean的
1)是通过 无参数的构造器 来帮助 我们实例化类的
2) 静态工厂实例化bean 简单
3)实例工厂实例化bean 两个 bean标签 ,调用的是 第二个 bean标签
<bean id="beanFactoryTwo" class="com.oracle.factory.BeanFactoryTwo"></bean>
<bean id="userServiceId" factory-bean="beanFactoryTwo" factory-method="getUserServiceImpl">
</bean>
====================
bean的生命周期:
1.初始化spring容器(其实是第0步,bean生命周期不包括,但却是bean所必须的,也许可以看作产房?);
2.spring对bean实例化,默认是单例(Bean的作用域);
3.检查Aware相关接口并设置相关依赖:
1)如果实现了BeanNameAware接口,spring将bean的id传给setBeanName()方法;
2)如果实现了BeanFactoryAware接口,spring调用setBeanFactory()方法,将BeanFactory实例传进来;
3)如果实现了ApplicationContextAware接口,spring调用setApplicationContext()会被调用,将应用上下文的应用传入到Bean中
4.BeanPostProcessor前置处理(如果有),调用postProcessorBeforeInitialization()
5.初始化相关:如果实现了InitializingBean接口,spring调用afterPropertiesSet();类似地,如果实现了init-method属性声明了初始化方法,该方法也会被调用;亦或是@PostConstruct注解
6.BeanPostProcessor后置处理(如果有),调用postProcessorAfterInitialization()
7.注册必要的Destruction相关回调接口
8.此时bean已经准备就绪,可被程序使用了,将一直驻留在应用上下文中,直到该应用上下文被销毁
9.销毁相关: 如果实现了DisposableBean接口,spring调用destory();类似地,还有自定义销毁方法destory-method和注
2.案例代码:
1)工程架构图:
2)代码:
1com.oracle.dao包:
package com.oracle.dao;
import com.oracle.vo.User;
public interface UserDao {
public void addUser(User user);
}
2.com.oracle.daoImpl包:
package com.oracle.daoImpl;
import com.oracle.dao.UserDao;
import com.oracle.vo.User;
public class UserDaoImpl implements UserDao {
@Override
public void addUser(User user) {
System.out.println("我是spring的DI技术...hahaahahaxixixixixi");
}
}
3.com.oracle.factory包:
package com.oracle.factory;
import com.oracle.daoImpl.UserDaoImpl;
import com.oracle.serviceImpl.UserServiceImpl;
/**
* 静态工厂
*/
public class BeanFactoy {
public static UserServiceImpl getUserServiceImpl(){
return new UserServiceImpl();
}
public static UserDaoImpl getUserDaoImpl(){
return new UserDaoImpl();
}
}
=============================================
package com.oracle.factory;
import com.oracle.daoImpl.UserDaoImpl;
import com.oracle.serviceImpl.UserServiceImpl;
/**
* 实例工厂
*/
public class BeanFactoyTwo {
public UserServiceImpl getUserServiceImpl(){
return new UserServiceImpl();
}
public UserDaoImpl getUserDaoImpl(){
return new UserDaoImpl();
}
}
4.com.oracle.service包:
package com.oracle.service;
import com.oracle.vo.User;
public interface UserService {
public void addUser(User user);
}
5.com.oracle.serviceImpl包:
package com.oracle.serviceImpl;
import com.oracle.dao.UserDao;
import com.oracle.daoImpl.UserDaoImpl;
import com.oracle.service.UserService;
import com.oracle.vo.User;
public class UserServiceImpl implements UserService {
UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void addUser(User user) {
userDao.addUser(new User());
}
}
6.com.oracle.test包:
package com.oracle.test;
import com.oracle.dao.UserDao;
import com.oracle.factory.BeanFactoyTwo;
import com.oracle.service.UserService;
import com.oracle.serviceImpl.UserServiceImpl;
import com.oracle.vo.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
@Test
public void TestDI(){
ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
UserService userService= (UserService) c.getBean("userServicId");
userService.addUser(new User());
}
}
7.com.oracle.vo包:
package com.oracle.vo;
public class User {
private String name;
public User() {
}
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
8.config包:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 静态工厂
<bean id="userServiceId" class="com.oracle.factory.BeanFactoy" factory-method="getUserServiceImpl">
<property name="userDao" ref="userDaoId"></property>
</bean>
<bean id="userDaoId" class="com.oracle.factory.BeanFactoy" factory-method="getUserDaoImpl"></bean>
-->
<!--实例工厂-->
<bean id="beanFactoyTwo" class="com.oracle.factory.BeanFactoyTwo"></bean>
<bean id="userServicId" factory-bean="beanFactoyTwo" factory-method="getUserServiceImpl">
<property name="userDao" ref="userDaoId"></property>
</bean>
<bean id="userDaoId" factory-bean="beanFactoyTwo" factory-method="getUserDaoImpl"></bean>
</beans>