spring-bean实例化
2021/8/14 23:07:52
本文主要是介绍spring-bean实例化,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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(); } }
这篇关于spring-bean实例化的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求