前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring4.0MVC学习资料,ApplicationContext中的方法详解(三)

Spring4.0MVC学习资料,ApplicationContext中的方法详解(三)

作者头像
业余草
发布2019-01-21 15:18:49
3870
发布2019-01-21 15:18:49
举报
文章被收录于专栏:业余草业余草

做为java开源的一部分,spring框架一直排在老大的位置。Spring4.0 是 Spring 推出的一个重大版本升级,进一步加强了 Spring 作为 Java 领域第一开源平台的地位。Spring4.0 引入了众多 Java 开发者期盼的新特性,如泛型依赖注入、SpEL、校验及格式化框架、Rest风格的 WEB 编程模型等。这些新功能实用性强、易用性高,可大幅降低 JavaEE 开发的难度,同时有效提升应用开发的优雅性。为了方便开发,Spring的ApplicationContext类,给我们提供了很多实用的方法,我在这里进行一下讲解。

看配置代码(applicationContext2.xml):

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
           
    <description>
    	herman
    </description>
    
    <alias name="nn" alias="abc"/>
    
	<bean class="com.herman.ss.pojo.Person"></bean>
	
	<bean id="person0" class="com.herman.ss.pojo.Person" name="a,b,c,d,e"></bean>
	
	<bean id="person1" class="com.herman.ss.pojo.Person" name="m,n">
		<property name="age" value="20"></property>
		<property name="name" value="herman"></property>
	</bean>
	
	<bean id="person2" class="com.herman.ss.pojo.Person">
		<property name="age">
			<value>20</value>
		</property>
		<property name="name">
			<value>herman</value>
		</property>
	</bean>
	
	<bean id="person3" class="com.herman.ss.pojo.Person">
		<constructor-arg name="name" value="herman"></constructor-arg>
		<constructor-arg name="age" value="20"></constructor-arg>
	</bean>
	
	<bean id="person4" class="com.herman.ss.pojo.Person">
		<constructor-arg type="int" value="20"></constructor-arg>
		<constructor-arg type="java.lang.String" value="herman"></constructor-arg>
	</bean>
	
	<bean id="house" class="com.herman.ss.pojo.House">
		<constructor-arg>
			<null></null>
		</constructor-arg>
		<constructor-arg name="address" value="Shanghai"></constructor-arg>
		<constructor-arg name="price" value="10000000.12"></constructor-arg>
	</bean>
	
	<bean id="person5" class="com.herman.ss.pojo.Person">
		<constructor-arg type="int" value="20"></constructor-arg>
		<constructor-arg type="java.lang.String" value="herman"></constructor-arg>
		<constructor-arg type="com.herman.ss.pojo.House" ref="house"></constructor-arg>
	</bean>
	
	<bean id="person6" class="com.herman.ss.pojo.Person">
		<constructor-arg type="int" value="20" index="1"></constructor-arg>
		<constructor-arg value="herman" index="0"></constructor-arg>
		<constructor-arg type="com.herman.ss.pojo.House" ref="house"></constructor-arg>
	</bean>
</beans>

在看测试代码:

代码语言:javascript
复制
package com.herman.ss.test;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.herman.ss.pojo.House;
import com.herman.ss.pojo.Person;

public class Test2 {
	
	/**
	 * @see 使用getBeansOfType获取bean对象集合
	 * @author Herman.Xiong
	 * @date 2014年8月6日15:38:10
	 */
	public static void test0(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
		Map<String, Person> map=ctx.getBeansOfType(Person.class);
		for (String key : map.keySet()) {
			System.out.println("key= "+ key + " and value= " + map.get(key));
		}
	}
	
	/**
	 * @see 使用containsBean判断bean对象是否存在
	 * @author Herman.Xiong
	 * @date 2014年8月6日15:40:18
	 */
	public static void test1(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
		System.out.println(ctx.containsBean("person0"));
	}
	
	/**
	 * @see 使用getBeanDefinitionCount统计定义bean对象的数量
	 * @author Herman.Xiong
	 * @date 2014年8月6日15:42:34
	 */
	public static void test2(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
		System.out.println(ctx.getBeanDefinitionCount());
	}
	
	/**
	 * @see 使用getBeanDefinitionNames获取定义的bean的名字
	 * @author Herman.Xiong
	 * @date 2014年8月6日15:45:50
	 */
	public static void test3(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
		String beanName []= ctx.getBeanDefinitionNames();
		for (int i = 0; i < beanName.length; i++) {
			System.out.println(beanName[i]);
		}
	}
	
	/**
	 * @see 根据bean名字获取bean的别名
	 * @author Herman.Xiong
	 * @date 2014年8月6日16:20:54
	 */
	public static void test4(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
		String[] aliases=ctx.getAliases("a");
		for (int i = 0; i < aliases.length; i++) {
			System.out.println(aliases[i]);
		}
		//person0,b,e,c,d
		/**
		 * 因为bean的名字由两部分组成:
		 * (1) 默认名字。当定义了bean的id属性时,默认名字为id属性的值;
		 *  未定义id时,取name属性的第一个值;id和name均未定义时,取类名。
		 * (2) 别名,除默认名字以外的其他名字。
		 */
	}
	
	/**
	 * @see isPrototype,isSingleton判断是否为多例,单例,原型
	 * @author Herman.Xiong
	 * @date 2014年8月6日16:25:56
	 */
	public static void test5(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
		System.out.println(ctx.isPrototype("person0"));
		System.out.println(ctx.isSingleton("person0"));
		System.out.println(ctx.isTypeMatch("person0", House.class));
	}
	
	/**
	 * @see 使用isTypeMatch方法判断bean对象是否为指定类型
	 */
	public static void test6(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
		System.out.println(ctx.isTypeMatch("person0", House.class));
	}
	
	/**
	 * @see 其他测试
	 */
	public static void test7(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
		System.out.println(ctx.getApplicationName());//模型的应用的名字
		System.out.println(ctx.getDisplayName());
		System.out.println(ctx.getStartupDate());
		System.out.println(ctx.findAnnotationOnBean("person0", Component.class));//返回对应的注解实例(参数指定了Bean的配置名称和需要返回的注解的类型)
		System.out.println(ctx.getBeanNamesForAnnotation(Component.class));//返回所有使用了对应注解annotationType的Bean
		/**
		 * ConfigurableBeanFactory 
			这个接口定义了设置父容器(ParentBeanFactory),设置类装载器,
			设置属性编辑器(PropertyEditorRegistrar)等一系列功能,增强了IoC容器的可定制性 
			AutowireCapableBeanFactory 
			定义了一些自动装配Bean的方法 
			SingletonBeanRegistry 
			这个接口没有继承BeanFactory,它主要定义了在运行期间向容器注册单例模式Bean的方法 
			BeanDefinitionRegistry 
			这个接口没有继承BeanFactory,它主要定义了向容器中注册BeanDefinition对象的方法
			在Spring配置文件中,每一个<bean>节点元素在Spring容器中都是由一个BeanDefinition对象描述的)
		 */
	}
	
	public static void main(String[] args) {
		//test0();
		//test1();
		//test2();
		//test3();
		//test4();
		//test5();
		//test6();
		test7();
	}
}

自己运行测试一把,是不是感觉很爽。

欢迎大家关注我的个人博客!!!!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014年08月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档