Java EE Developer Spring Boot combat with book learning-1
Next, learn Java configuration (or add new packages and code in the previous article):
Java configuration is the recommended configuration for spring4.x, which can completely replace the XML configuration and is the recommended configuration for Springboot. The Java configuration is implemented through @configuration and @bean.
@Configuration declares that the current class is a configuration class that automatically scans all classes under the specified package that use @service @Component @Repository and @controller through @componentscan annotations. Register them as spring's beans
@Bean annotations Using the method, declare that the return value of the current method is a Bean,bean name for the method name.
When do you use Java configuration? The main principle is that the global configuration uses Java configuration (such as database-related configuration, MVC-related configuration), and the configuration of business beans is configured using annotations (@Service @Component @Repository @Controller). The following shows the contents of one of the simplest Java configurations
1. Write the bean for the function class
1 PackageCom.wisely.highlight_spring4.ch1.javaconfig;2 3 /**4 * 1. The bean defining the function class no longer uses the @service annotation declaration the current Functionservice class is a bean managed by spring5 * @authorAdministrator6 */7 //And the difference in the previous example, hereThe Bean is not declared with @service8 Public classFunctionservice {9 Ten Publicstring SayHello (string word) { One return"Hello" + Word + "!"; A } - -}
2. Beans using the Feature class
1 PackageCom.wisely.highlight_spring4.ch1.javaconfig;2 3 /**4 * 2. Functionservice bean with function class5 * 6 * @authorAdministrator7 *8 */9 //no longer use @service to declare beansTen Public classUsefunctionservice { One //no longer use @autowired to automatically inject beans, instead set method settings Functionservice A PrivateFunctionservice Functionservice; - - //Remove @service and @autowired annotations, add Set method the Public voidSetfunctionservice (Functionservice functionservice) { - This. Functionservice =Functionservice; - } - + Publicstring SayHello (string word) { - returnFunctionservice.sayhello (word); + } A at}
3.Java Configuration Class Authoring (emphasis)
1 PackageCom.wisely.highlight_spring4.ch1.javaconfig;2 3 ImportOrg.springframework.context.annotation.Bean;4 ImportOrg.springframework.context.annotation.ComponentScan;5 Importorg.springframework.context.annotation.Configuration;6 /**7 * 3.spring Configuration Class8 * Spring4.x recommends using Java Configuration instead of XML configuration, by @Configuration and @Bean two annotations to achieve9 * @Configuration declares that the current class is a configuration class, equivalent to a spring-configured XML fileTen * @Bean Annotations Use the method to declare that the return value of the current method is a Bean One * When to use Java configuration? : Global configuration using Java configuration (such as database-related configuration, MVC-related configuration), business bean configuration using A * Annotation configuration (@Service @Component @Repository @Controller) - * @authorAdministrator - * the */ - @Configuration - Public classJavaconfig { - + //The Bean annotation declares that the current method return value is a Bean,bean name that is the method name Functionservice - @Bean + PublicFunctionservice Functionservice () { A return NewFunctionservice (); at } - - @Bean - PublicUsefunctionservice Usefunctionservice () { -Usefunctionservice Usefunctionservice =NewUsefunctionservice (); - //call Functionservice () directly into the Functionservice bean inUsefunctionservice.setfunctionservice (Functionservice ());// - returnUsefunctionservice; to } + - //@Bean the //another way to inject: directly to the Functionservice as a parameter to UseFunctionService1 (). * //In a spring container, as long as there is a bean in the container, it can be injected in the parameters of another bean's declaration Method!!! $ /*Public usefunctionservice UseFunctionService1 (functionservice functionservice) {//cannot also be called hereUsefunctionservice, the error will be
Notoginsengusefunctionservice.setfunctionservice (functionservice) + * * * *
Description
(1) [email protected] The note indicates that the current class is a configuration class, which means that the class might have 0 or more @bean annotations immediately, and that the @componentscan annotation scan package is not used here because all beans are defined in this class
(2). Use @bean annotations to declare the current method, such as: Public Functionservice Functionservice () {return new Functionservice ();} The return value is a bean, The name of the Bean is the method name Functionservice
(4). Another way to inject the functionservice directly into the Usefunctionservice () as a parameter, which is a feature provided by the spring container. In a spring container, as long as there is a bean in the container, it can be injected into the parameters of another bean's declaration method.
4. Testing
1 PackageCom.wisely.highlight_spring4.ch1.javaconfig;2 3 ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;4 /**5 * 4. Test6 * @authorAdministrator7 *8 */9 Public classMaintest {Ten One Public Static voidMain (string[] args) { A //1. Accept a configuration class as a parameter using the Annotationconfigapplicationcontext most spring container -Annotationconfigapplicationcontext context =NewAnnotationconfigapplicationcontext (Javaconfig.class); - //2. Get the Usefunctionservice bean for the declared configuration theUsefunctionservice Usefunctionservice = Context.getbean (usefunctionservice.class); - //3. Methods for invoking Beans -System.out.println (Usefunctionservice.sayhello ("Java config")); - context.close (); + - + } A at}
Springboot Combat (Wang Yunfei) Learn-1-2