在单独的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());
    }
}

 

转载于:https://my.oschina.net/00fly/blog/893621

你可能感兴趣的:(在单独的main方法中运行spring 管理的类方法)