Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Spring inject a type of object to enumerate     - CentOS installation Docker series (Linux)

- CentOS 6.x and CentOS7 install MPlayer (Linux)

- Configuring a Linux operating system security management services (Linux)

- MySQL 5.6.26 source install (Database)

- systemd run levels and service management command Introduction (Linux)

- Use HttpClient remote interface testing (Programming)

- Java 8 Lambda principle analysis (Programming)

- C ++ Supplements - Virtual Function Principle (Programming)

- MySQL + Corosync + Pacemaker + DRBD build highly available MySQL (Server)

- Java framework for parallel study - ForkJoin (Programming)

- Hibernate + JUnit test entity class generate database table (Programming)

- Snapshot DataGuard (Database)

- RedHat Linux 5.5 installation process SVN Service Notes (Server)

- You really do need to know a variety of programming languages (Programming)

- Python pickle module for simple use notes (Programming)

- Linux Mint under tty.js Installation Guide (Linux)

- Modular JavaScript (Programming)

- Linux System Getting Started Learning: Linux in the last command (Linux)

- Give your photos Instagram style filters plus program in ubuntu (Linux)

- Install and use automated tools Ansible in CentOS 7 (Linux)

 
         
  Spring inject a type of object to enumerate
     
  Add Date : 2018-11-21      
         
         
         
  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

        
    

        
    
note:

To enumerate types injected into the class, be sure to use org.springframework.beans.factory.config.FieldRetrievingFactoryBean class enumeration type conversion, namely


        

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:

   
       
   
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.
     
         
         
         
  More:      
 
- CentOS7 install JAVA notes (Linux)
- Linux Mint 17.2 64 bit installation Docker and management software seagull (Linux)
- Ubuntu installed racing game Speed Dreams 2.1 (Linux)
- PLSQL often lose resolution process automatically disconnect the connection (Database)
- Installation of Python2.7.10 under CentOS 6.4 (Linux)
- Android official recommendation: DialogFragment create dialog (Programming)
- Python: Finding meet the conditions specified in the file directory (Programming)
- Using the Linux VNC service (Server)
- Use Swift remove the spaces in the string (Programming)
- Deploy OpenStack Juno on Ubuntu 14.04 (Linux)
- Ubuntu install image browser and manager Phototonic 1.6.17 (Linux)
- C ++ How to determine the types of constants (Programming)
- MariaDB 10.1 and MySQL 5.7 in general performance on commodity hardware (Database)
- Linux SSH login without a password (Linux)
- Fedora 21 setting boot script (Linux)
- configuration ssh without password under Linux (Linux)
- ThinkPad X201i installation notes Debian 7.5 (Linux)
- DRBD + Heartbeat solve NFS single point of failure (Server)
- CentOS7 Minimal minimize installation and then install the GNOME graphical interface (Linux)
- Linux, C programming language library file handling and preparation of Makefile (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.