如何使用注释而不是 XML 定义 Spring bean?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10126537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define a Spring bean using annotation instead of XML?
提问by mmm
I have defined in a xml config file:
我在 xml 配置文件中定义了:
<bean id="bootstrap" class="com.package.Bootstrap"></bean>
this works fine.
这工作正常。
The bootsrap class :
引导类:
public class Bootstrap {
@PostConstruct
public void onServerStart() {
System.out.println("PRINTSSSSSSSSSSSSSSSSSSS");
}
}
The method gets fired.
该方法被触发。
But how can I get rid of the xml part, and annotate bootstrap to be a bean instead?
但是我怎样才能摆脱 xml 部分,并将引导程序注释为一个 bean?
I have
我有
<mvc:annotation-driven />
<context:annotation-config />
and
和
<context:component-scan base-package="com.package" />
But I was wondering what the annotationused should be that replaces:
但我想知道使用的注释应该是什么来替换:
<bean id="bootstrap" class="com.package.Bootstrap"></bean>
I could not find anything about this online and in the spring docs :(
我在网上和春季文档中找不到任何关于此的信息:(
回答by Dave Newton
There's documentation regarding this; you'll want a stereotype annotation like @Component.
有关于此的文档;你会想要一个像@Component.
回答by Farzan Skt
this is a simple example that I have just made:
这是我刚刚制作的一个简单示例:
Main.java
主程序
package the.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext aac = new AnnotationConfigApplicationContext(Person.class, Phones.class);
Person person = aac.getBean(Person.class);
System.out.println(person.getPhones().getPhoneOne());
System.out.println(person.getPhones().getPhoneTwo());
System.out.println(person.getSurname());
System.out.println(person.getName());
System.out.println(person.getAge());
aac.close();
}
}
Person.java
人.java
package the.test;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
//you may use @ComponentScan("the.test") here and omit declaring
//"Phone.class" in the main method
public class Person {
private int age;
private String name;
private String surname;
private Phones phones;
public int getAge() {
return age;
}
@Value("33")
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
@Value("John")
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
@Value("Due")
public void setSurname(String surname) {
this.surname = surname;
}
public Phones getPhones() {
return phones;
}
@Resource
public void setPhones(Phones phones) {
this.phones = phones;
}
}
Phones.java
电话.java
package the.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Phones {
private String PhoneOne;
private String PhoneTwo;
public String getPhoneOne() {
return PhoneOne;
}
@Value("987654321")
public void setPhoneOne(String phoneOne) {
PhoneOne = phoneOne;
}
public String getPhoneTwo() {
return PhoneTwo;
}
@Value("123456")
public void setPhoneTwo(String phoneTwo) {
PhoneTwo = phoneTwo;
}
}
this is completely based on Spring Annotation and is made on spring framework 4.2.5
这完全基于 Spring Annotation 并在 spring 框架 4.2.5 上制作
hope it helps.
希望能帮助到你。

