如何使用注解而不是XML定义Spring bean? [英] How to define a Spring bean using annotation instead of XML?

查看:102
本文介绍了如何使用注解而不是XML定义Spring bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在xml配置文件中定义了

I have defined in a xml config file:

<bean id="bootstrap" class="com.package.Bootstrap"></bean>

这很好用.

bootsrap类:

The bootsrap class :

public class Bootstrap {

   @PostConstruct
   public void onServerStart() {
      System.out.println("PRINTSSSSSSSSSSSSSSSSSSS");
   }
}

该方法被触发.

但是我如何摆脱xml部分,而将引导程序注释为bean?

But how can I get rid of the xml part, and annotate bootstrap to be a bean instead?

我有

<mvc:annotation-driven />
<context:annotation-config /> 

<context:component-scan base-package="com.package" />

但是我想知道使用的注释应该是什么:

<bean id="bootstrap" class="com.package.Bootstrap"></bean>

我在网上和春季文档中都找不到关于此的任何信息:(

I could not find anything about this online and in the spring docs :(

推荐答案

有关于此的文档.您将需要像@Component这样的构造型注释.

There's documentation regarding this; you'll want a stereotype annotation like @Component.

定型bean注释

这篇关于如何使用注解而不是XML定义Spring bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆