import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@Service
@Lazy(false)
public class SpringUtil implements ApplicationContextAware,DisposableBean
{
public static ApplicationContext applicationContext = null;
@SuppressWarnings("unchecked")
public static Object getBean(String beanName){
isInjected();
return applicationContext.getBean(beanName);
}
public static <T> T getBean(Class<T> requiredType){
isInjected();
return applicationContext.getBean(requiredType);
}
@Override
public void destroy() throws Exception {
System.out.println("springUtils工具类清除注入的applicationContext");
SpringUtil.applicationContext = null;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
System.out.println("springUtils工具类注入的applicationContext");
SpringUtil.applicationContext = applicationContext;
}
/**
* 判断是否注入
* @return
*/
public static void isInjected(){
if(SpringUtil.applicationContext == null){
throw new RuntimeException("springUtils applicationContext is not injected!");
}
}
}
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.ConfigUtils;
import com.alibaba.dubbo.container.Container;
/**
* Main. (API, Static, ThreadSafe)
*
* @author william.liangf
*/
public class StartDubboContainer {
public static final String CONTAINER_KEY = "dubbo.container";
public static final String SHUTDOWN_HOOK_KEY = "dubbo.shutdown.hook";
private static final Logger logger = LoggerFactory.getLogger(StartDubboContainer.class);
private static final ExtensionLoader<Container> loader = ExtensionLoader.getExtensionLoader(Container.class);
private static volatile boolean running = true;
public static void main(String[] args) {
try {
if (args == null || args.length == 0) {
String config = ConfigUtils.getProperty(CONTAINER_KEY, loader.getDefaultExtensionName());
args = Constants.COMMA_SPLIT_PATTERN.split(config);
}
final List<Container> containers = new ArrayList<Container>();
for (int i = 0; i < args.length; i ++) {
containers.add(loader.getExtension(args[i]));
}
logger.info("Use container type(" + Arrays.toString(args) + ") to run dubbo serivce.");
if ("true".equals(System.getProperty(SHUTDOWN_HOOK_KEY))) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
for (Container container : containers) {
try {
container.stop();
logger.info("Dubbo " + container.getClass().getSimpleName() + " stopped!");
} catch (Throwable t) {
logger.error(t.getMessage(), t);
}
synchronized (StartDubboContainer.class) {
running = false;
StartDubboContainer.class.notify();
}
}
}
});
}
for (Container container : containers) {
container.start();
logger.info("Dubbo " + container.getClass().getSimpleName() + " started!");
}
System.out.println(new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]").format(new Date()) + " Dubbo service server started!");
} catch (RuntimeException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
System.exit(1);
}
}
}
import org.springframework.stereotype.Service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.iesp.base.api.provider.ICompanyProvider;
@Service("testProvider")
public class TestProvider {
//@Reference
//@Reference(url="dubbo://172.19.5.70:20100")
@Reference(url="dubbo://172.16.36.68:20100/com.iesp.base.api.provider.ICompanyProvider")
ICompanyProvider iCompanyService;
public Object getProvider(){
return iCompanyService;
}
}
import com.iesp.config.PropertiesFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.util.Properties;
/**
* @ClassName: SpringDSConfig
* @Description: 只加载spring加载数据源
* @author: rongxin
* @date: 2015年9月30日 下午3:52:05
*/
@Configuration
@ComponentScan({ "com.iesp.base"})
public class SpringRootConfig{
Properties properties = PropertiesFactory.createProperties("iesp/base","dbConfig");
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(properties.getProperty("jdbc.driverClassName"));
dataSource.setUrl(properties.getProperty("jdbc.url"));
dataSource.setUsername(properties.getProperty("jdbc.username"));
dataSource.setPassword(properties.getProperty("jdbc.password"));
return dataSource;
}
@Bean
@Autowired
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
DataSourceTransactionManager txManager = new org.springframework.jdbc.datasource.DataSourceTransactionManager();
txManager.setDataSource(dataSource);
return txManager;
}
}
public class TestMain {
static{
String []args={};
init(args);
}
private static void init(String[] args){
System.setProperty("dubbo.spring.javaconfig","com.iesp.base.rest.config");
String[] startArgs = {"javaconfig"};
StartDubboContainer.main(startArgs);
}
public static void main(String []args){
System.out.println("\r\n\r\n****************************************************");
System.out.println(SpringUtil.getBean(ICompanyConfigRestService.class));
System.out.println("\r\n\r\n****************************************************");
System.out.println(SpringUtil.getBean("companyConfigConsumer"));
System.out.println("\r\n\r\n****************************************************");
TestProvider t=(TestProvider)SpringUtil.getBean("testProvider");
System.out.println("*********local:" + t);
System.out.println("*********provider:"+t.getProvider());
System.out.println("\r\n\r\n****************************************************");
}
}