1 The basic idea of spring is to simplify java development.
Simplify java development using the following 4 strategies
1) Lightweight and minimally intrusive programming based on POJO
2) Achieve loose coupling by using DI and AOP
3) Declarative programming based on aspects and conventions
4) Reduce boilerplate code through aspects and templates
2 Dependency injection: object dependencies Relationships will be set at object creation time by third-party components responsible for coordinating various objects in the system. Objects do not need to create or manage their dependencies themselves - dependencies will be automatically injected into the objects that need them.
3 Assembly: The act of creating collaboration between application components is called assembly.
4 Aspect-oriented programming is defined as a technique that promotes the separation of concerns in an application. Crosscutting concerns always span multiple components of the system.
The main function is to help object decoupling.
5 Use templates to eliminate boilerplate code. For example, jJDBC/JMS/JNDI/REST are all scenarios where template code is used.
6 The spring container is responsible for creating objects, assembling them, configuring them and managing their entire life cycle.
spring containers can be divided into two types, bean factory and ApplicationContext. Generally prefer to use ApplicationContext.
Common ones are AnnotationConfigApplicationContext/AnnotationConfigWebApplicationContext/ClassPathXmlApplicationContext/FileSystemXmlApplicationContext/XmlWebApplicationContext
ClassPathXmlApplicationContext searches for file context in all class paths.
7 Configuration of bean objects, three methods. 1) Automated assembly 2) Assembling beans through java code 3) Assembling beans through xml
7.1 Automated assembly
@Component is used on a class, and using it on a class indicates that the class is a component class, but component scanning is not enabled by default.
You can use the following techniques to enable scanning
@Configuration @ComponentScanpublic class PlayerConfig { }
@ComponentScan默认会扫描与配置类相同的包以及这个包下所有子包。 当然也可以在xml配置文件中使用<context:component-scan />元素开启扫描。 为组件扫描的bean命名。可以有两种方式 1)@Component("lonelyHeartsClub") 2)@Named("lonelyHeartsClub") 字符串相当于是xml配置文件中的id
Generally use the first method.
7.1.1 Component scanning
7.1.2 Autowiring
Autowiring is to let Spring automatically satisfy bean dependencies A method that, in the process of satisfying dependencies, will look for other beans that match a certain bean requirement in the spring application context.
Keywords:@Autowired,@Inject. However, the first method is generally used.
Note:
1) If the required bean is not found, spring will throw an exception.
The above is the detailed content of Detailed explanation of the basic concepts of spring. For more information, please follow other related articles on the PHP Chinese website!