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的值

這里寫圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM