4、FactoryBean
Spring有两种类型的bean,一种是普通的bean,另一种是工厂bean(FactoryBean),工厂模式的意义就是为了不暴露对象创建的过程。
普通bean:在配置文件中定义bean类型就是返回类型
工厂bean:在配置文件中定义的bean类型可以和返回类型不一样

需要注意的是,上面还提了一个BeanFactory,两者的区别还是要搞清楚的,BeanFactory是IOC容器的基本实现,是Spring内部的使用接口。
创建一个Mybean类,实现FactoryBean接口,重写FactoryBean中的三个方法,可以看出源码中的三个方法:
getObject():返回需要注册的对象 ,如果为单例,该实例会放到Spring容器中单实例缓存池中
getObjectType():返回对象的类型
isSingleton():判断是否是单例 ,非单例时每次创建都会返回一个新的bean
package org.springframework.beans.factory;
import org.springframework.lang.Nullable;
public interface FactoryBean<T> {
String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
@Nullable
T getObject() throws Exception;
@Nullable
Class<?> getObjectType();
default boolean isSingleton() {
return true;
}
}
package com.factorybean;
import com.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.setCourseName("老滑头");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
5、bean的作用域
(1)在spring里面,默认的情况下,bean是单实例,当用getBean获取对象两次时,输出的引用对象地址一样

(2) 如何设置单实例还是多实例
在spring配置文件bean标签里面有属性(scope)用于设置单实例还是多实例。
scope属性值:
第一个值为默认值,singleton,表示单例对象,第二个值,prototype,表示多实例对象。
singleton和prototype的区别:
第一 singleton单实例,prototype多实例
第二 设置scope值是singleton的时候,在加载spring配置文件时就会创建单实例对象(饿汉式)。设置scope值是prototype的时候,不是在加载spring配置文件的时候创建对象,而是在调用getBean方法时才会创建多实例对象。可以看出当用getBean获取对象两次时,输出的引用对象地址不一样。

6、bean生命周期
生命周期,即从对象的创建到对象销毁的过程
bean的生命周期(面试常问):
(1)通过构造器创建bean实例(无参构造)
(2)为bean的属性设置值和对其他bean的引用(调用set方法)
(3)把bean实例传递给bean后置处理,执行postProcessBeforeInitialization方法
(4)调用bean的初始化方法(需要进行配置)
(5)把bean实例传递给bean后置处理器,执行postProcessAfterInitialization方法
(6)bean可以使用了(对象获取到了)
(7)当容器关闭的时候,调用bean的销毁方法(需要自己配置销毁的方法)
package com.bean;
public class Orders {
private String oName;
//无参构造方法
public Orders() {
System.out.println("第一步:执行无参构造创建bean实例");
}
public void setoName(String oName) {
this.oName = oName;
System.out.println("第二步:调用set方法设置对象的属性值");
}
//创建执行的初始化方法
public void initMethod() {
System.out.println("第三步:执行初始化的方法");
}
//创建执行的销毁方法
public void destroyMethod() {
System.out.println("第五步:执行销毁的方法");
}
}
@Test
public void test4(){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("bean4.xml");
Orders orders = context.getBean("orders", Orders.class);
System.out.println("第四步:获取创建bean实例对象");
System.out.println(orders);
//手动让bean实例销毁
context.close();
}
package com.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
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;
}
}
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="orders" class="com.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
<property name="oName" value="手机"/>
</bean>
<!--配置后置处理器-->
<bean id="myBeanPost" class="com.bean.MyBeanPost"/>
</beans>

7、xml自动装配
根据指定的装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入。bean便签属性autowire,配置自动装配,autowire属性常用的两个值:byName根据属性名称注入,注入值bean的id和类属性名称一样,如果不一样,属性注入失败,得出的结果为null


byType根据属性类型注入(class的类型),如果是多个属性,采取就近原则,选第一个,后面的autowire注解也会谈到。
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employees" class="com.autowire.Employees" autowire="byType">
</bean>
<bean id="department" class="com.autowire.Department">
</bean>
</beans>
8、外部属性文件
外部属性文件在Mybatis中经常会用到,Spring也可以通过引入外部属性文件配置数据库连接池。把外部properties属性文件引入到配置文件中,这里需要引入context名称空间:
<?xml version="1.0" encoding="UTF-8"?>
<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">
六、IOC操作Bean管理(基于注解的方式)
使用注解进行spring配置,注解作用在类上面,方法上面,属性上面,使用注解的目的:简化xml配置,springboot就是简化spring的配置,可以实现完全注解开发(基于注解的方式在实际应用中使用的较多)。
1、Spring针对Bean管理创建对象提供注解
(1)@Component:相当于配置文件中的,泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类
(2)@Service:service层
(3)@Controller:web层
(4)@Repository:dao层
*上面四个注解功能是一样的,都可以创建bean实例
2、基于注解方式实现对象创建
第一步 开启组件扫描
<context:component-scan base-package="com"/>
如果扫描多个包,多个包使用","隔开 ,并且扫描包的上层目录。use-default-filters="false" 表示现在不使用默认的filter,自己配置filter。
context:include-filter,设置扫描哪些内容:
<context:component-scan base-package="com.service" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
context:exclude-filter:设置哪些内容不进行扫描context:exclude-filter:设置哪些内容不进行扫描
<context:component-scan base-package="com.service">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
3、基于注解方式进行属性的注入
(1)@AutoWired:根据属性类型进行自动装配
第一步:创建service和dao对象,在service和dao类上添加创建对象注解,在注解里面value属性值可以不写,默认是类名称,首字母小写,value值和bean的id等价。
package com.dao;
import org.springframework.stereotype.Repository;
@Repository(value ="userDaoImpl")
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("dao add..........");
}
}
package com.service;
import com.dao.UserDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class UserService {
private UserDao userDao;
@Value(value = "老王")
private String name;
public void add() {
System.out.println("service add........"+name);
userDao.add();
}
}
第二步:在service里面注入dao对象,在service类里面添加dao类型的属性,在属性上使用注解。
(2)@Qualifier:根据属性名称进行注入。这个@Qualifier注解的使用要和上面的@AutoWired一起使用。
@Autowired //根据类型进行注入
@Qualifier(value = "userDaoImpl11")//根据名称进行注入
(3)@Resource:可以根据类型注入,也可以根据名称注入。根据导包来看,import javax.annotation.Resource;是 java自带的注解,不是框架里有的。
@Resource(name = "userDaoImpl11")//根据名称进行注入
(4)@Value:注入普通类型属性
@Value(value = "老王")
private String name;
4、完全注解开发
创建一个配置类,替代xml配置文件。@Configuration作为配置类注解,可以替代xml配置文件,
组件扫描也可以用注解@ComponentScan(basePackages = {"com"})实现。
package com.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com"})
public class SpringConfig {
}