美文网首页
4. 采用注解方式开发bean

4. 采用注解方式开发bean

作者: 叶小慈呀 | 来源:发表于2019-03-01 20:07 被阅读0次

    1.传统的Spring做法

    1.1两种做法

    • 使用.xml文件来对bean进行注入
    • 配置aop

    1.2缺点

    • 如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大;如果按需求分开.xml文件,那么.xml文件又会非常多。总之这将导致配置文件的可读性与可维护性变得很低。
    • 在开发中在.java文件和.xml文件之间不断切换,是一件麻烦的事,同时这种思维上的不连贯也会降低开发的效率。为了解决这两个问题,Spring引入了注解,通过"@XXX"的方式,让注解与Java Bean紧密结合,既大大减少了配置文件的体积,又增加了Java Bean的可读性与内聚性。

    2.Spring常用注解总结

    3.HelloWorld的例子改成用注解来实现

    • HelloWorld类,采用@Component注解
    package com.spring.annotation;
    /**
     * 采用注解开发的bean
     */
    import org.springframework.stereotype.Component;
    /**
     * @component用于类级别注解,标注本类为一个可被Spring容器托管的bean
     */
    @Component
    public class HelloWorld {
        public String getHello(){
            return  "Hello World";
        }
    }
    
    
    • HelloWorldApp类,采用@ComponentScan注解
    package com.spring.annotation;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    /**@ComponentScan
     * 用于寻找用@ComponentScan注解标注的bean
     */
    @ComponentScan
    public class HelloWorldApp {
        public static void main(String[] args) {
            //1.通过注解创建上下文
            ApplicationContext context = new AnnotationConfigApplicationContext     (HelloWorldApp.class);
            //2.读取bean
            HelloWorld hello = context.getBean(HelloWorld.class);
            //3.运行
            System.out.println(hello.getHello());
      }
    }
    
    • 运行结果
    运行结果

    4.Student和Phone改成注解形式

    • Lombok插件的使用
      1)Settings->plugins,搜索Lombok,安装,重启IDEA
      2)添加依赖
      3)使用@Data注解,简化POJO类,不用再写getter/setter,toString()构造方法了
    <!--Lombok依赖-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.18</version>
                <scope>provided</scope>
            </dependency>
    
    • Phone类
    package com.spring.annotation;
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    /**
     * 采用注解和Lombok开发的Phone类
     */
    @Component
    @Data
    public class Phone {
        //通过@Value注解给简单类型赋值
        @Value("三星")
        private  String brand;
        @Value("4000.06")
        private double prize;
    }
    
    • Student类
    package com.spring.annotation;
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    @Component
    @Data
    public class Student {
        @Value("Lily")
        private String name;
        @Value("20")
        private int age;
        //引用类型,通过@Autowired注入Phone的bean
        @Autowired
        private Phone phone;
    }
    
    • StudentApp类
    package com.spring.annotation;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    @ComponentScan
    public class StudentApp {
        public static void main(String[] args) {
            ApplicationContext context = new AnnotationConfigApplicationContext  (StudentApp.class);
            Student student = context.getBean(Student.class);
            System.out.println(student);
        }
    }
    
    • 运行结果


      运行结果

    相关文章

      网友评论

          本文标题:4. 采用注解方式开发bean

          本文链接:https://www.haomeiwen.com/subject/nojjuqtx.html