javascript
Spring @Bean and @Configuration
2019獨角獸企業重金招聘Python工程師標準>>>
四、學習spring 注解的使用
4.12?Java-based container configuration
4.12.1?Basic concepts: @Bean and @Configuration
The central artifacts in Spring’s new Java-configuration support are?@Configuration-annotated classes and?@Bean -annotated methods.
The?@Bean ?annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container. For those familiar with Spring's?<beans/>?XML configuration the?@Bean?annotation plays the same role as the?<bean/>?element. You can use?@Bean?annotated methods with any Spring?@Component, however, they are most often used with?@Configuration?beans.
Annotating a class with?@Configuration??indicates that its primary purpose is as a source of bean definitions. Furthermore,?@Configuration?classes allow inter-bean dependencies to be defined by simply calling other?@Bean?methods in the same class. The simplest possible?@Configuration?class would read as follows:
@Configurationpublic class AppConfig { ?
? ?@Bean
? ?public MyService myService() { ? ? ? ?
? ? ? ? ?return new MyServiceImpl();
? ?}
}
The?AppConfig?class above would be equivalent to the following Spring?<beans/>?XML:
<beans><bean?id="myService"?class="com.acme.services.MyServiceImpl"/> </beans>The?@Bean?and?@Configuration?annotations will be discussed in depth in the sections below. First, however, we'll cover the various ways of creating a spring container using Java-based configuration.
4.12.2 AnnotationConfigApplicationContext
The sections below document Spring’s?AnnotationConfigApplicationContext, new in Spring 3.0. This versatile?ApplicationContext?implementation is capable of accepting not only?@Configuration?classes as input, but also plain?@Component?classes and classes annotated with JSR-330 metadata.
When?@Configuration?classes are provided as input, the?@Configuration?class itself is registered as a bean definition, and all declared?@Bean?methods within the class are also registered as bean definitions.
When?@Component?and JSR-330 classes are provided, they are registered as bean definitions, and it is assumed that DI metadata such as?@Autowired?or?@Inject?are used within those classes where necessary.
Simple construction
In much the same way that Spring XML files are used as input when instantiating a?ClassPathXmlApplicationContext,?@Configuration?classes may be used as input when instantiating an?AnnotationConfigApplicationContext. This allows for completely XML-free usage of the Spring container:
public static void main(String[] args) {? ?ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);//如上所定義的AppConfig class 文件
? ?MyService myService = ctx.getBean(MyService.class);
? ?myService.doStuff();
}
As mentioned above,?AnnotationConfigApplicationContext?is not limited to working only with?@Configuration?classes. Any?@Component?or JSR-330 annotated class may be supplied as input to the constructor. For example:
public static void main(String[] args) {? ?ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class);
? ?MyService myService = ctx.getBean(MyService.class);
? ?myService.doStuff();
}
The above assumes that?MyServiceImpl,?Dependency1?and?Dependency2?use Spring dependency injection annotations such as?@Autowired.
Building the container programmatically using register(Class<?>…)
An?AnnotationConfigApplicationContext?may be instantiated using a no-arg constructor and then configured using the?register()?method. This approach is particularly useful when programmatically building an?AnnotationConfigApplicationContext.
public?static?void?main(String[]?args)?{AnnotationConfigApplicationContext?ctx?=?new?AnnotationConfigApplicationContext();ctx.register(AppConfig.class,?OtherConfig.class);ctx.register(AdditionalConfig.class);ctx.refresh();MyService?myService?=?ctx.getBean(MyService.class);myService.doStuff(); }Support for web applications with AnnotationConfigWebApplicationContext
A?WebApplicationContext?variant of?AnnotationConfigApplicationContext?is available with?AnnotationConfigWebApplicationContext. This implementation may be used when configuring the Spring?ContextLoaderListener?servlet listener, Spring MVC?DispatcherServlet, etc. What follows is a?web.xml?snippet that configures a typical Spring MVC web application. Note the use of the?contextClass?context-param and init-param:
<web-app><!--?Configure?ContextLoaderListener?to?use?AnnotationConfigWebApplicationContextinstead?of?the?default?XmlWebApplicationContext?--><context-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext????????</param-value></context-param><!--?Configuration?locations?must?consist?of?one?or?more?comma-?or?space-delimitedfully-qualified?@Configuration?classes.?Fully-qualified?packages?may?also?bespecified?for?component-scanning?--><context-param><param-name>contextConfigLocation</param-name><param-value>com.acme.AppConfig</param-value></context-param><!--?Bootstrap?the?root?application?context?as?usual?using?ContextLoaderListener?--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--?Declare?a?Spring?MVC?DispatcherServlet?as?usual?--><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--?Configure?DispatcherServlet?to?use?AnnotationConfigWebApplicationContextinstead?of?the?default?XmlWebApplicationContext?--><init-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext????????????</param-value></init-param><!--?Again,?config?locations?must?consist?of?one?or?more?comma-?or?space-delimitedand?fully-qualified?@Configuration?classes?--><init-param><param-name>contextConfigLocation</param-name><param-value>com.acme.web.MvcConfig</param-value></init-param></servlet><!--?map?all?requests?for?/app/*?to?the?dispatcher?servlet?--><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/app/*</url-pattern></servlet-mapping></web-app>轉載于:https://my.oschina.net/u/2308739/blog/419631
總結
以上是生活随笔為你收集整理的Spring @Bean and @Configuration的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: this的五种指法
- 下一篇: 第二次冲刺------第三天