当前位置: 首页 > news >正文

做环卫车怎么做网站韩国日本比分

做环卫车怎么做网站,韩国日本比分,常州溧阳网站建设,数据库内容进 wordpress1. Spring中的BeanFactory BeanFactory是一个接口,表示它是一个工厂,负责生产和管理bean。在Spring中,BeanFactory是IOC容器的核心接口,定义了管理Bean的通用方法,如 getBean 和 containsBean。 BeanFactory与IOC容器…

1. Spring中的BeanFactory

BeanFactory是一个接口,表示它是一个工厂,负责生产和管理bean。在Spring中,BeanFactory是IOC容器的核心接口,定义了管理Bean的通用方法,如 getBeancontainsBean

BeanFactory与IOC容器

在这里插入图片描述

DefaultListableBeanFactory类图

在这里插入图片描述

BeanFactory只是个接口,并不是IOC容器的具体实现。Spring提供了多种实现,如 DefaultListableBeanFactoryXmlBeanFactoryApplicationContext等。

ApplicationContext

ApplicationContext是Spring框架中最常用的IoC容器,是BeanFactory的子接口,提供了更丰富的功能和更强的扩展性。

在这里插入图片描述

ApplicationContext的子类

  1. ClassPathXmlApplicationContext:基于XML配置文件的ApplicationContext实现类,可以加载类路径下的XML配置文件。
  2. FileSystemXmlApplicationContext:基于XML配置文件的ApplicationContext实现类,可以加载文件系统中的XML配置文件。
  3. AnnotationConfigApplicationContext:基于Java注解的ApplicationContext实现类,可以通过Java配置类来管理Bean实例。
  4. WebApplicationContext:适用于Web应用场景的ApplicationContext子接口,提供了更丰富的Web应用支持。

这些ApplicationContext子类都实现了ApplicationContext接口,提供了不同的功能和扩展性,可以根据具体的应用场景选择合适的ApplicationContext子类来管理Bean实例。

BeanFactory的使用示例

// User.java
public class User {private int id;private String name;private Friends friends;public User() { }public User(Friends friends) {this.friends = friends;}// getters and setters...
}// Friends.java
public class Friends {private List<String> names;public Friends() { }public List<String> getNames() {return names;}public void setNames(List<String> names) {this.names = names;}
}

配置文件(bean.xml)

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.2.xsd"><bean id="User" class="com.example.factory.User"><property name="friends" ref="UserFriends" /></bean><bean id="UserFriends" class="com.example.factory.Friends"><property name="names"><list><value>LiLi</value><value>LuLu</value></list></property></bean>
</beans>

测试类

public class SpringFactoryTest {public static void main(String[] args) {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");User user = ctx.getBean("User", User.class);List<String> names = user.getFriends().getNames();for (String name : names) {System.out.println("FriendName: " + name);}ctx.close();}
}

2. Spring中的FactoryBean

FactoryBean 是一个工厂Bean,相当于将工厂类放到了Spring中管理。当获取此Bean时返回的是该工厂生成的Bean。

FactoryBean通常用于创建比较复杂的bean。对于一般的bean,可以直接用XML配置;但如果一个bean的创建过程中涉及到很多其他bean和复杂的逻辑,用XML配置可能比较困难,这时可以考虑用FactoryBean。

FactoryBean接口

public interface FactoryBean<T> {String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";@NullableT getObject() throws Exception;@NullableClass<?> getObjectType();default boolean isSingleton() {return true;}
}

代码示例

Car实体

public class Car {private String color;private String brand;private double price;// getters and setters...
}

CarFactoryBean

@Component("carFactoryBean")
public class CarFactoryBean implements FactoryBean<Car> {@Overridepublic Car getObject() throws Exception {System.out.println("FactoryBean的getObject替换掉getBean...");return new Car();}@Overridepublic Class<?> getObjectType() {return Car.class;}@Overridepublic boolean isSingleton() {return true;}
}

测试类

public class Test01 {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);Car car = context.getBean(Car.class);System.out.println(car);}
}// 打印结果
// FactoryBean的getObject替换掉getBean...
// com.example.Car@4d14b6c2

在实例化Bean过程比较复杂的情况下,如果按照传统的方式,则需要在中提供大量的配置信息。配置方式的灵活性是受限的,这时采用编码的方式可能会得到一个简单的方案。Spring为此提供了一个 FactoryBean的工厂类接口,用户可以通过实现该接口定制实例化Bean的逻辑。

FactoryBean 与 BeanFactory 的区别

  • BeanFactory是一个大工厂,是IOC容器的根基,有繁琐的bean声明周期处理过程,可以生成各种各样的Bean。
    在这里插入图片描述

  • FactoryBean是一个小工厂,它自己本身也是一个Bean,但是可以生成其他Bean。用户可以通过实现该接口定制实例化Bean的逻辑。
    在这里插入图片描述

这种设计模式本质上是一个工厂方法模式,通过公共的工厂接口和不同的具体工厂,来获取对象。

http://www.shuangfujiaoyu.com/news/53329.html

相关文章:

  • ui设计周末培训机构合肥全网优化
  • 曲阳县做网站seo整站优化服务
  • 温州模板网站建站安仁网络推广
  • 做化妆品销售网站如何福州网络营销推广公司
  • 深圳哪家公司需要网站建设的搜索推广出价多少合适
  • 个人博客网站制作搭建百度平台我的订单查询在哪里
  • 高端的食品行业网站开发网站关键词优化怎么弄
  • 庆阳做网站的公司百度搜索关键词排名查询
  • 做代购注册什么网站如何给公司做网络推广
  • 单位网站怎么做属于网络营销的特点是
  • 网站策划书范文模板引擎优化seo是什么
  • 厦门哪些企业做视频网站的网站seo快速
  • python在线播放seo初学教程
  • 效果图网站推荐大全seo广告投放
  • 平面设计师作品网站泉州seo
  • wordpress文章底部内容郑州seo优化培训
  • 电子商务网站建设的建议百度搜索量查询
  • 河南网站优化靠谱的广告联盟
  • 揭阳做网站哪个好中国法律服务网app最新下载
  • 余姚做网站设计的站长统计app最新版本2023
  • wordpress打不开自定义龙斗seo博客
  • wordpress注册无法设置密码网络搜索优化
  • 成品网站和模板建站营销网站建设服务
  • 美女图片的网站网站源码资源网站优化排名优化
  • 商丘房产网seo实战教程
  • 世界重大新闻seo sem优化
  • php网站做安卓客户端seo赚钱项目
  • asp.net做网站如何展示界面少女长尾关键词挖掘
  • 网站做支付宝花呗分期爱站关键词搜索
  • 深圳有哪些网站开发公司今天最近的新闻