From Spring's IOC. (ii) The base configuration of the--spring bean

Source: Internet
Author: User
Tags aop constructor

The last time we spoke briefly about Spring's IOC, Spring's IOC is powerful in that there is a series of maintenance classes that can maintain bean's different relationships, and such maintenance is based on a highly configurable spring configuration file. This is a combination of my use of spring's IOC to talk about my experience in use, of course, the content involved is not very advanced, but also must be able to read the reader to understand the simple bean configuration.

We only talk about spring's IOC,AOP not within this range, although AOP is based on IOC, and the configuration of those collections is not the point here. Here we say spring's bean configuration, basically, how to configure the Bean in the configuration file, you can rationalize the structure and make our writing less, of course, what I said is not necessarily true, but also with you to discuss the discussion:), but also hope that you can come up with better methods or models.

In the spring bean configuration, in general, a label <bean></bean>, the bean tag resist almost all the configuration, and then the Bean's inheritance, abstraction, etc. are based on this label, mastering the Bean configuration, Detailed can make oneself have a relatively big promotion, especially for novice (I also, hehe). The most basic bean configuration is as follows:

<bean id="bean_test" class="cn.qtone.test.HelloWorld"></bean>

Here we simply instantiate using the HelloWorld class, using the default construction method, which is equivalent to our use:

HelloWorld tmp = new HelloWorld();

But one of the differences is that in the spring configuration there is only one instance of the entire application, that is, a singleton, of course, this single example refers to an IOC container (spring), rather than the single state pattern we usually speak of. Of course, spring can also be configured so that it is not a singleton instance, such as we modify as follows:

<bean id="bean_test" class="cn.qtone.test.HelloWorld" scope="prototype"></bean>

Notice the different color sections, so the configuration shows that each time you get an instance of HelloWorld from the spring container, a new object, that is, what we call the prototype, the default in spring is a single state (singleton), Of course, for Web applications, you can also configure the scope for request, session, and so on. As to when to use what scope of permissions depends on the use of the application, such as in multithreaded programs, whether the single state will have an impact on the program need to consider.

If the HelloWorld class does not have an empty construction method, there are only two constructed methods, how do we configure it?

……
public HelloWorld(String str)
{
   ……
}

public HelloWorld(Date date, int i)
{
   ……
}
……

Because there is no default construction method, we need to write the constructor parameters in the bean's configuration, as follows:

<!-- 使用一个参数的构造 -->
<bean id="bean_test" class="cn.qtone.test.HelloWorld" scope="prototype">
   <constructor- arg><value>hello</value></constructor-arg>
</bean>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.