springmvc 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-->创建bean三种方法<-->
    <!-->1 无参构造函数  scope="singleton" 默认单例 生命周期 先执行其构造函数 然后init 然后其它方法 最后随容器ApplicationContext.close() 执行destroy方法
        scope="prototype" bean对象最后destory不会随容器ApplicationContext.close()而销毁
    <-->
    <bean id="userService" class="com.howhy.jdbc.impl.UserServiceImpl" init-method="init" destroy-method="destory" ></bean>
    <!-->2 类中方法生成对象
    public class InstanceFactory {
    public UserService getUserService()  {
        return new UserServiceImpl();
    }
}
    <-->
    <!-->
    <bean id="instanceFactory" class="com.howhy.jdbc.factory.InstanceFactory"></bean>
    <bean id="userServiceFactory" factory-bean="instanceFactory" factory-method="getUserService"></bean><-->
    <!-->3 类中静态方法生成对象
    public class StaticBeanFactory {
    public static UserService getUserService()  {
        return new UserServiceImpl();
    }
}
    <-->
    <!-->
    <bean id="userstaticServiceFactory" class="com.howhy.jdbc.factory.StaticBeanFactory" factory-method="getUserService"></bean>
    <-->
</beans>

 

posted @ 2020-08-08 16:55  howhy  阅读(71)  评论(0)    收藏  举报