0%

SPRING3–动态工厂和静态工厂

1.xml比较 区别:factory-bean class

1
2
3
4
5
6
7
<!–动态工厂–>
<bean id=”beanServiceFactory” class=”com.bean.service1.beanServiceFactory”></bean>
<bean id=”beanServiceImpl” factory-bean=”beanServiceFactory” factory-method=”add”>
</bean>

<!–静态工厂–>
<bean id=“beanServiceImpl” class=“com.bean.service1.beanServiceFactory” factory-method=“update”></bean>

2.service比较

动态service:

1
2
3
4
5
6
7
8
9
public interface beanService1 {  
public beanServiceImpl add();
}
public class beanServiceFactory implements beanService1{
//动态方法
public beanServiceImpl add() {
return new beanServiceImpl();
}
}

静态service:

1
2
3
4
5
6
public class beanServiceFactory{
//静态方法
public static beanServiceImpl update() {
return new beanServiceImpl();
}
}

3.实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//动态工厂
@Test
public void test2() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
beanServiceImpl beanServiceImpl= (beanServiceImpl) applicationContext.getBean(“beanServiceImpl”);
System.out.println(beanServiceImpl.getId());
}
//静态动态工厂
@Test
public void test3() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
beanServiceImpl beanServiceImpl= (beanServiceImpl) applicationContext.getBean(“beanServiceImpl”);
System.out.println(beanServiceImpl.getId());
}