How to "ingenious" complex attributes in spring IOC, spring MVC source code book

1, Write in front ========== Hello, Hello, I'm grey ape, ...
summary

1, Write in front

==========

Hello, Hello, I'm grey ape, a program ape who can write bug s!

Last issue was with you** SSM programming diary **I shared with you the basic introduction of SSM framework and the basic introduction to springIOC, so today I will continue to tell you how to assign complex Properties in beans, such as a new bean, list, map and Properties, in the xml configuration file? And cascade assignment and inheritance and use of attribute classes in IOC.

2, Assignment of complex attributes in IOC

=================

Let's first create a bean object person with multi type attributes, which has the following attributes, and add getXxx() and setXxx() methods to it:

public class Person { private String name; private int age; private String sex; private String email; private Car car; private List<Book> books; private Map<String, Object> maps; private Properties properties; }

Next, we will explain how to assign a value to the assignment attribute in turn.

1. Assign values to class properties

Taking the above person class as an example, we assign values to the car attribute. We know that car is also a javaBean here, which also contains many attributes. How should we assign values to it?

Here are two methods:

(1) . reference assignment

We can first assign a value to a car object in the IOC, and then directly use ref for reference when assigning a value to the car attribute in the person object.

The following is in the xml configuration file:

<!-- For one car Class --> <bean id="car01"> <property name="carName" value="Cadillac"></property> <property name="price" value="200000"></property> <property name="color" value="black"></property> </bean> <!-- For multivalued bean Assign values --> <bean id="person01"> <!-- Reference assignment --> <property name="car" ref="car01"></property> </bean>

(2) , reference new class object assignment

In addition to the above reference methods, we can also directly assign values to the car object when assigning values to person, that is, we can directly assign values to the car object in the label of person,

When using this method, you need to write a new label inside the label assigned to the car attribute, in which the car object is assigned.

Examples are as follows:

<!-- For multivalued bean Assign values --> <bean id="person01"> <!-- adopt bean To reference a new class object and assign values to its properties --> <property name="car" > <bean> <property name="carName" value="Wuling Hongguang"></property> <property name="price" value="100"></property> </bean> </property> </bean>

Let's test it. Get the person object in IOC and read the car attribute

//****************Perform multi value assignment to obtain the bean object************* ApplicationContext iocContext2 = new ClassPathXmlApplicationContext("ioc2.xml"); /** * Assign values to class properties and get * */ @Test public void test05() { Person person = (Person)iocContext2.getBean("person01"); Car car = person.getCar(); System.out.println(car); }

2. Assign a value to the List property

When assigning values to the list attribute, you need to use the list tag to write the element values to be stored in the list.

The tag here is equivalent to new arraylist(), in which the element value is written. See code for specific use:

<!-- Assign values to complex attributes --> <bean id="person02"> <!-- by list Attribute assignment --> <property name="books"> <!-- by list Attribute assignment requires list label --> <!-- amount to new arraylist<Book>() --> <list> <!-- Where is stored and written to list Classes in --> <bean p:bookName="Journey to the West" p:author="Wu Chengen"> </bean> <ref bean="book01"/> </list> </property> </bean>

There are many other tags in the tag that can be used. Because the list in our example stores the book class, we only store two book objects in it.

There are also many tags, such as, and so on. For the list storing String, we can directly assign values with, such as 1

3. Assign a value to the map attribute

We know that maps are stored in the form of key value pairs, and sometimes multiple types of data can be stored. How should we assign values to map type attributes?

Similar to the list assignment, the data stored in the map here needs to be written in the tag. At the same time, a specific tag is used to write the element. An entry can create a key and value,

At the same time, the value types are different, and the assignment methods are also different. For ordinary value, you can directly use value = "", for reference object, use value ref, and for new bean object or list object, you need to create it in the tag. See the following code for specific use:

<!-- Assign values to complex attributes --> <bean id="person02"> <!-- by map Property --> <property name="maps"> <!-- Equivalent to creating a new linkHashMap() --> <map> <!-- One entry You can create one key and value --> <!-- For ordinary value Assign values --> <entry key="key01" value="Zhang San"></entry> <!-- Reference an external car object --> <entry key="key02" value-ref="car01"></entry> <!-- Assign a new bean object --> <entry key="key03"> <!-- use p The namespace is new book Object --> <bean p:bookName="Romance of the Three Kingdoms" p:author="Luo Guanzhong"></bean> </entry> </map> </property>

4. Assign a value to the Properties class

summary

This interview question contains almost all the interview questions and answers he encountered in a year, even the detailed dialogue and quotations in the interview. It can be said that the details are to the extreme, and even the optimization of resume and how to submit resume are easier to get interview opportunities! It also includes teaching you how to get the push quota of some big manufacturers, such as Alibaba and Tencent!

A celebrity said that success depends on 99% sweat and 1% opportunity, and if you want to get that 1% opportunity, you have to pay 99% sweat first! Only if you persevere towards your goal step by step can you have a chance to succeed!

Success will only be left to those who are prepared!

CodeChina open source project: [analysis of Java interview questions of front-line large manufacturers + core summary learning notes + latest explanation Video]

summary

This interview question contains almost all the interview questions and answers he encountered in a year, even the detailed dialogue and quotations in the interview. It can be said that the details are to the extreme, and even the optimization of resume and how to submit resume are easier to get interview opportunities! It also includes teaching you how to get the push quota of some big manufacturers, such as Alibaba and Tencent!

A celebrity said that success depends on 99% sweat and 1% opportunity, and if you want to get that 1% opportunity, you have to pay 99% sweat first! Only if you persevere towards your goal step by step can you have a chance to succeed!

Success will only be left to those who are prepared!

CodeChina open source project: [analysis of Java interview questions of front-line large manufacturers + core summary learning notes + latest explanation Video]

24 November 2021, 07:54 | Views: 6672

Add new comment

For adding a comment, please log in
or create account

0 comments