public class HelloWorld {
public HelloWorld(){
System.out.println("aaaa");
}
public void hello(){
System.out.println("hello world");
}
}
public class HelloWorldFactory {
public static HelloWorld getInstance(){
return new HelloWorld();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--
在这个配置中,spring容器要用默认的构造函数为HelloWorld创建对象
-->
<bean id="helloWorld" class="HelloWorld"></bean>
<!--
采用静态工厂方法创建对象
factory-method为工厂方法
-->
<bean id="helloWorld2" class="HelloWorldFactory" factory-method="getInstance"></bean>
</beans>
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld world = (HelloWorld)context.getBean("helloWorld2");
world.hello();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有