2012-10-11 36 views
7

Tôi có một mã số:mùa xuân, làm việc với @Configuration và chú thích @Bean

@Configuration 
public class BeanSample { 

    @Bean(destroyMethod = "stop") 
    public SomeBean someBean() throws Exception { 
     return new SomeBean("somebean name1"); 
    } 


    class SomeBean { 

     String name; 

     public SomeBean(String name) { 
      this.name = name; 
     } 

     public void stop() { 
      System.out.println("stop"); 
     } 
    } 

    public static void main(String[] args) throws Exception { 

     BeanSample beanSample = new BeanSample(); 
     SomeBean someBean1 = beanSample.someBean(); 

     ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
       new String[] {"appContext.xml"}); 

     SomeBean someBean2 = (SomeBean) appContext.getBean("someBean"); 

     if (someBean1 == someBean2) System.out.println("OK"); 

    } 
} 

Tôi đang mong đợi, khi tôi bắt đầu ứng dụng, BeanSample.getSomeBean() sau đó SomeBean đang bắt đầu được cung cấp bởi ' someBean '.

Bu bây giờ tôi có một lỗi: Không đậu có tên là 'someBean' được định nghĩa

Thực ra, tôi chấm không hiểu được các ứng dụng bối cảnh tôi nên sử dụng để chọn đậu của tôi lên?

Về @Configuration:

Bất kỳ lý do này, lý do tại sao tôi nên sử dụng @Configuration chú thích ở đây? (Với thế này, IDE của tôi nhấn mạnh các lớp học của tôi vì nó là mùa xuân liên quan sau đó, vì vậy nó nên có ý nghĩa)

- OK: sau khi tôi nhận được một câu trả lời mã của tôi trông như thế này:

public static void main(String[] args) throws Exception { 

     AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(BeanSample.class); 

     SomeBean someBean2 = (SomeBean) appContext.getBean("someBean"); 

     if (someBean2 != null) System.out.println("OK"); 

    } 

Trả lời

6

thứ nhất, nếu bạn sử dụng cấu hình Java, bạn phải nhanh chóng hoàn cảnh của bạn như thế này:

new AnnotationConfigApplicationContext(BeanSample.class) 

thứ hai, @Configuration chú thích không làm cho một đậu ra khỏi lớp mà được chú thích. Chỉ có các phương pháp @Bean được sử dụng để tạo hạt.

Nếu bạn muốn có một hạt đậu BeanSample, bạn phải tạo phương thức @Bean khác để tạo. Nhưng sau đó một lần nữa, tại sao bạn sẽ muốn điều đó? Tôi cho rằng lớp @Configuration chỉ nên được sử dụng làm vùng chứa cấu hình chứ không phải cho bất kỳ thứ gì khác.

Thứ ba, tên bean mặc định cho @Bean không tuân theo quy ước của getters thuộc tính. Tên phương thức được sử dụng trực tiếp, có nghĩa là trong ví dụ của bạn, đậu sẽ được đặt tên là getSomeBean và không phải là someBean. Thay đổi phương thức thành phương thức này:

@Bean(destroyMethod = "stop") 
public SomeBean someBean() throws Exception { 
    return new SomeBean("somebean name1"); 
} 

Cuối cùng, lớp @Configuration không được khởi tạo. Các phương pháp của nó chỉ phục vụ mục đích tạo ra các hạt cà phê. Mùa xuân sau đó sẽ xử lý vòng đời của họ, tiêm thuộc tính và như vậy. Ngược lại, nếu bạn khởi tạo lớp và gọi trực tiếp các phương thức, các đối tượng được trả về sẽ là các đối tượng bình thường không có liên quan gì đến Spring.

+0

Ok, tôi đã thay đổi câu hỏi của mình rồi. – ses

+0

Và tôi đã thay đổi câu trả lời. ;) – rolve

+0

ok. nó hoạt động. Tôi cũng chuyển BeanSample - không phải là bên trong. Ngoài ra, đã cung cấp hàm tạo mặc định cho BeanSample. Đang cố gắng hiểu tại sao tôi cần: @Configuration then .. – ses

4

thử nghiệm của bạn sẽ trông như thế này với @Configuration cấu hình đậu (những gì bạn có thể đã được xác định trước đó sử dụng một tập tin xml bối cảnh hiện nay được định nghĩa sử dụng @Configuration mã java của bạn)

này sẽ tạo ra một bối cảnh ứng dụng với BeanSample cung cấp cấu hình đậu:

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeanSample.class); 

Bây giờ, trong @Configuration của bạn:

@Bean 
public SomeBean someBean() throws Exception { 
    return new SomeBean("somebean name1"); 
} 

tên đậu là tên phương thức ..để phía trên tên phương pháp là "someBean", trong trường hợp của bạn, bạn có tên đậu như "getSomeBean"

Vì vậy, để tìm kiếm các đậu bạn phải làm:

SomeBean bean = appContext.getBean("someBean", SomeBean.class); 
+0

câu trả lời này cũng tốt. thx – ses

6

bean sản xuất bởi

@Bean 
public SomeBean getSomeBean() 

sẽ có tên mặc định - và đó là tên của phương pháp sản xuất getSomeBean

Vì vậy, bạn có thể làm hai việc

@Bean 
public SomeBean getSomeBean() {...} 
... 
SomeBean bean = (SomeBean) appContext.getBean("getSomeBean"); 
if (bean != null) System.out.println("OK"); 

hoặc

@Bean(name="someBean") 
public SomeBean getSomeBean() {...} 
... 
SomeBean bean = (SomeBean) appContext.getBean("someBean"); 
if (bean != null) System.out.println("OK"); 

Một số ví dụ hoàn chỉnh tôi đã sử dụng AnnotationConfigApplicationContext thay vì ClassPathXmlApplicationContext

import java.util.Arrays; 

import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
public class BeanSample { 

    @Bean(name="someBean") 
    public SomeBean getSomeBean() throws Exception { 
     return new SomeBean("somebean name1"); 
    } 

    class SomeBean { 

     String name; 

     public SomeBean(final String name) { 
      this.name = name; 
     } 

     public void stop() { 
      System.out.println("stop"); 
     } 
    } 

    public static void main(final String[] args) throws Exception { 

     AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(BeanSample.class); 

     BeanSample beanSample = (BeanSample) appContext.getBean("beanSample"); 

     //next time use this to have a look at the beans in the context! 
     System.out.println(Arrays.toString(appContext.getBeanDefinitionNames())); 

     SomeBean bean = (SomeBean) appContext.getBean("someBean"); 
     if (bean != null) System.out.println("OK"); 

    } 
} 

OUTPUT:

[org.springframework.context.annotation.internalConfigurationAnnotationProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor, org.springframework.context.annotation.internalPersistenceAnnotationProcessor, beanSample, org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0, someBean] OK

+0

Tại sao tôi nên sử dụng @Configuration ở đây? (Tôi đã thêm câu hỏi này vào bài đăng) – ses

+0

Để đánh dấu lớp này là lớp cấu hình ngữ cảnh mùa xuân – Ralph

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