4.以注解方式开发bean

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注解使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容