1、使用类构造器进行实例化

<bean  />

测试代码:

@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		PersonIService personIService=(PersonIService)ac.getBean("personIService");
		personIService.helloSpring();
	}

2、使用静态工厂实例化

public class BeanFactory {
	public static PersonServiceImpl createPersonServiceImpl(){
		return new PersonServiceImpl();
	}
}
<bean  />


测试代码:

	@Test
	public void test3() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		PersonIService personIService=(PersonIService)ac.getBean("personIService2");
		personIService.helloSpring();
	}

3、使用实例化工厂实例化

public class BeanFactory {
	public PersonServiceImpl createPersonServiceImpl2(){
		return new PersonServiceImpl();
	}
}
	<bean />
	<bean  />


测试代码:

	@Test
	public void test4() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		PersonIService personIService=(PersonIService)ac.getBean("personIService3");
		personIService.helloSpring();
	}


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2022-12-23
猜你喜欢
  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
  • 2021-04-04
  • 2021-11-29
  • 2022-12-23
相关资源
相似解决方案