您的位置:首页 > 房产 > 建筑 > 建筑网格布_百度logo在线设计生成器_全球热搜榜排名今日_发布平台

建筑网格布_百度logo在线设计生成器_全球热搜榜排名今日_发布平台

2025/2/27 12:27:20 来源:https://blog.csdn.net/qq_36400213/article/details/143942995  浏览:    关键词:建筑网格布_百度logo在线设计生成器_全球热搜榜排名今日_发布平台
建筑网格布_百度logo在线设计生成器_全球热搜榜排名今日_发布平台

Spring 框架提供了大量的注解来支持各种功能,以下是一些常用的 Spring 注解及其作用:

  1. @Component

    • 泛指Spring管理的组件,没有明确的边界。
    • 用法:@Component("myBean")
  2. @Service

    • 用于标注服务层(业务层)的组件。
    • 用法:@Service("myService")
  3. @Repository

    • 用于标注数据访问组件,即DAO组件。
    • 用法:@Repository("myRepository")
  4. @Controller

    • 用于标注控制层的组件,即Spring MVC中的控制器。
    • 用法:@Controller("myController")
  5. @Configuration

    • 用于定义配置类,可替换xml配置文件。
    • 用法:@Configuration
  6. @Bean

    • 在配置类中使用,声明一个bean。
    • 用法:@Bean public MyBean myBean() { return new MyBean(); }
  7. @Value

    • 注入外部配置文件中的值。
    • 用法:@Value("${property}") private String property;
  8. @Autowired

    • 自动导入依赖的bean。
    • 用法:@Autowired private MyBean myBean;
  9. @Qualifier

    • 当有多个同一类型的bean时,用于指定具体注入哪一个。
    • 用法:@Autowired @Qualifier("myBean") private MyBean myBean;
  10. @Scope

    • 定义bean的作用域。
    • 用法:@Scope("prototype")
  11. @PostConstruct@PreDestroy

    • 分别用于在bean创建后和销毁前执行的方法。
    • 用法:@PostConstruct public void init() { ... }
  12. @Lazy

    • 延迟加载bean,只有在第一次使用该bean时才初始化。
    • 用法:@Lazy @Autowired private MyBean myBean;
  13. @Profile

    • 指定组件在哪个环境的配置下才能被注册到容器中。
    • 用法:@Profile("dev") @Component public class DevComponent { ... }
  14. @PropertySource

    • 指定配置文件的位置。
    • 用法:@PropertySource("classpath:app.properties")
  15. @PropertySources

    • 指定多个配置文件的位置。
    • 用法:@PropertySources({@PropertySource("classpath:app.properties"), @PropertySource("classpath:db.properties")})
  16. @Import

    • 导入其他配置类。
    • 用法:@Import({Config1.class, Config2.class})
  17. @Aspect

    • 用于声明一个切面。
    • 用法:@Aspect @Component public class MyAspect { ... }
  18. @Before@After@Around@Pointcut

    • 用于定义切面的不同通知类型。
    • 用法:@Before("execution(* com.myapp.*.*(..))") public void beforeAdvice() { ... }
  19. @Transactional

    • 用于声明事务管理。
    • 用法:@Transactional public void myMethod() { ... }
  20. @EnableTransactionManagement

    • 用于开启基于注解的事务管理。
    • 用法:@Configuration @EnableTransactionManagement public class TransactionConfig { ... }

这些注解是 Spring 框架中非常核心和常用的,它们极大地简化了配置和开发过程。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com