创建Bean时,class属性必须指定,此时为静态工厂类. factory-method指定静态工厂方法名. 接口: public interface Being { public void testBeing(); } Dog类 public class Dog implements Being{ private String msg; public void setMsg(String msg) { this.msg = msg; } @Override public void testBe…
public class Factory { public static Person staticCreate(){ Person p = new Person(); p.name="staticCreate"; return p; } public Person instanceCreate(){ Person p = new Person(); p.name="instanceCreate"; return p; } } public static void…
1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class Car { private String brand; private double price; public Car(){ } public Car(String brand,double price){ this.brand=brand; this.price=price; } public S…
Spring 框架Bean支持以下五个作用域: 下面介绍两种作用域,singleton和protoype singleton作用域 singleton作用域为默认作用域,在同一个ioc容器内getBean是同一个bean,如果创建一个singleton作用域Bean定义的对象实例,该实例将存储在该Bean的缓存中,那么以后所有针对该 bean的请求和引用都返回缓存对象. 编写HelloWorld.java package com.example.spring; public class Hell…