bean的实例化有3种方式:
方式一:
默认构造方式:表示调用类的默认构造方法创建bean对象。
<bean id="" class=""></bean>
目录结构:
创建UserService:
package com.spring02.beanbox;
public class UserService {
public void saveUser() throws Exception{
System.out.println("UserService.saveUser()");
}
}
创建bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="UserService" class="com.spring02.beanbox.UserService"></bean>
</beans>
创建测试类:
package com.spring02.beanbox;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
@Test
public void test() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("com/spring02/beanbox/bean.xml");
UserService userService = (UserService) ac.getBean("UserService");
userService.saveUser();
}
}
方式二:
静态工厂方式:有一个工厂类,在类中所有方法都是静态的,作用生产目标类对象。创建工厂类,定义静态方法生产需要的bean对象。
配置:<bean. id="". class="静态工厂bean". factory-method="工厂方法"></bean>
目录结构:
创建UserService:
package com.spring02.beanbox;
public class UserService {
public void saveUser() throws Exception{
System.out.println("UserService.saveUser()");
}
}
创建静态工厂bean:MyStaticFactoryBean
package com.spring02.beanbox;
public class MyStaticFactoryBean {
private static UserService userService;
public static UserService createUserService(){
if (userService == null){
userService = new UserService();
}
return userService;
}
}
创建bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="UserService2" class="com.spring02.beanbox.MyStaticFactoryBean" factory-method="createUserService"></bean>
</beans>
创建测试类:
package com.spring02.beanbox;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
@Test
public void test() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("com/spring02/beanbox/bean.xml");
UserService userService = (UserService) ac.getBean("UserService2");
userService.saveUser();
}
}
方式三:
实例工厂方式:有一个工厂类,在类中所有方法都是成员方法,作用生产目标类对象。创建工厂类,定义成员方法生产需要的bean对象。
配置文件中:先配置示例工厂bean,再利用工厂bean生产目标bean对象。
<bean id="" factory-bean="" factory-method=""></bean>
创建UserService:
package com.spring02.beanbox;
public class UserService {
public void saveUser() throws Exception{
System.out.println("UserService3.saveUser()");
}
}
创建实例工厂MyObjectFactoryBean:
package com.spring02.beanbox;
public class MyObjectFactoryBean {
private static UserService userService;
public UserService createUserService(){
if(this.userService == null){
this.userService = new UserService();
}
return userService;
}
}
创建bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置工厂bean -->
<bean id="MyFactoryBean" class="com.spring02.beanbox.MyObjectFactoryBean"></bean>
<!-- 利用工厂bean生产目标bean -->
<bean id="UserService3" factory-bean="MyFactoryBean" factory-method="createUserService"></bean>
</beans>
创建测试类:
package com.spring02.beanbox;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
@Test
public void test() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("com/spring02/beanbox/bean.xml");
UserService userService = (UserService) ac.getBean("UserService3");
userService.saveUser();
}
}