Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package com.zxy.commons.spring; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Springspring? * * <p> * ? * ???spring?spring? * <p>?? * <p>spring?xml * <p><bean id="springBeanUtils" class="com.zxy.commons.spring.SpringBeanUtils" /> * <p>? * <p><import resource="classpath:spring_config/zxy_commons_spring.xml" /> * <p> * <a href="SpringBeanUtils.java"><i>View Source</i></a> * * @author zhaoxunyong@qq.com * @version 1.0 * @since 1.0 */ public class SpringBeanUtils implements ApplicationContextAware { private static ApplicationContext context; /* (non-Javadoc) * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) */ @Override public void setApplicationContext(ApplicationContext appContext) throws BeansException { if (context == null) { context = appContext; } } /** * beanNamespring? * * @param beanName bean name * @return spring */ public static Object getBean(String beanName) { return context.getBean(beanName); } /** * beanClassspring? * * @param <T> This is the type parameter * @param beanClass bean class * @return spring */ public static <T> T getBean(Class<T> beanClass) { return context.getBean(beanClass); } /** * beanClassspring? * * @param <T> This is the type parameter * @param name the name of the bean to retrieve * @param beanClass bean class * @return spring */ public static <T> T getBean(String name, Class<T> beanClass) { return context.getBean(name, beanClass); } /** * class typespring? * * @param <T> This is the type parameter * @param type class type * @return spring * @throws BeansException BeansException */ public static <T> Object getBeanOfType(Class<T> type) throws BeansException { return BeanFactoryUtils.beanOfType(context, type); } /** * ?spring? * * @param applicationContextPaths Application context paths * @return Application context */ public static ApplicationContext init(String... applicationContextPaths) { return new ClassPathXmlApplicationContext(applicationContextPaths); } /*public static ClassPathXmlApplicationContext init(String... applicationContextPaths){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(); // context.setAllowBeanDefinitionOverriding(false); context.setConfigLocations(applicationContextPaths); context.refresh(); return context; }*/ /*private final static class SpringBeanUtilsBuilder { private final static SpringBeanUtils BUILDER = new SpringBeanUtils(); } private SpringBeanUtils() { super(); } @Override protected String getConfigLocation() { return "classpath:applicationContext.xml"; } public static ApplicationContext getContext() { return SpringBeanUtilsBuilder.BUILDER.context; }*/ }