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

What are the knowledge points of Spring's IOC container?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "what are the knowledge points of Spring's IOC container", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what are the knowledge points of Spring's IOC container" can help you solve your doubts.

What is IOC

1) inversion of control, leaving the creation of objects and the calling process of objects to Spring management.

2) the purpose of using IOC is to reduce the degree of coupling.

Second, the underlying principle of IOC

XML parsing, factory mode, reflection

Third, IOC thought

Based on the IOC container, the bottom layer of the IOC container is the object factory.

4. Spring provides two ways to implement IOC container: (two interfaces)

(1) the basic implementation of BeanFactory:IOC container is the internal interface of Spring, which is not available for developers to use.

Features: objects are not created when the configuration file is loaded, but only when the object is obtained (used).

(2) the subinterface of the ApplicationContext:BeanFactory interface. For developers to use

Features: create objects when the configuration file is loaded

5. Bean management of IOC operation

1. What is Bean management:

Bean management refers to two operations:

Spring create object

Spring injection value: manual injection, automatic assembly

2. Two ways of Bean management of IOC operation

1) based on XML configuration file:

First create a User class:

Public class User {private Integer id; private String name; public User () {} public User (Integer id, String name) {this.id = id; this.name = name;} public void setId (Integer id) {this.id = id } public void setName (String name) {this.name = name;} public void setAddress (String address) {this.address = address;}}

Configure the User object in the XML configuration file:

Test class:

Public class testDemo {@ Test public void test () {/ / 1, load the xml configuration file of Spring5 ApplicationContext context = new ClassPathXmlApplicationContext ("bean1.xml"); / / 2, get the object created by the configuration User user = context.getBean ("usera", User.class); System.out.println (user);}}

Add: introduce external files in the XML configuration file (to jdbc.properties)

The jdbc.properties configuration file is as follows:

Prop.driverClassName=com.mysql.jdbc.Driverprop.url=jdbc:mysql://localhost:3306/userdbprop.username=rootprop.password=tianfei

2) based on annotation (take UserDao interface, actual class and UserService class as examples)

Public interface UserDao {public void add ();} @ Controller (value = "userDaoImpl") public class UserDaoImpl implements UserDao {@ Override public void add () {System.out.println ("userdao add.") } public UserDaoImpl () {}} / / value attribute can be omitted. If omitted, the object name created by default is the class name initials lowercase object @ Service (value = "userService") public class UserService {/ / injects basic type data @ Value (value = "abc") private String str based on annotations. / / injecting object type values based on annotations / / @ Autowired / / injecting Qualifier based on type / / @ Qualifier (value = "userDaoImpl") / / using / / private UserDao userDao;// @ Resource / / injecting @ Resource based on type (name = "userDaoImpl") / / injecting private UserDao userDao based on name Public void add () {System.out.println ("service add."); userDao.add ();} public void test () {System.out.println ("userDao =" + userDao); System.out.println (str);}}

Test class:

Public class test {@ Test public void test1 () {/ / Test creating objects using annotations / / need to be configured in the mxl configuration file:, to open the annotation scanning component / / there is no need to add the set method ApplicationContext context = new ClassPathXmlApplicationContext ("bean1.xml"); UserService userService = context.getBean ("userService", UserService.class); System.out.println (userService) UserService.add (); userService.test ();} @ Test public void test2 () {/ / Test using annotations to create objects / / No need to add set methods / / use full annotations ApplicationContext context = new AnnotationConfigApplicationContext (SpringConfig.class); UserService userService = context.getBean ("userService", UserService.class); System.out.println (userService) UserService.add (); userService.test ();}}

When using full annotations, you need to create a class as a configuration class:

@ Configuration / / as a configuration class instead of xml configuration file, fully annotated development @ ComponentScan (value = {"tianfei.Spring5"}) public class SpringConfig {}

3. IOC operation Bean management (factory Bean)

There are two Bean types for Spring:

1) normal Bean: defining the bean type in the configuration file is the return value type.

2) Factory Bean: defining the bean type and the return value type in the configuration file can be different

Step 1: create a class that acts as a factory bean and implements the interface FactoryBean

Step 2: implement the method in the interface and define the returned bean type in the implemented method

Public class MyBean implements FactoryBean {/ / is used to change the returned object (the returned object can be different from the created object) @ Override public User getObject () throws Exception {return new User (1, "Zhang San");} @ Override public Class getObjectType () {return null;} @ Override public boolean isSingleton () {return false;}}

Create a MyBean.xml file to add:

Test class:

Public class test {@ Test public void testMyBean () {/ / Test FactoryBean factory ApplicationContext context = new ClassPathXmlApplicationContext ("MyBean.xml"); Book book = context.getBean ("myBean", Book.class); System.out.println (book);}}

4. The life cycle of Bean (with a total of seven steps after adding the post processor):

(1) create a bean instance through a no-parameter constructor (no-parameter construction)

(2) inject values for the attributes of bean or reference other bean (set method)

(3) pass the bean instance to the bean post processor method postProcessBeforeInitialization

(4) call the initialization method of bean (you need to configure the initialization method yourself)

(5) pass the bean instance to the bean post processor method postProcessAfterInitialization

(6) get the bean instance object

(7) when the container is closed, call the termination method of bean (you need to configure the termination method by yourself)

After reading this, the article "what are the knowledge points of Spring's IOC container" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report