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

4.以注解方式开发bean

作者: sherlockwit_孙鸣 | 来源:发表于2019-03-01 21:10 被阅读0次

    1.以Hello类为例改为用注解方式

    • Hello类,采用@Component注解:

      import org.springframework.stereotype.Component;
      
      /**
       * Created by Administrator on 2019/2/28.
        * @Component用于类级别注解,标注本类为一个可被Spring容器托管的
              */
          @Component
            public class Hello {
               public String gethello(){
                  return "Hello World";
       }
      }
      
    • HelloApp类,则采用@ComponentScan来注解:

      import org.springframework.context.ApplicationContext;
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.stereotype.Component;
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      
      /**
       * Created by Administrator on 2019/2/28.
       * @Component
       */
      
      @Component
      public class HelloApp {
          public static void main(String[] args) {
              //1.通过注解创建上下文对象
              ApplicationContext context = new AnnotationConfigApplicationContext(HelloApp.class);
              //2.读取bean
              Hello hello = context.getBean(Hello.class);
              //3.运行
              System.out.println(hello.gethello());
          }
      }
      
    • 运行结果为:


      运行结果

    2.以Student和Phone为例

    • 首先学会如何使用Lombok插件
    • 按照下面步骤安装并记得需要重启IDEA


      File-Settings...
    找到Plugins
    • 接下来添加依赖:

      <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>
      
    • Phone类

      import lombok.Data;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;
      
      /**
       * Created by Administrator on 2019/2/28.
       * 采用注解和Lombok开发的Phone类
       */
      @Component
      @Data
      public class Phone {
          @Value("华为")
          private String brand;
      
          @Value("1888.88")
          private double price;
      }
      
    • Student类

      import lombok.Data;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;
      
      /**
       * Created by Administrator on 2019/2/28.
       */
      @Component
      @Data
      public class Student {
          @Value("Sunming")
          private String name;
      
          @Value("21")
          private int age;
      
          //注入@Autowired注入
          @Autowired
          private Phone phone;
      }
      
    • StudentApp类:

      import org.springframework.context.ApplicationContext;
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      import org.springframework.context.annotation.ComponentScan;
      
      /**
       * Created by Administrator on 2019/2/28.
       */
      @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);
          }
      }
      
    • 运行结果:


      运行结果

    也可以看一下Spring的@Bean注解使用,加深理解。
    Spring的@Bean注解使用

    相关文章

      网友评论

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

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