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

How Spring controls inverting IOC containers

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

Share

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

What this article shares with you is about how Spring controls the inverted IOC container. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Inversion of Control (IoC) container

one。 What is the control inversion mode? Objects are not created, but describe how they are created. Do not connect directly to objects and services in the code, but describe which component needs which service in the configuration file. Containers (IOC containers in the Spring framework) are responsible for linking these together.

2. Bean in Spring? Objects managed by the Spring IoC container are called bean. Bean is the object initialized, assembled, and managed by the Spring container. Bean definitions and bean dependencies on each other will be described by configuration metadata. Third, what is a Spring IoC container? The org.springframework.beans package is the foundation of the Spring IoC container. The org.springframework.beans.factory.BeanFactory interface is the actual representative of the Spring IoC container. The IoC container is responsible for accommodating the bean described earlier and managing the bean.

1.BeanFactory interface BeanFactory is the core interface of the IoC container. It is the realization of factory design pattern. The concept of a bean factory is the basis for Spring as an IOC container. Its responsibilities include instantiating, retrieving, and configuring objects in the application and managing relationships between objects. BeanFactory supports two object models. Singleton model: provides a shared instance of an object with a specific name that can be retrieved when queried. Singleton is the default and most commonly used object model. Ideal for stateless service objects. Prototype model: make sure that a separate object is created for each retrieval. The prototype model works best when each user needs his or her own object.

The 2.ApplicationContext interface org.springframework.context.ApplicationContext is extended from the BeanFactory interface, thus providing all the functions of BeanFactory. When building J2EE applications, using ApplicationContext will be a better choice. The context package also provides the following features: MessageSource, which provides international message access. Resource access, such as URL and files. Event propagation, which implements the bean of the ApplicationListener interface. Load multiple (inherited) contexts.

3. The configuration metadata Spring IoC container reads configuration metadata and uses it to instantiate, configure, and assemble objects in the application. XML-based metadata is the most commonly used configuration metadata format. However, it is not a description format for *. The Spring IoC container is completely open at this point. When using XML-based configuration metadata, one or more elements are configured in the top-level elements. The bean definition corresponds one to one to the objects actually used in the application. In general, the definition of bean includes: service layer object, data access layer object (DAO), Struts Action-like presentation layer object, Hibernate SessionFactory object, JMS Queue object, and so on.

four。 The instantiated IoC container (XML-based metadata) instantiates the implementation of the BeanFactory interface by loading one or more XML documents through the ClassPathXmlApplicationContext class to extend the ApplicationContext class. To retrieve the bean from BeanFactory, simply call the getBean () method, passing in the name of the bean to be retrieved.

five。 A simple example of Spring

1. Set up the Java project: MySpring

two。 Import the Spring framework.

3. Create a JavaBean:HelloBean. Write testHello methods.

HelloBean.java

View plaincopy to clipboardprint?

Package com.qu.bean

Public class HelloBean {

Public String sayHello (String name) {

Return String.format ("% 1$ s: Hello World!", name)

}

}

Package com.qu.bean

Public class HelloBean {

Public String sayHello (String name) {

Return String.format ("% 1$ s: Hello World!", name)

}

}

4. Configure applicationContext.xml to inject HelloBean into the Spring container.

ApplicationContext.xml

View plaincopy to clipboardprint?

View plaincopy to clipboardprint?

HelloBean.xml

HelloBean.xmlview plaincopy to clipboardprint?

View plaincopy to clipboardprint?

5. Import Junit 4 tests.

6. Write the test class TestHello. Rewrite the setUp method instantiation container and write the testHello method to test the hello method of HelloBean.

View plaincopy to clipboardprint?

TestHello.java

TestHello.javaview plaincopy to clipboardprint?

Package com.qu.test

Import org.springframework.context.ApplicationContext

Import org.springframework.context.support.ClassPathXmlApplicationContext

Import com.qu.bean.HelloBean

Import junit.framework.TestCase

Public class TestHello extends TestCase {

Private ApplicationContext ctx

Private HelloBean hello

Protected void setUp () throws Exception {

Super.setUp ()

This.ctx = new ClassPathXmlApplicationContext (

New String [] {"ApplicationContext.xml", "OtherXML/helloBean.xml"})

This.hello = (HelloBean) this.ctx.getBean ("helloBean")

}

Public void testSayHello () {

AssertEquals ("Java: Hello World!", this.hello.sayHello ("Java"))

}

}

This is how Spring controls the inverted IOC container. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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