• spring学习笔记之——注解


    spring注解可以很大的减少xml的配置,spring.xml文件的声明为:

    <?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
        <context:annotation-config />
    </beans>

    spring 3 支持几种不同的用于自动装配的注解:

    1、spring自带的@Autowired注解;

    2、JSR-330 的@Inject注解;(不属于spring不讲解)

    3、JSR-250 的@Resource注解(不属于spring不讲解)

    @Autowired注解:采用的是byType自动装配

    样例1:required=false表示:spring尝试装配instrument属性,但是,如果没有查找到与之匹配的类型为Instrument的Bean,应用就不会发生任何问题,而instrument属性会设置为null;若没有设置required=false当没有匹配的时候会报错

    @Autowired(required=false)
    private Instrument instrument;

    样例2:匹配bean的Id为person1的bean

    @Autowired
    @Qualifier("person1")
    private Person monitor;
    ...
    
    @Qualifier("person1")
    public class Monitor extends Person{
    ...
    
    @Qualifier("person2")
    public class Monitor2 extends Person{

    虽然<context:annotation-config>有助于完全消除spring配置中的<property>和<constructor-arg>元素,我们仍然需要使用<bean>元素显示定义Bean

    然而springy有另外一种方式<context:component-scan>元素,它除了完成与<context:annotation-config>元素相同的工作外,还允许spring自动检测Bean和定义Bean。这意味着不使用<bean>元素,spring应用中大多数或者说有的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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
    
        <context:component-scan base-package="action"></context:component-scan>
    
    </beans>

    <context:component-scan>元素查找使用构造型注解所标注的类,这些特殊注解如下:

    @Component——通用的构造性注解,标识该类为spring组件

    @Controller——标识将该类定义为spring mvc Controller

    @Repository——标识将该类定义为数据仓库

    使用@Component标注的任意自定义注解

    样例:

    package action;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    
    @Component("p") //若没有设定值p,则获取名字为personA
    public class PersonA {
        
        @Value("lily")
        private String name;
        @Value("15")
        private int age;
        public PersonA(){
            System.out.println("create person");
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
    }
    package spring.studye.spring.bean2;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import action.PersonA;
    import action.SpringIdolConfig;
    
    /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
            System.out.println( "begin" );
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
            PersonA p = (PersonA)ctx.getBean("p");//若没有设定值p,则ctx.getBean("personA")
            System.out.println("name = "+p.getName()+" age = "+p.getAge());
            System.out.println("end");
        }
    }

    可以对base-package下的组件进行过滤,采用<context:include-filter>和<context:exclude-filter>

    样例:要求派生于Person1的所有类自动注册为spring bean,要求派生于Person2的的不要自动注册为spring bean

    <context:component-scan base-package="action">
            <context:include-filter type="assignable" expression="Person1"/>
            <context:exclude-filter type="assignable" expression="Person2"/>
        </context:component-scan>

    过滤类型:

    annotation:过滤器扫描使用指定注解所标注的那些类,通过expression属性指定要扫描的注解

    assignable:过滤器扫描派生于expression属性所指定类型的那些类

    aspectj:过滤器扫描与expression属性所指定的AspectJ表达式所匹配的那些类

    custom:使用自定义的org.springframework.core.type.TypeFilter实现类,该类由expression属性指定

    regex:过滤器扫描类的名称与expression属性所指定的正则表达式所匹配的那些类

    最后说一个完全较大减少xml文件的注解:@Configuration

    <context:component-scan>会自动加载使用在base-package下的@Configuration注解标注的类

    样例:

    <?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
    
        <context:component-scan base-package="action">
            
        </context:component-scan>
    
    </beans>
    package action;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    
    public class PersonA {
        
        @Value("lily")
        private String name;
        @Value("15")
        private int age;
        public PersonA(){
            System.out.println("create person");
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
    }
    package spring.studye.spring.bean2;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    import action.PersonA;
    import action.SpringIdolConfig;
    
    public class App2 {
        public static void main(String[] args) {
            AnnotationConfigApplicationContext actx = new AnnotationConfigApplicationContext(SpringIdolConfig.class);
            PersonA p = (PersonA)actx.getBean("personA2");
            System.out.println(p.getName());
            p.setName("kange");
            PersonA p2 = (PersonA)actx.getBean("personA2");
            System.out.println(p2.getName());
        }
    }

    另外注意导入包:

    <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2</version>
        </dependency>

    AnnotationConfigApplicationContext 提供了三个构造函数用于初始化容器。

    AnnotationConfigApplicationContext():该构造函数初始化一个空容器,容器不包含任何 Bean 信息,需要在稍后通过调用其 register() 方法注册配置类,并调用 refresh() 方法刷新容器。
    AnnotationConfigApplicationContext(Class<?>... annotatedClasses):这是最常用的构造函数,通过将涉及到的配置类传递给该构造函数,以实现将相应配置类中的 Bean 自动注册到容器中。
    AnnotationConfigApplicationContext(String... basePackages):该构造函数会自动扫描以给定的包及其子包下的所有类,并自动识别所有的 Spring Bean,将其注册到容器中。它不但识别标注 @Configuration 的配置类并正确解析,而且同样能识别使用 @Repository、@Service、@Controller、@Component 标注的类。

  • 相关阅读:
    Linux 服务器 个人常用操作命令记录
    Thinkphp5.0 自定义命令command的使用
    vue初学之node.js安装、cnpm安装、vue初体验
    php实现在不同国家显示网站的不同语言版本
    array_map、array_walk、array_filter三个函数的区别
    实现简单点赞功能
    SQL语言-----数据操作
    SQL语言
    MySQL高可用架构之Keepalived+主从架构部署
    MyCAT源码分析——分析环境部署
  • 原文地址:https://www.cnblogs.com/tiramisuyj/p/4738080.html
Copyright © 2020-2023  润新知