2012-04-12 29 views
15

Tôi đã xác định trong tệp cấu hình xml:Làm cách nào để xác định bean Spring bằng chú thích thay vì XML?

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

hoạt động tốt.

Lớp bootsrap:

public class Bootstrap { 

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

Phương pháp này bị sa thải.

Nhưng làm thế nào tôi có thể loại bỏ phần xml và chú thích bootstrap là một bean thay thế?

Tôi có

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

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

Nhưng tôi đã tự hỏi những gì các chú thích sử dụng nên được thay thế:

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

tôi không thể tìm thấy bất cứ điều gì về vấn đề này trực tuyến và trong tài liệu mùa xuân: (

Trả lời

18

Có tài liệu về vấn đề này; bạn sẽ muốn có chú thích lập thể như @Component.

Stereotype bean annotations

+3

'@ Component' liên kết bạn với Spring. Cân nhắc sử dụng chú thích JSR-330 ('@ Named',' @ Inject') thay vào đó: http://www.mkyong.com/spring3/spring-3-and-jsr-330-inject-and-named-example/ –

+0

@JJZabkar Vì vậy, không '', chúng đã được gắn với Spring. –

+0

Có, nhưng trong các tệp riêng biệt. Giả sử bạn chú thích đậu của bạn với @ Component/@ Service/etc. Điều đó có nghĩa là tất cả các lớp của bạn có các phụ thuộc 'import' (biên dịch) trong khung công tác Spring. Bạn vẫn sẽ có cấu hình XML của mình. (Tiếp theo) –

4

đây là một ví dụ đơn giản mà tôi đã thực hiện:

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

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

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; 
} 

} 

này là hoàn toàn dựa trên mùa xuân Chú và được thực hiện trên khuôn khổ mùa xuân 4.2.5

hy vọng nó giúp.

Các vấn đề liên quan