1. Use pojo for lightweight and minimally invasive development
2. Achieve loose coupling through dependency injection and interface-based programming
3. Declarative programming through Aop and default habits
4. Use Aop and template reduction Schematic code Annotation for
declaring bean (declaring that the current bean has a bean managed by the spring container)
@Compent component, there is no clear role
@Service is used in the business logic layer (service layer) Use
@Repository in the data access layer (dao layer)
@Controller uses Bean-
injected (can be annotated on set methods or properties. The habit is to annotate on properties)
@Autowired: annotations provided by spring
@Inject: annotations provided by JSR-330
@Resource: JSR The annotation
@Configuration provided by -250 declares that the current class is a configuration class.
@CompanentScan("") will automatically scan all classes under the package name that are annotated with the above declaration and register as Bean
Java configuration:
The configuration recommended by spring 4.x can be completely Alternative to xml configuration, implemented through @Configuration and @Bean
@Configuration declares that the current class is a configuration class equivalent to the xml file of spring configuration
@Bean is annotated on the method to declare that the return value of the current method is a Bean
public class FunctionService { public String sayHello(String word){ return "hello " + word + " !"; } } public class UseFunctionService { @Autowired private FunctionService functionService; public String sayHello(String word){ return functionService.sayHello(word); } } @Configuration public class JavaConfig { @Bean public FunctionService functionService() { return new FunctionService(); } @Bean(initMethod="init",destroyMethod="destroy") public UseFunctionService useFunctionService () { return new UseFunctionService (); } public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class); UseFunctionService service = context.getBean (UseFunctionService.class); String res = service.sayHello("young"); System.out.println(res); context.close(); } }
Scope of Beans: Spring's default configuration is that all containers share an instance. This is
achieved through the @Scope annotation:
1.Singleton has only one Bean instance
2.Prototype: create a new instance for each call
3.Request: For each http request in the web project Create a new bean instance
4.Session: Create a new bean instance for each http session in the web project
5.GlobalSession: @StepScope
spring's El expression:
use @Value annotation
including injecting ordinary characters; injecting system properties; injecting expressions Operation result; Inject other bean properties; Inject file content; Inject URL content; Inject property file Use configuration file
via @PropertySource("classpath:a.properties")
@Value("t love you") private String normalStr; @Value("#{systemProperties['os.name']}") private String osName; @Value("#{demoService.anther}") private String normalStr; @Value("${user.name}") private String username;