Annotation configuration and java configuration of Spring Boot

Keywords: Spring xml Java

Annotation configuration

Statement bean

  • @Component component
  • @Service business logic layer use
  • @Repository data access layer (dao) use
  • @Controller presentation layer use
    Injecting bean
  • @Autowired

Write function bean
Where @ Component, @ Service, @ Repository and @ Controller are the same, you can choose

package com.example.demoboot.Di;

import org.springframework.stereotype.Service;

@Service//Marked as the class to be assembled, a bean managed by spring
public class demofuncService {
    public String demofun(String st){
        return "demofun!--"+st+"--!";
    }

}

Configuration class
Here is a reference:
@The Configuration class of the Configuration annotation has the following requirements:
@Configuration cannot be of final type;
@Configuration cannot be an anonymous class;
Nested configuration must be a static class.
I. use @ Configuration to load spring
1.1, @ Configuration configures spring and starts the spring container
1.2 @ Configuration start container + @ Bean register Bean
1.3 @ Configuration startup container + @ Component registration Bean
1.4. Two methods of registering AppContext class with annotationconfidapplicationcontext
1.5. Configure Web application (annotationconfigpplicationcontext is configured in web.xml)

@Configuration load Spring method:
@Configuration configure spring and start the spring container
@Configuration is marked on the class, which is equivalent to taking the class as the xml configuration file of spring. Its function is to configure the spring container (application context)

@Configuration summary:
@Configuration is equivalent to
@Bean is equivalent to
@Component scan is equivalent to < context: component scan base package = "com.dxz.demo" / >
Original text: https://blog.csdn.net/BinshaoNo_1/article/details/85005935

package com.example.demoboot.Di;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.example.demoboot.Di")
public class Diconfig {
}

Bean using function class
Use @ Service annotation to declare a bean similar to spring management
Use @ Autowired to inject demofuncService class entity bean s into UseFuncService

package com.example.demoboot.Di;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UseFuncService {
    @Autowired
    demofuncService funcService;

    public String DamoFun(String node){
        return funcService.demofun(node);
    }
}

implement

package com.example.demoboot;

import com.example.demoboot.Di.Diconfig;
import com.example.demoboot.Di.UseFuncService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@SpringBootApplication
public class DemoBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoBootApplication.class, args);

        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(Diconfig.class);
        UseFuncService usefs = context.getBean(UseFuncService.class);
        System.out.println(usefs.DamoFun("ok"));
        context.close();
    }

}

Final results:

java configuration
java Configuration is implemented through @ Configuration and @ Bean.

package com.example.demoboot.Di;

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

@Configuration
public class Javaconfig {
    @Bean
    public demofuncService demofuncService(){
        return new demofuncService();
    }

    @Bean//Call demofuncService() directly when injecting bean s
    public UseFuncService useFuncService(){
        UseFuncService useFuncService = new UseFuncService();
        useFuncService.setfuncService(demofuncService());
        return useFuncService;
    }

    @Bean//As long as a Bean exists, it can be written directly to the declared method parameter of another Bean
    public UseFuncService useFuncService(demofuncService demofuncService){
        UseFuncService useFuncService = new UseFuncService();
        useFuncService.setfuncService(demofuncService());
        return useFuncService;
    }
}

package com.example.demoboot.Di;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//@Service
public class UseFuncService {
    //@Autowired
    demofuncService funcService;

    public void setfuncService(demofuncService demofuncService){
        this.funcService = demofuncService;
    }

    public String DamoFun(String node){
        return funcService.demofun(node);
    }
}

Posted by undertaker16 on Mon, 11 Nov 2019 08:52:13 -0800