Spring的一些標簽說明


1、alias 標簽:

作用:為已配置的 bean 設置別名

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6         
 7     <bean id="user" name="test" class="cn.mgy.entity.User"/>
 8     
 9     <!-- 
10         標簽alias: 為已配置的bean設置別名
11             屬性name: 必要屬性, 代表為哪一個bean配置別名, 
12                 此屬性的值為其他bean標簽的id或name屬性值
13             屬性alias: 必要屬性, 代表新命名的別名是什么
14      -->
15     <alias name="user" alias="user1"/>
16             
17 </beans>

測試代碼:

 1 import org.springframework.context.ApplicationContext;
 2 import org.springframework.context.support.ClassPathXmlApplicationContext;
 3 
 4 import cn.mgy.entity.User;
 5 
 6 public class Test {
 7     public static void main(String[] args) {
 8         // 讀取Spring配置文件
 9         ApplicationContext context = new ClassPathXmlApplicationContext(
10                 "applicationContext.xml");
11 
12         // 通過id獲取User對象
13         User user = (User) context.getBean("user");
14         // 測試對象
15         System.out.println(user);
16         System.out.prntln("=====================================");
17         // 通過alias獲取User對象
18         user = (User) context.getBean("user1");
19         // 測試對象
20         System.out.println(user);
21     }
22 }

2、bean 標簽的配置:

  bean 標簽的作用:用於聲明一個類,在啟動 Spring 框架的時候根據該配置的類創建對象到容器里面。

  屬性說明:

 1 <!-- <bean>標簽:用於聲明一個類,在啟動Spring框架的時候根據該配置的類創建對象到容器里面
 2          name:設置對象名(唯一標識符)
 3          id:設置對象名(唯一標識符,功能和name一樣)
 4          class:用於指定對象對應的類名,如果不是實現類必須要將bean聲明為抽象的!!!
 5          scope:用於設置的對象的作用范圍,可選參數如下:
 6             *singleton:單例(默認)
 7                 對象出生:當程序加載配置文件創建容器時,創建
 8                 對象活着:只要容器還在,一直活着
 9                 對象死亡:應用停止,容器銷毀,對象死亡
10             *prototype:多例(原型對象)
11                 對象出生:當程序加載配置文件創建容器時,創建,(每次調用會創建一個新對象)
12                 對象活着:只要對象被使用,一直活着
13                 對象死亡:對象長時間不用,會被Java垃圾回收機制回收 (該對象不被容器管理)
14             *reqeust:web項目中,Spring將創建的對象放在request作用域中
15             *session:web項目中,Spring將創建的對象放在session作用域中
16         init-method:設置創建對象的時候,調用初始化方法
17         destroy-method:設置對象被回收時,調用注銷的方法
18         
19       -->
20     <bean name="customerServiceImpl"  class="cn.mgy.service.impl.CustomerServiceImpl"></bean>

3、實例化 Bean 的四種方式:

(1)通過 class 直接創建

   1 <bean name="customerService" class="cn.mgy.service.CustomerService"> 

(2)通過靜態方法工廠創建

 1 package cn.mgy.factory;
 2 
 3 import cn.mgy.service.CustomerService;
 4 
 5 public class CreateFactory {
 6 
 7     /**
 8      * 使用一個靜態工廠類,通過字符串創建一個對象
 9      * 
10      * @param className
11      * @return
12      */
13     public static Object create() {
14 
15         return new CustomerService();
16 
17     }
18 
19 }
1 package cn.mgy.factory;
2 public class CustomerService {
3      void say();  
4 }
1 package cn.mgy.factory;
2 
3 
4 public class CustomerServiceImpl implements CustomerService {
5         @Override
6          public void say() {
7                 System.out.println("你好世界!");
8          }    
9 }

  靜態工廠配置:

1  <bean name="customerService" factory-method="create" class="cn.mgy.factory.CreateFactory">
2      <property name="customerDAO" ref="customerDAOImpl"></property>
3  </bean>

  (3)通過實體工廠創建

  實體工廠,注意 create 方法沒有 static

 1 package cn.mgy.factory;
 2 
 3 import cn.mgy.service.CustomerService;
 4 
 5 public class CreateFactory {
 6 
 7     /**
 8      * 使用一個靜態工廠類,通過字符串創建一個對象
 9      * 
10      * @param className
11      * @return
12      */
13     public  Object create() {
14 
15         return new CustomerService();
16 
17     }
18 
19 }

  配置方式:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <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-4.2.xsd ">
3    <bean  name="customerDAOImpl" class="cn.mgy.dao.impl.CustomerDAOImpl"></bean>
4    <!--非靜態的方法,必須需要對象來調用,所以必須要創建工廠類的對象 -->
5    <bean name="createFactory" class="cn.mgy.factory.CreateFactory" ></bean>
6    <bean name="customerService" factory-method="create" factory-bean="createFactory" >
7      <property name="customerDAO" ref="customerDAOImpl"></property>
8    </bean>
9 </beans>

  (4)內置 FactoryBean 工廠創建對象的實現

    Spring 支持一種通過實現 FactoryBean 的接口創建工廠類對象。必須返回泛型指定的類型對象。

 1 public class HelloWorldServiceFactory implements FactoryBean<HelloWorldService> {
 2 
 3     /**
 4      * 返回創建的對象
 5      */
 6     @Override
 7     public HelloWorldService getObject() throws Exception {
 8         return new HelloWorldService();
 9     }
10 
11     /**
12      * 返回對象的類型
13      */
14     @Override
15     public Class<?> getObjectType() {
16         return HelloWorldService.class;
17     }
18 
19     /**
20      * 是否是單例,如果是true ,否則就是false
21      */
22     @Override
23     public boolean isSingleton() {
24         return false;
25     }
26     
27 }

  調用代碼:

 1 package cn.mgy.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.beans.BeansException;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import cn.mgy.service.HelloWorldService;
 8 
 9 public class HelloWorldTest {
10     
11     @Test
12     public void say() {
13     
14             try {
15                 //Spring框架的路徑格式:如果從包里面開始讀取的路徑,使用classpath開始
16                 ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
17                 //對象名必須要和配置的對象名一一對應
18                 HelloWorldService helloWorldService = applicationContext.getBean("helloWorldService", HelloWorldService.class);
19                 helloWorldService.say();
20                 applicationContext.close();
21             } catch (BeansException e) {
22                 // TODO Auto-generated catch block
23                 e.printStackTrace();
24             }
25     
26     }
27 
28 }

  FactoryBean 創建對象的應用場景,有這么一種情況,創建好的對象可以設置很多需要的參數再返回。如果我們創建一個對象需要設置很多參數的話,我們可以通過一個工廠類創建對象,這樣就可以設置很多的參數。這樣也就實現了將參數和對象打包了。

  我們經常看見框架整合的時候,會看到 FactoryBean 接口創建對象。因為框架調用的時候經常涉及參數與對象綁定在一起,所以使用 FactoryBean 接口創建對象。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM