springboot声明Bean的注解有

声明Bean的注解有:

  • @Component 没有明确角色的组件
  • @Service 在业务逻辑层(Service层)使用
  • @Repositpry 在数据访问层(dao层)使用
  • @Controller 用于标注控制层组件
  • @RestController

3. @Component注解

@Component源码:

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @since 2.5
 * @see Repository
 * @see Service
 * @see Controller
 * @see org.springframework.context.annotation.ClassPathBeanDefinitionScanner // 扫描包中Bean,注册
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {

    // 如果有返回组件名称,否则返回空字符串
    String value() default "";

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  1. @Component作用在类上
  2. @Component注解作用域默认为singleton
  3. 使用注解配置和类路径扫描时,被@Component注解标注的类会被Spring扫描并注册为Bean
  4. @Component使用在不确定哪一个层的时候使用,可以作用在任何层次,把普通pojo实例化到spring容器
  5. 不推荐使用@Component注解,而应该使用它的扩展,如@Service、@Repository

3.1 @Component注解使用

package com.example.demo.annotation;

public interface IUser {
    public String get();
}

package com.example.demo.annotation.component;

@Component
public class UserComponentImpl implements IUser {
    private String name = "UserComponentImpl";

    @Override
    public String get() {
        return name;
    }
}

//@Component("componentBeanId")
@Component(value="componentBeanId")
public class UserComponentImplWithParam implements IUser {
    private String name = "UserComponentImplWithParam";

    @Override
    public String get() {
        return name;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

@Component注解测试:

package com.example.demo.annotation.component;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.example.demo.annotation.IUser;

@SpringBootApplication
public class ComponentApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ComponentApplication.class, args);

        IUser userComponentImpl1 = (UserComponentImpl)context.getBean("userComponentImpl");
        System.out.println(userComponentImpl1.get());

        IUser userComponentImpl2 = (UserComponentImplWithParam)context.getBean("componentBeanId");
        System.out.println(userComponentImpl2.get());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

@Component注解测试结果: 
这里写图片描述

4. @Service注解

@Service注解源码:

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.annotation.AliasFor;

/**
 * @since 2.5
 * @see Component
 * @see Repository
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {

    @AliasFor(annotation = Component.class)
    String value() default "";

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  1. @Service是@Component注解的一个特例,作用在类上
  2. @Service注解作用域默认为singleton
  3. 使用注解配置和类路径扫描时,被@Service注解标注的类会被Spring扫描并注册为Bean
  4. @Service用于标注业务层组件,表示定义一个bean
  5. @Service使用时没有传参数,Bean名称默认为当前类的类名,首字母小写
  6. @Service(“serviceBeanId”)或@Service(value=”serviceBeanId”)使用时传参数,使用value作为Bean名字

4.1 @service注解使用

package com.example.demo.annotation;

public interface IUser {
    public String get();
}

package com.example.demo.annotation.service;

@Service
public class UserServiceImpl implements IUser {
    private final String name = "UserServiceImpl";

    public String get () {
        return name;
    }
}

//@Service("userService")
@Service(value="userService")
public class UserServiceImplWithParam implements IUser {
    private String name = "UserServiceImplWithParam";

    public String get() {
        return name;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

@Service注解测试:

package com.example.demo.annotation.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.example.demo.DemoApplication;
import com.example.demo.annotation.IUser;

@SpringBootApplication
public class ServiceApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);

        IUser serviceImpl1 = (UserServiceImpl) context.getBean("userServiceImpl");
        System.out.println(serviceImpl1.get());

        IUser serviceImpl2 = (UserServiceImplWithParam)context.getBean("userService");
        System.out.println(serviceImpl2.get());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

@Service注解测试结果:

说明:

  • @Service注解使用时不传参Bean名默认为当前类名,首字母小写
  • @Service注解使用时传参Bean名为参数value的值

这里写图片描述

posted @ 2020-09-17 22:15  博客新者  阅读(1091)  评论(0)    收藏  举报