Introduction example of spring (II)

Any programming technology, especially the introductory example, is usually Hello world, and I also follow this industry recognized principle here.

For the Maven project I use here, if you want to demonstrate, it is recommended to use eclipse (including Maven plug-in) or idea (including Maven plug-in) or eclipse + to install Maven 3 or above locally.

1、 Import Maven dependencies

Here I have imported all the basic required dependencies.

2、 Write helloword java

3、 Write mainapp java

4、 Create beans XML and put it in the Src / main / resources directory

As for bean ID, there is also a name corresponding to the ID

So here's the difference between bean ID and bean name

Common ground: both can be obtained through getBean

Differences: (1) the bean ID is unique, while the bean name can be non unique and can be repeated. Of course, if two bean names are the same, the latter will overwrite the former;

(2) the bean ID cannot be named with special characters, such as ×#@, It cannot start with a number, but the bean name can use special characters and can name multiple, such as < bean name = "test1, test2, test3" class = "CN. Test" / >

It is recommended to use bean ID instead of bean name during development

In the spring configuration, property is used as the property of the bean. That is, the members of a class. At the same time, this member must have get and set methods.

Property has such common usage:

(1) Configure the data source, where the El expression is used

(2) Reference other objects

(3) Use map list set props and other methods

props:

map:

list:

set:

< property name = "sleeptime" value = "${app. Dispatcher. Sleeptime}" / > / / obtain the value from the external property file with El expression

< property name = "sleeptime" value = "333" / > / / directly assign

< property name = "playerdao" ref = "playerdao" / > reference other bean objects. The value of ref is the ID name of other beans

5、 Run the main method to print the following results

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>