Spring_Bean管理

Spring_Bean管理

八月 19, 2019

Bean的配置

IOC容器的配置

ctx
1
2
3
4
5
//两种方式获取
//利用id 定位到IOC容器中的bean
HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");
//利用类型返回IOC 容器中的Bean 但要求IOC容器中只能有一个该类型的bean
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);

依赖注入的方式

属性注入

构造器注入




工厂方法注入(很少使用,不推介)

三种实例化方式

1.使用构造器实例化(默认无参数)
2.静态工厂方式实例化(简单工厂模式)
3.实例工厂方法实例化(工厂方法模式)