Spring创建静态工厂
我们创建一个猫类
public class Cat {
private String name;
private int age;
public Cat(String name,int age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
创建一个生产猫的静态工厂
public class CatStaticFactory {
public static Cat catFactory(int type){
if(type==1){
return new Cat("小花",6);
}else if(type==2){
return new Cat("小黑",7);
}else {
return new Cat("小黄",5);
}
}
}
在xml中写入该工厂
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--创建静态工厂-->
<bean id="catFactory" class="a02Factory.CatStaticFactory" factory-method="catFactory" >
<constructor-arg name="type" value="1"></constructor-arg>
</bean>
</beans>
此时在主程序中调用该方法就ok了!
public static void main(String[] args) {
//当前应用的xml配置文件在类路径下
ApplicationContext ioc=new ClassPathXmlApplicationContext("ioc2.xml");
//获取当前容器中id名为person1的对象
Cat p=(Cat)ioc.getBean("catFactory");
System.out.println(p);
}
Spring创建实例工厂
创建一个生产猫的实例工厂
public class CatStaticFactory {
public static Cat catFactory(int type){
if(type==1){
return new Cat("小花",6);
}else if(type==2){
return new Cat("小黑",7);
}else {
return new Cat("小黄",5);
}
}
}
在xml中写入该工厂
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--创建实例工厂-->
<bean id="catFactory2" class="a02Factory.CatFactory"></bean>
<bean id="cat" class="a02Factory.Cat" factory-bean="catFactory2" factory-method="catFactory">
<constructor-arg name="type" value="1"></constructor-arg>
</bean>
</beans>
此时在主程序中调用该方法就ok了!
public static void main(String[] args) {
//当前应用的xml配置文件在类路径下
ApplicationContext ioc=new ClassPathXmlApplicationContext("ioc2.xml");
Cat p=(Cat)ioc.getBean("cat");
System.out.println(p);
}
FactoryBean内置工厂接口
继承FactoryBean接口
public class CatFactorySpring implements FactoryBean<Cat> {
@Override
public Cat getObject() throws Exception {
return new Cat("小花",1);
}
@Override
public Class<?> getObjectType() {
return Cat.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
在xml中写入该工厂
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--FactoryBean是Spring定义的一个接口,只要是这个接口实现类,Spring都认为是个工厂-->
<bean id="catBeanFactory" class="a02Factory.CatFactorySpring"></bean>
</beans>
此时在主程序中调用该方法就ok了!
public static void main(String[] args) {
//当前应用的xml配置文件在类路径下
ApplicationContext ioc=new ClassPathXmlApplicationContext("ioc2.xml");
Cat p=(Cat)ioc.getBean("catBeanFactory");
System.out.println(p);
}