Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring [01. Collation of basic knowledge (part I)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Concept IOC (Inversion of Control)

The idea is to reverse the direction of resource acquisition. The traditional resource lookup method requires the component to initiate a request to the container to find the resource. In response, the container returns resources at the right time. After applying IOC, the container actively pushes the resources to the components it manages, and all the components need to do is to choose an appropriate way to accept the resources. This kind of behavior is also called the passive form of search.

DI (Dependency Injection)-another expression of IOC

That is, components accept resource injections from containers in predefined ways (for example, setter methods). This expression is more straightforward than IOC.

Configure Bean injection through the reflection mechanism. Constructor mode (constructor with parameters is required in the class) property mode (constructor without arguments in the class)

When the abstract attribute is true, the IOC container will not instantiate the object. If the value attribute contains special characters, you need to wrap it with a wrapper

Collection property is list collection property is props administrator@example.org support@example.org development@example.org property is map property is set just some string configure Bean static factory method through factory method

The Factory class itself does not need to be instantiated, and a static method is provided in this Factory class to generate the bean object

Public class StaticAddressFactory {private static Map addressMap = new HashMap (); static {addressMap.put ("JINAN", new Address ("JINAN", "SHILIHE")); addressMap.put ("QINGDAO", new Address ("QINGDAO", "SIFANG"));} public static Address getAddress (String name) {return addressMap.get (name);}} instance factory method

The Factory class itself needs to be instantiated

Public class SigletonAddressFactory {public Map getAddressMap () {return addressMap;} public void setAddressMap (Map addressMap) {this.addressMap = addressMap;} private Map addressMap = new HashMap (); public Address getAddress (String name) {return this.addressMap.get (name);}} Factory Bean mode configuration BeanFactory Bean mode

More common than the factory method pattern, the FactoryBean factory class must implement the interface to FactoryBean provided by spring

Override the following three methods:

GetObject ()

This is the bean object you want to produce using the factory class, usually new 1 object on okgetObjectType ()

You must specify the classisSingleton of the object above bean

This method determines whether the bean is a single case public class MyAddressFactory implements FactoryBean {private String city; private String street; public String getCity () {return city;} public void setCity (String city) {this.city = city;} public String getStreet () {return street;} public void setStreet (String street) {this.street = street @ Override public Address getObject () throws Exception {return new Address (this.city, this.street);} @ Override public Class getObjectType () {return Address.class;} @ Override public boolean isSingleton () {return false;}}

Through FactoryBean, bean from the IOC container, and return the specified bean,property: MyAddressFactory property settings through the getObject of FactoryBean

Injection of Bean component scanning (component scanning) through annotations: Spring can automatically scan, detect, and instantiate components with specific annotations from classpath. Specific components include:

@ Component: basic annotation that identifies a component managed by Spring

@ Respository: identifies persistence layer components

@ Service: identifies the service layer (business layer) component

@ Controller: identifies the presentation layer components

Spring has a default naming strategy for scanned components: use unqualified class names with the first letter lowercase. You can also identify the name of the component by the value of the value attribute in the annotation

After using specific annotations on the component class, you also need to declare in the Spring configuration file:

The base-package attribute specifies a base class package that needs to be scanned, and the Spring container will scan all classes in this base class package and its subpackages.

When you need to scan multiple packages, you can use commas to separate them.

If you want to scan only specific classes instead of all classes under the base package, you can use the resource-pattern attribute to filter specific classes

The child node represents the target class to be included. The child node indicates that there can be several and child nodes Scope ("prototype") under the target class to be excluded.

When bean adds the annotation @ Scope ("prototype"), it is the multi-instance bean obtained.

Naming strategy

Spring has a default naming policy for scanned components:

Use an unqualified class name with the first letter lowercase. You can also identify the scope singleton of the component name bean by the value of the value attribute in the annotation

Singleton mode

Prototype

Each retrieved bean object needs to be instantiated

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report