关于@Component注解的含义说明

目录
  • 一、注解分类
  • 二、@Component 注解含义
  • 三、基于@Component扩展的注解
  • 四、component的扫描组件
  • 五、方法初始化和销毁
    • 1. @PostConstruct注解
    • 2. @PreDestroy注解
    • 3. 示例

一、注解分类

1、@controller:controller控制器层(注入服务)

2、@service:service服务层(注入dao)

3、@repository:dao持久层(实现dao访问)

4、@component:标注一个类为Spring容器的Bean,(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)

二、@Component 注解含义

@Component: 标注Spring管理的Bean,使用@Component注解在一个类上,表示将此类标记为Spring容器中的一个Bean。

三、基于@Component扩展的注解

@Repository

@Repository本身是基于@Component注解的扩展,被@Repository注解的POJO类表示DAO层实现,从而见到该注解就想到DAO层实现,使用方式和@Component相同;

@Service

@Service本身是基于@Component注解的扩展,被@Service注解的POJO类表示Service层实现,从而见到该注解就想到Service层实现,使用方式和@Component相同;

@Controller

@Controller本身是基于@Component注解的扩展,被@Controller注解的类表示Web层实现,从而见到该注解就想到Web层实现,使用方式和@Component相同;

Tips:使用@Component,@Service,@Controller,@Repository注解的类,表示把这些类纳入到spring容器中进行管理,同时也是表明把该类标记为Spring容器中的一个Bean。

四、component的扫描组件

下面写这行配置是引入component的扫描组件

<context:component-scan base-package=”com.mmnc”> 

其中base-package为指定需要扫描的包(含所有子包) ,扫描被@Service、@Controller、@Repository、@Component等注解标注的Java类,将其扫描注入到Spring容器,注入成Bean:

  • @Service用于标注业务层组件
  • @Controller用于标注控制层组件(如struts中的action)
  • @Repository用于标注数据访问组件,即DAO组件.
  • @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注,标识为一个Bean。

五、方法初始化和销毁

从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解: @PostConstruct和@PreConstruct ,这两个注解被用来修饰一个非静态的void()方法,而且这个方法不能有抛出异常声明,标注方法的初始化和销毁,当你需要在系统启动时提前设置一下变量或者设置值操作时,可以使用@PostConstruct注解进行项目启动时设置来完成,当你需要处理关闭资源或者发送通知相关操作时可以使用@PreConstruct 完成。

1. @PostConstruct注解

被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。

2. @PreDestroy注解

被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。

加载顺序如图:

在Spring中可以使用以下方式指定初始化方法和销毁方法(方法名任意):

//对象创建并赋值之后调用
@PostConstruct
public void init() {
  System.out.println("对象创建并赋值之后调用...");
} 

//容器移除对象之前
@PreDestroy
public void detory(){
 System.out.println("容器移除对象之前执行...");
}

3. 示例

@Controller
@Lazy(false)
public class ServletInit {
 @Autowired
 private PCASigninServlet  pcaSigninServlet;
 @Autowired
 private PCASignoutServlet pcaSignoutServlet;
 @Autowired
 private PCAInitServlet pcaInitServlet;
 @Autowired
 private PCALogoutServlet pcaLogoutServlet;
 @Autowired
 private PCAInfoServlet pcaInfoServlet;
 @Autowired
 private HelloServlet helloServlet;
 @Autowired @Qualifier("handler")
 private WebAppContext webapp; 

 @PostConstruct
 void init(){
      Server server = new Server(8848);
         ServletContextHandler contextHander = new ServletContextHandler(ServletContextHandler.SESSIONS);
         contextHander.setContextPath("/");
         System.out.println("start init");
         HandlerCollection hc =new HandlerCollection();
         hc.setHandlers(new Handler[]{webapp, contextHander});
         server.setHandler(hc);
         System.out.println("addServlet /hello");
         contextHander.addServlet(new ServletHolder(helloServlet), "/hello");
         contextHander.addServlet(new ServletHolder( pcaSigninServlet), "/pca/signin");
            contextHander.addServlet(new ServletHolder( pcaSignoutServlet), "/pca/signout");
            contextHander.addServlet(new ServletHolder( pcaInitServlet), "/pca/init");
            contextHander.addServlet(new ServletHolder( pcaLogoutServlet), "/pca/logout");
            contextHander.addServlet(new ServletHolder( pcaInfoServlet), "/pca/info");
         try
   {
          System.out.println("server start");
    server.start();
    server.join();
    System.out.println("server stop");
   } catch (Exception e)
   {
    e.printStackTrace();
   }
 }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Spring注解开发@Bean和@ComponentScan使用案例

    组件注册 用@Bean来注册 搭建好maven web工程 pom加入spring-context,spring-core等核心依赖 创建实例类com.hjj.bean.Person, 生成getter,setter方法 public class Person { private String name; private int age; } 创建com.hjj.config.MainConfig @Configuration //告诉spring是一个配置类 public class Main

  • springboot @ComponentScan注解原理解析

    这篇文章主要介绍了springboot @ComponentScan注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 @ComponentScan 告诉Spring从哪里找到bean. 如果你的其他包都在@SpringBootApplication注解的启动类所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了. 如果你有一些bean所在的包,不在启动类的包及其下级包,那么你需要手动加上@Compone

  • @ComponentScan注解用法之包路径占位符解析

    目录 代码测试 底层行为分析 总结 @ComponentScan注解的basePackages属性支持占位符吗? 答案是肯定的. 代码测试 首先编写一个属性配置文件(Properties),名字随意,放在resources目录下. 在该文件中只需要定义一个属性就可以,属性名随意,值必须是要扫描的包路径. basepackages=com.xxx.fame.placeholder 编写一个Bean,空类即可. package com.xxx.fame.placeholder; import org

  • @Bean注解和@Configuration、@Component注解组合使用的区别

    目录 一.@Bean的"full"模式和"lite"模式 二.两种模式的差异 1."full"模式下@Bean方法互相调用 2."lite"模式下@Bean方法互相调用 三.总结 一.@Bean的"full"模式和"lite"模式 在一般常见情况下,@Bean注解在@Configuration类中声明,称之为"full"模式:当@Bean注解和@Component注解

  • 聊聊注解@controller@service@component@repository的区别

    目录 注解@controller@service@component@repository的区别 命名不一样主要是为了区分类的作用和所属层级: Spring中的主要注解 1. 组件类注解@Component.@Repository.@Service.@Controller[创建注解] 1.@Component 标注为一个普通的spring Bean类 2.@Repository 标注为一个DAO层的组件类 3.@Service 标注为Service层(业务逻辑层)的组件类 4.@Controll

  • spring @Component注解原理解析

    这篇文章主要介绍了spring @Component注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.@controller 控制器(注入服务) 2.@service 业务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>) 5.@Comp

  • 一个@Component注解引发的大坑

    目录 一个@Component注解引发的大坑 问题是这样的 思考 对spring @component注解的理解 @Component注解的使用 注解的类 测试的类 一个@Component注解引发的大坑 首先,我们这个是用springboot架构来实现的业务 这是项目包结构和配置文件结构 这是定时需要执行的任务 这是我执行PromoCodeCMCJob这个定时器的报错信息 问题是这样的 加了两个MQ之后才报错的这个信息,当我执行启动PromoCodeCMCJob定时任务的时候就报错,报错信息如

  • 基于@ComponentScan注解的使用详解

    目录 @ComponentScan注解的使用 一.注解定义 二.使用 1.环境准备 2.excludeFilters的使用 3.includeFilters的使用 4.自定义过滤规则 关于@ComponentScan注解的一些细节 @ComponentScan注解的使用 一.注解定义 @ComponentScan注解的定义如下: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Repeatabl

  • 关于@Component注解的含义说明

    目录 一.注解分类 二.@Component 注解含义 三.基于@Component扩展的注解 四.component的扫描组件 五.方法初始化和销毁 1. @PostConstruct注解 2. @PreDestroy注解 3. 示例 一.注解分类 1.@controller:controller控制器层(注入服务) 2.@service:service服务层(注入dao) 3.@repository:dao持久层(实现dao访问) 4.@component:标注一个类为Spring容器的Be

  • Spring @bean和@component注解区别

    目录 Spring 中的一些注解 2. Autowire 和 @Resource 的区别 3. 将一个类声明为 Spring 的 bean 的注解有哪些? 4. @Configuration :配置类注解 5. @ControllerAdvice :处理全局异常利器 6. @Component, @Repository, @Service 的区别 总结 本文打算介绍几个不太容易说出其区别,或者用途的 Spring 注解,比如 @Component 与 @Bean 的比较,@ControllerA

  • springboot中@component注解的使用实例

    目录 @component注解的使用 方式一 方式二 @component注解有什么作用 用一句话概括 @component注解的使用 配置响应头的内容. 方式一 直接在拦截器里配置响应头内容. /**  * 拦截器--用户身份确认.  */ public class RestInterceptor implements HandlerInterceptor {         private static final Logger log = LoggerFactory.getLogger(R

  • 关于@Component注解下的类无法@Autowired问题

    目录 @Component注解下类无法@Autowired 这个问题心累 @Component注解下@Autowired报错 下面是我的解决方案 @Component注解下类无法@Autowired 这个问题心累 在把我的一个非Web程序迁移从Spring迁移到SpringBoot时,出现了在@Component注解下@Autowired的类为null的情况,也就是没注入成功,或者说是此类在bean加载之前就被调用了. 试了各种办法,修改扫描包,修改@Component注解等等,皆未成功,后来看

  • 浅谈spring 常用注解

    我们不妨先将spring常用的注解按照功能进行分类 1 .将普通类加入容器形成Bean的注解 日常开发中主要使用到的定义Bean的注解包括(XML方式配置bean暂不讨论): @Component.@Repository.@Service.@Controller.@Bean 其中@Component.@Repository.@Service.@Controller实质上属于同一类注解,用法相同,功能相同,区别在于标识组件的类型.当一个组件代表数据访问层(Dao)时,你可以给它加上@Reposit

  • 快速理解spring中的各种注解

    Spring中的注解大概可以分为两大类: 1)spring的bean容器相关的注解,或者说bean工厂相关的注解: 2)springmvc相关的注解. spring的bean容器相关的注解,先后有:@Required, @Autowired, @PostConstruct, @PreDestory,还有Spring3.0开始支持的JSR-330标准javax.inject.*中的注解(@Inject, @Named, @Qualifier, @Provider, @Scope, @Singlet

随机推荐