美文网首页
采用注解开发bean

采用注解开发bean

作者: 国王兔子 | 来源:发表于2019-03-02 12:52 被阅读0次

1.Hello用注解来实现

  • Hello类,采用@Component注解
package com.spring.annotation;

import org.springframework.stereotype.Component;

/**
 *采用注解开发bean
 * @Component用于类级别注解
 */
@Component
public class Hello {
    public String getHello(){

        return "Hello Word";
    }
}
  • HelloApp类,采用@ComponentScan注解
package com.spring.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
public class HelloApp {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(HelloApp.class);
        HelloWorld  helloWorld = context.getBean(HelloWorld.class);
        System.out.println(helloWorld.getHello());
    }
}

  • 运行结果

    image

2.Student和Phone的例子用注解实现

  • Lombok插件的使用
    1)Settings->plugins,搜索Lombok,安装,重启IDEA
    2)添加依赖
       <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>

  1. 使用@Data注解
  • Phone类
package com.spring.annotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 采用注解Lombok开发的Phone类
 * Created by HP on 2019/2/28.
 */
@Component
public class Phone {
    @Value("iPhoneX")
    private String brand;
    @Value("6666.6")
    private double price;

}
  • 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;

/**
 * Created by HP on 2019/2/28.
 */
@Component
@Data
public class Student {
    @Value("Tom")
    private String name;
    @Value("21")
    private String age;
    @Autowired
    private Phone phone;



  • StuentApp类
package com.spring.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by HP 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);
}
}
  • 运行结果

    image

相关文章

  • 采用注解方式开发bean

    以注解实现Student和Phone为例 Lombok插件的使用①File->Settings->plugins,...

  • 采用注解开发bean

    1.Hello用注解来实现 Hello类,采用@Component注解 HelloApp类,采用@Componen...

  • 采用注解方式开发bean

    创建一个Hello类,采用@Component注解 HelloApp类,采用@ComponentScan注解 运行...

  • spring的bean的注解

    bean可以xml配置,也可以采用注解,注解更加简单,注解也需要配置相关的xml文件。因为bean的装配,采用注解...

  • 6 采用注解方式开发bean

    Spring从3.0开始使用注解,到Spring5.0,已经大量使用,注解的使用可以省却大量的xml文件 1. H...

  • Spring:采用注解开发bean

    传统的Spring使用.xml文件来对bean进行注入或者是配置aop,这么做导致.xml文件变得十分庞大,导致配...

  • 4 采用注解方式开发bean

    1. Hello的例子改成用注解来实现 创建一个Hello类,采用@Component注解image HelloA...

  • 4 采用注解方式开发bean

    1. Hello的例子改成用注解来实现 创建一个Hello类,采用@Component注解 HelloApp类,采...

  • 02 注解方式开发bean

    采用注解方式开发bean可以省去get、set构造方法同时不需要在xml去添加bean的属性1.Component...

  • 06-采用注解开发bean

    对于java bean的定义和依赖配置,使用xml文件真心是不方便。到Spring5.0,已经大量使用,注解的使用...

网友评论

      本文标题:采用注解开发bean

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