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 接口创建对象。

猜你喜欢

转载自www.cnblogs.com/maigy/p/10808408.html