1、获取spring容器java
一、Spring中,管理XML配置Bean定义的容器:ApplicationContext context = new ClassPathXmlApplicationContext("application.xml")。经过容器能够获取bean,context .getBean("beanId")。web
二、Spring中,管理注解Bean定义的容器有两个:AnnotationConfigApplicationContext和AnnotationConfigWebApplicationContex。这两个类是专门处理Spring注解方式配置的容器,直
接依赖于注解做为容器配置信息来源的IOC容器。AnnotationConfigWebApplicationContext是AnnotationConfigApplicationContext的web版本,二者的用法以及对注解的处理方式几乎。spring
2、bean配置与管理app
2.一、spring xml配置文件:配置beanspa
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="duke" class="com.springinaction.springidol.Juggler" > <!-- 经过构造方法设置属性值 --> <constructor-arg value="15"></constructor-arg> </bean> </beans>
2.二、从Spring3.0,@Configuration用于定义配置类。配置bean至关于一个spring xml配置文件code
package com.dxz.demo.configuration; import org.springframework.context.annotation.Configuration; @Configuration public class TestConfiguration {
@Bean public Juggler duke() { return new com.springinaction.springidol.Juggler(); } }
3、自定义注解xml
import java.lang.annotation.*; @Documented @Inherited @Target({ ElementType.FIELD, ElementType.METHOD ,ElementType.TYPE_USE,ElementType.ANNOTATION_TYPE,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface RedisHandel { String key() default ""; String keyField() default "username"; }
4、获取自定义注解blog
4.一、获取指定注解全部的 Beanget
Map<String,Object> objectMap = applicationContext.getBeansWithAnnotation(Service.class);
4.二、获取指定注解全部的 Bean 的名字it
String[] beanNames = applicationContext.getBeanNamesForAnnotation(Service.class);