The first lecture shows how to use annotations to configure beans. In fact, this is a feature introduced by Spring3. Spring2 uses XML to configure beans, at that time, the XML file in the sky gave Spring the title of "configuration hell. Spring has been striving to change this defect. The annotation method introduced by Spring3 does make the configuration much simpler, while Spring4 introduces Groovy DSL for configuration. Its syntax is much simpler than XML, and Groovy itself is a language, the configuration file is equivalent to code and can be used to implement complex configurations. Let's get in touch with Groovy DSL configurations for the first time. First, we first implement an XML bean configuration, which follows the example in the first lecture. Configuration. xml <? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "movieService" class = "huangbowen.net. service. defaultMovieService "/> <bean id =" cinema "class =" huangbowen.net. service. cinema "> <property name =" movieServi Ce "ref =" movieService "/> </bean> </beans> the XML file is clear and clear. OK. Write a test as shown in the following example to test it. XmlConfigurationTest. java package huangbowen.net; import huangbowen.net. service. cinema; import huangbowen.net. service. defaultMovieService; import org. junit. test; import org. junit. runner. runWith; import org. springframework. beans. factory. annotation. autowired; import org. springframework. context. applicationContext; import org. springframework. test. context. contextConfiguration; import org. springframework. test. Context. junit4.SpringJUnit4ClassRunner; import static org. hamcrest. core. isInstanceOf. instanceOf; import static org. junit. assert. assertNotNull; import static org. junit. assert. assertThat; @ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfiguration (locations = {"/configuration. xml "}) public class XmlConfigurationTest {@ Autowired private ApplicationContext applicationContext; @ Autowired private Cinema Cinema; @ Test public void shouldGetCinemaInstance () {Cinema cinema = applicationContext. getBean (Cinema. class); assertNotNull (cinema) ;}@ Test public void shouldGetAutowiredCinema () {assertNotNull (cinema);} @ Test public void Merge () {assertNotNull (cinema. getMovieService (); assertThat (cinema. getMovieService (), instanceOf (DefaultMovieService. class) ;}} this test is basically the same as the test in the second lecture, However, the Spring configuration is read from configuration. xml. In @ ContextConfiguration, the xml file is specified as the Spring configuration file. If you want to use Groovy DSL, introduce groovy dependencies in the first step. Pom. xml <dependency> <groupId> org. codehaus. groovy </groupId> <artifactId> groovy-all </artifactId> <version> 2.2.2 </version> </dependency> then you can create a new groovy file for configuration writing. Configuration. groovy beans {movieService huangbowen.net. service. defaultMovieService cinema huangbowen.net. service. cinema, movieService: movieService} does not reflect the strong and flexible Groovy DSL, because our example is too simple. Beans are equivalent to beans labels in xml. The first line is in the form of bean id + class. The second line is the form of bean id + class + properties map. The second parameter is a map array, which corresponds to the property and value respectively. There are many ways to implement the same Bean configuration. MovieService (huangbowen.net. service. defaultMovieService) cinema (huangbowen.net. service. cinema, {movieService: movieService}) The above is actually a feature of Groovy syntax. brackets are optional when calling methods, either or not. MovieService huangbowen.net. service. DefaultMovieService cinema (huangbowen.net. service. Cinema) {movieService: ref movieService} uses another method to set attributes through a closure. MovieService huangbowen.net. service. DefaultMovieService cinema (huangbowen.net. service. Cinema) {movieService: movieService}. The ref method is optional. Write a test to test it. Policydslconfigurationtest. java package huangbowen.net; import huangbowen.net. service. cinema; import huangbowen.net. service. defaultMovieService; import org. junit. test; import org. junit. runner. runWith; import org. springframework. beans. factory. annotation. autowired; import org. springframework. beans. factory. groovy. groovyBeanDefinitionReader; import org. springframework. beans. factory. support. beanDefinitionRea Der; import org. springframework. context. applicationContext; import org. springframework. context. support. genericApplicationContext; import org. springframework. test. context. contextConfiguration; import org. springframework. test. context. junit4.SpringJUnit4ClassRunner; import org. springframework. test. context. support. abstractGenericContextLoader; import static huangbowen.net. policydslconfigurationtest. *; impor T static org. hamcrest. core. isInstanceOf. instanceOf; import static org. junit. assert. assertNotNull; import static org. junit. assert. assertThat; @ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfiguration (value = "classpath: Configuration. groovy ", loader = genericpolicycontextloader. class) public class yydslconfigurationtest {public static class GenericGroovyContextLoader extends actgenericcont ExtLoader {@ Override protected BeanDefinitionReader createBeanDefinitionReader (GenericApplicationContext context) {return new policybeandefinitionreader (context);} @ Override protected String getResourceSuffix () {return ". groovy ";}@ Autowired private ApplicationContext applicationContext; @ Autowired private Cinema cinema; @ Test public void shouldGetCinemaInstance () {Cinema cinema = applica TionContext. getBean (Cinema. class); assertNotNull (cinema) ;}@ Test public void shouldGetAutowiredCinema () {assertNotNull (cinema);} @ Test public void Merge () {assertNotNull (cinema. getMovieService (); assertThat (cinema. getMovieService (), instanceOf (DefaultMovieService. class) ;}} if the xml configuration file is loaded in the integration test, Spring provides the GenericXmlContextLoader class. If the annotation-based configuration class is loaded, Spring provides AnnotationConfigConte XtLoader class. However, the Groovy configuration file Spring testContext framework does not provide the corresponding Loader, so you need to implement a Loader in this test method. In fact, it is also simple, as long as you implement two methods.