|
1, The enumerator
In actual programming, often there is such a "data set", and their value in the program is stable, and that "data set" of elements is limited.
For example, Monday to Sunday seven data elements for a week "data set" data elements four seasons four seasons of "data set."
In java how to make better use of these "data sets" mean? So he sent the enumeration handy, it is an example of enumeration:
public enum ColorEnum {
RED, GREEN, YELLOW, BLUE;
}
2, in the spring configured inject an enumerated type of object
1), the definition of an enumeration type object
package com.zcr.util;
public enum ColorEnum {
RED, GREEN, YELLOW, BLUE;
}
2) Use enumerated type classes have
package com.zcr.util
// Enum type
public class Car {
private ColorEnum colorEnum;
}
3), spring configuration file by adding the following code
// Spring injection profile
Bean>
Bean>
note:
To enumerate types injected into the class, be sure to use org.springframework.beans.factory.config.FieldRetrievingFactoryBean class enumeration type conversion, namely
Bean>
RED ColorEnum.RED will convert this bean, then used to inject the bean can be referenced.
3), the call
You want to inject in places like add the following code to the class information injection.
@Autowired
private Car car;
3, the use of java comes with a Enum (enum) type of injection tests
there are also a lot of java enum class, such as java.util.concurrent.TimeUnit, its class is defined as follows:
public enum TimeUnitextends Enum
From the above definition we can see that it is an enumeration class. Now I want to configure it to generate an instance file by spring.
In the spring configuration file if the following configuration:
Bean>
Class call:
@Autowired
private TimeUnit timeUnit;
Thus the object is to get the child.
4. Why do you want to use the file to configure the way?
The TimeUnit want java class, it has a variety of units of time, in milliseconds, seconds, minutes, hours, days ......, and when we're using in the project, must be unified unit of time, so we can profile configuration, so sub favor decoupling and when we want to modify the unit of time, we simply modify our profile on it, (or less preferably change the code). At the same time, we affirmed in many places uses TimeUnit this object, if we configured in the configuration file, you only need to modify a position to complete all modifications call, easy to modify the project. |
|
|
|