bean实例化的三种方式实现
第一种:使用类的无参数构造方法创建(常用 重要)
第一种实例化方式最常用,实例化类时会通过调用无参构造方法创建。示例代码如下:
package spring.com.UserService; public class UserService { public UserService() { //该方法是无参方法 } public void AddUser(){ System.out.println("Add........................."); } }
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-3.0.xsd"> <bean id="userService" class="spring.com.UserService.UserService"></bean> </beans>
单元测试:
package spring.com.Test; import static org.junit.Assert.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.com.UserService.UserService; public class Test { @org.junit.Test public void test() { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService s=(UserService) context.getBean("userService"); s.AddUser(); } }
结果:
方法正常执行
如果手动添加一个有参构造方法时,那么创建时会找不到无参构造方法;示例代码:
package spring.com.UserService; public class UserService { public UserService(String name) { //该方法是有参方法 } public void AddUser(){ System.out.println("Add........................."); } }
结果:
找不到无参构造方法,所以无法实例化。必须手动添加无参构造方法
package spring.com.UserService; public class UserService { public UserService() { //该方法是无参方法 } public UserService(String name) { //该方法是有参方法 } public void AddUser(){ System.out.println("Add........................."); } }
第二种:使用静态工厂创建(知道即可)
示例代码:
创建一个工厂类,改工厂用来创建一个类的实例
package spring.com.UserService; public class BeanFactory { public static UserService getBeanFactory(){ return new UserService(); } }
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-3.0.xsd"> <bean id="factory" class="spring.com.UserService.BeanFactory" factory-method="getBeanFactory"></bean><!--创建工厂创建对象--> </beans>
单元测试:
package spring.com.Test; import static org.junit.Assert.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.com.UserService.BeanFactory; import spring.com.UserService.UserService; public class Test { @org.junit.Test public void test() { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService s=(UserService) context.getBean("factory"); s.AddUser(); } }
结果:
第三种:使用实例工厂创建(知道即可)
示例代码:
package spring.com.UserService; public class BeanFactory { //普通工厂方法 public UserService getBeanFactory(){ return new UserService(); } }
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-3.0.xsd"> <bean id="bean" class="spring.com.UserService.BeanFactory"></bean> <bean id="factory" factory-bean="bean" factory-method="getBeanFactory"></bean> </beans>
单元测试:
package spring.com.Test; import static org.junit.Assert.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.com.UserService.BeanFactory; import spring.com.UserService.UserService; public class Test { @org.junit.Test public void test() { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService s=(UserService) context.getBean("factory"); s.AddUser(); } }
结果: