码迷,mamicode.com
首页 > 编程语言 > 详细

Spring(六)—— 装配bean的三种方式

时间:2020-09-07 18:59:10      阅读:38      评论:0      收藏:0      [点我收藏+]

标签:code   factor   path   构造方法   void   create   inf   实现   装配bean   

所谓装配bean【对象】,就是在xml中写一个bean标签。

 

一、实例化Bean的三种方式(一)—— 使用构造方法实例化

技术图片

 

技术图片

 

 

二、实例化Bean的三种方式(二)—— 使用静态工法方法实例化

1. java代码实现静态工厂的方法

UserServiceFactory1.java

1 package com.gyf.service;
2 
3 public class UserServiceFactory1 {
4 
5     public static IUserService createUserService(){
6         return new UserServiceImpl();
7     }
8 }

 

Lesson03.java

 1 package com.gyf.test;
 2 
 3 import com.gyf.service.IUserService;
 4 import com.gyf.service.UserServiceFactory1;
 5 
 6 import org.junit.Test;
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 /**
11  * 装配bean的三种方式
12  */
13 public class Lesson03 {
14 
15     @Test
16     public void test2(){
17         /**
18          * 静态工厂
19          */
20         IUserService userservice2 = UserServiceFactory1.createUserService();
21         userservice2.add();
22     }
23 }

 

 

2. Spring的方式实现静态工厂

 beans3.xml

1 <!-- 第二种方式:通过静态工厂方法-->
2     <bean id="userService2" class="com.gyf.service.UserServiceFactory1" factory-method="createUserService"></bean>

 

Lesson03.java

 1 package com.gyf.test;
 2 
 3 import com.gyf.service.IUserService;
 4 import com.gyf.service.UserServiceFactory1;
 5 
 6 import org.junit.Test;
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 /**
11  * 装配bean的三种方式
12  */
13 public class Lesson03 {
14     @Test
15     public void test3(){
16 
17         /**
18          * 装配bean的第二种方式:使用静态工厂方法实例化
19          *
20           */
21         ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
22         IUserService userService3 = (IUserService) context.getBean("userService2");
23         userService3.add();
24     }
25 }

 

 

三、实例化Bean的三种方式(三)—— 使用实例工厂方法实例化

1. 使用java代码的实例工厂方法实例化bean

UserServiceFactory2.java

1 package com.gyf.service;
2 
3 public class UserServiceFactory2 {
4     public IUserService createUserService(){
5         return new UserServiceImpl();
6     }
7 }

 

Lesson03.java

 1 package com.gyf.test;
 2 
 3 import com.gyf.service.IUserService;
 4 import com.gyf.service.UserServiceFactory1;
 5 import com.gyf.service.UserServiceFactory2;
 6 
 7 import org.junit.Test;
 8 import org.springframework.context.ApplicationContext;
 9 import org.springframework.context.support.ClassPathXmlApplicationContext;
10 
11 /**
12  * 装配bean的三种方式
13  */
14 public class Lesson03 {
15     
16     @Test
17     public void test4(){
18         /**
19          * 实例化工厂
20          */
21         UserServiceFactory2 factory = new UserServiceFactory2();
22         IUserService userService4 = factory.createUserService();
23         userService4.add();
24     }
25 }

 

 

2. 使用 Spring框架的实例化工厂方法,实例化bean

beans.xml

1 <!--第三种方式:通过实例化工厂方法-->
2     <!--创建实例factory2 bean-->
3     <bean id="factory2" class="com.gyf.service.UserServiceFactory2"></bean>
4     <!--创建实例userService3 bean-->
5     <bean id="userService3" factory-bean="factory2" factory-method="createUserService"></bean>

Lesson03.java

1 @Test
2     public void test5(){
3         /**
4         * 装配bean的第三种方式:使用实例化工厂方法实例化
5         */
6         ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
7         IUserService userService5 = (IUserService) context.getBean("userService3");
8         userService5.add();
9     }

 

Spring(六)—— 装配bean的三种方式

标签:code   factor   path   构造方法   void   create   inf   实现   装配bean   

原文地址:https://www.cnblogs.com/upyang/p/13562635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!