zoukankan      html  css  js  c++  java
  • spring知识点

    一、获取spring容器

    1、Spring中,管理XML配置Bean定义的容器:ApplicationContext context = new ClassPathXmlApplicationContext("application.xml")。通过容器可以获取bean,context .getBean("beanId")。

    2、Spring中,管理注解Bean定义的容器有两个:AnnotationConfigApplicationContext和AnnotationConfigWebApplicationContex。这两个类是专门处理Spring注解方式配置的容器,直
    接依赖于注解作为容器配置信息来源的IOC容器。AnnotationConfigWebApplicationContext是AnnotationConfigApplicationContext的web版本,两者的用法以及对注解的处理方式几乎。

    二、bean配置与管理

    2.1、spring xml配置文件:配置bean

    <?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.2、从Spring3.0,@Configuration用于定义配置类。配置bean相当于一个spring xml配置文件

    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(); } }

    三、自定义注解

    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.1、获取指定注解所有的 Bean

    Map<String,Object> objectMap = applicationContext.getBeansWithAnnotation(Service.class);

    4.2、获取指定注解所有的 Bean 的名字

    String[] beanNames = applicationContext.getBeanNamesForAnnotation(Service.class);
  • 相关阅读:
    Jedis客户端以及redis中的pipeline批量操作
    Redis5.x两种持久化方式以及主从复制配置
    博客园原创文章防剽窃、反爬虫指南(持续更新.....)
    【金主打赏榜】
    Swift LeetCode 目录 | Catalog(每周日更新......)
    [Swift]SkeletonView:在UITableView中使用骨架屏
    【Xcode】加快Xcode编译调试速度
    【Xcode】ITMS-90809:查找UIWebView
    [Swift]PhotoKit-照片框架
    [SourceTree]remote: HTTP Basic: Access denied
  • 原文地址:https://www.cnblogs.com/chenweichu/p/10781113.html
Copyright © 2011-2022 走看看