1. 程式人生 > >在單獨的main方法中執行spring 管理的類方法

在單獨的main方法中執行spring 管理的類方法

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericXmlApplicationContext;

import com.fly.entity.TMeasure;
import com.fly.service.TMeasureService;
import com.fly.service.UsersService;

/**
 * 
 * Main
 * 
 * @author 00fly
 * @version [版本號, 2017-4-16]
 * @see [相關類/方法]
 * @since [產品/模組版本]
 */
public class Main
{
    private static Logger logger = LoggerFactory.getLogger(Main.class);
    
    /**
     * Main
     * 
     * @param args
     * @throws ServiceException
     * @throws DaoException
     * @see [類、類#方法、類#成員]
     */
    public static void main(String[] args)
    {
        GenericXmlApplicationContext context = new GenericXmlApplicationContext();
        context.setValidating(false);
        context.load("classpath*:application*.xml");
        context.refresh();
        UsersService usersService = context.getBean(UsersService.class);
        TMeasureService measureService = context.getBean(TMeasureService.class);
        logger.info("data: {}", usersService.queryAll());
        logger.info("--------size: {}", measureService.queryAll().size());
        try
        {
            TMeasure tMeasure = new TMeasure();
            tMeasure.setNo("no_" + RandomStringUtils.randomNumeric(5));
            // tMeasure.setAg(1.000);
            measureService.saveOrUpdate(tMeasure);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        logger.info("---------size: {}", measureService.queryAll().size());
    }
}