List of usage examples for org.springframework.context ApplicationContext getBeansWithAnnotation
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
From source file:es.us.isa.ideas.utilities.PopulateDatabase.java
public static void main(String[] args) { ApplicationContext ctx; EntityManagerFactory emf;//from w w w. j a v a 2 s . c om EntityManager em; EntityTransaction et; ctx = new ClassPathXmlApplicationContext("utilities/PopulateDatabase.xml"); emf = Persistence.createEntityManagerFactory("persistenceUnit"); em = emf.createEntityManager(); et = em.getTransaction(); et.begin(); try { for (Entry<String, Object> entry : ctx.getBeansWithAnnotation(Entity.class).entrySet()) { em.persist(entry.getValue()); System.out.println(String.format("Persisting (%s, %s@%d)", entry.getKey(), entry.getValue().getClass().getName(), entry.getValue().hashCode())); } et.commit(); } catch (Exception oops) { oops.printStackTrace(); et.rollback(); oops.printStackTrace(); } finally { if (em.isOpen()) em.close(); if (emf.isOpen()) emf.close(); ((ClassPathXmlApplicationContext) ctx).close(); } }
From source file:utilities.MinPopulateDatabase.java
public static void main(String[] args) { ApplicationContext populationContext; DatabaseUtil databaseUtil;/*from w ww. j av a 2 s. c o m*/ databaseUtil = null; try { System.out.printf("MinPopulateDatabase 1.3%n"); System.out.printf("--------------------%n%n"); System.out.printf("Initialising persistence context `%s'...%n", DatabaseConfig.PersistenceUnit); databaseUtil = new DatabaseUtil(); System.out.printf("Creating database `%s' (%s)...%n", databaseUtil.getDatabaseName(), databaseUtil.getDatabaseDialectName()); databaseUtil.recreateDatabase(); System.out.printf("Reading configuration file `%s'...%n", "MinPopulateDatabase.xml"); populationContext = new ClassPathXmlApplicationContext("classpath:minPopulateDatabase.xml"); System.out.printf("Persisting %d entities...%n%n", populationContext.getBeanDefinitionCount()); databaseUtil.openTransaction(); for (Entry<String, Object> entry : populationContext.getBeansWithAnnotation(Entity.class).entrySet()) { String beanName; DomainEntity entity; beanName = entry.getKey(); entity = (DomainEntity) entry.getValue(); System.out.printf("> %s: %s", beanName, entity.getClass().getName()); databaseUtil.persist(entity); System.out.printf(" -> id = %d, version = %d%n", entity.getId(), entity.getVersion()); } databaseUtil.commitTransaction(); } catch (Throwable oops) { System.out.flush(); System.err.printf("%n%s%n", oops.getLocalizedMessage()); oops.printStackTrace(System.err); } finally { if (databaseUtil != null) databaseUtil.close(); } }
From source file:com.graphaware.server.foundation.stats.ControllerStatsCollector.java
@Autowired public ControllerStatsCollector(StatsCollector statsCollector, ApplicationContext context) { for (Object bean : context.getBeansWithAnnotation(Controller.class).values()) { statsCollector.moduleStart(bean.getClass().getCanonicalName()); }/*from www. j a va 2s . com*/ }
From source file:com.github.wnameless.spring.bulkapi.BulkApiValidator.java
public BulkApiValidator(ApplicationContext appCtx) { Map<String, Object> bulkableBeans = appCtx.getBeansWithAnnotation(Bulkable.class); List<String> basePackageNames = ra(bulkableBeans.values()).map(new TransformBlock<Object, String>() { @Override/* ww w. j a v a 2 s .c o m*/ public String yield(Object item) { return item.getClass().getPackage().getName(); } }); pathRes = new RoutingPathResolver(appCtx, basePackageNames.toArray(new String[basePackageNames.size()])); }
From source file:de.otto.jsonhome.controller.GeneratorBasedJsonHomeSource.java
@Autowired public void setApplicationContext(final ApplicationContext applicationContext) { final Map<String, Object> controllerBeans = applicationContext.getBeansWithAnnotation(Controller.class); controllerTypes = new HashSet<Class<?>>(); for (Object o : controllerBeans.values()) { controllerTypes.add(o.getClass()); }/* w w w .j a va 2 s . com*/ LOG.info("Found {} controllers in application context", controllerTypes.size()); }
From source file:com.github.springfox.loader.SpringfoxLoader.java
private Object getBeanWithAnnotation(ApplicationContext applicationContext) { Map<String, Object> beans = applicationContext.getBeansWithAnnotation(EnableSpringfox.class); if (beans.size() == 1) { return beans.values().iterator().next(); } else {//from ww w . j a va 2 s. co m throw new IllegalStateException( "Expected to find 1 @EnableSpringfox annotation, but found " + beans.size()); } }
From source file:com.github.aenygmatic.spring.osgi.registration.OsgiSpringComponentCollector.java
public List<SpringOsgiComponent> findOsgiComponents(ApplicationContext springContext) { List<SpringOsgiComponent> components = new ArrayList<>(); for (Map.Entry<String, Object> entry : springContext.getBeansWithAnnotation(OsgiService.class).entrySet()) { Object bean = entry.getValue(); components.add(new SpringOsgiComponent(registration(bean), bean, null)); }/* ww w. j a v a 2 s . c o m*/ return components; }
From source file:com.haulmont.cuba.core.sys.remoting.RemoteServicesBeanCreator.java
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { log.info("Configuring remote services"); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; ApplicationContext coreContext = context.getParent(); Map<String, Object> services = coreContext.getBeansWithAnnotation(Service.class); for (Map.Entry<String, Object> entry : services.entrySet()) { String serviceName = entry.getKey(); Object service = entry.getValue(); List<Class> serviceInterfaces = new ArrayList<>(); List<Class> interfaces = ClassUtils.getAllInterfaces(service.getClass()); for (Class intf : interfaces) { if (intf.getName().endsWith("Service")) serviceInterfaces.add(intf); }/*from w ww .ja va 2 s. co m*/ String intfName = null; if (serviceInterfaces.size() == 0) { log.error("Bean " + serviceName + " has @Service annotation but no interfaces named '*Service'. Ignoring it."); } else if (serviceInterfaces.size() > 1) { intfName = findLowestSubclassName(serviceInterfaces); if (intfName == null) log.error("Bean " + serviceName + " has @Service annotation and more than one interface named '*Service', " + "but these interfaces are not from the same hierarchy. Ignoring it."); } else { intfName = serviceInterfaces.get(0).getName(); } if (intfName != null) { BeanDefinition definition = new RootBeanDefinition(HttpServiceExporter.class); MutablePropertyValues propertyValues = definition.getPropertyValues(); propertyValues.add("service", service); propertyValues.add("serviceInterface", intfName); registry.registerBeanDefinition("/" + serviceName, definition); log.debug("Bean " + serviceName + " configured for export via HTTP"); } } }
From source file:com.github.wnameless.spring.routing.RoutingPathResolver.java
/** * Creates a {@link RoutingPathResolver}. * //from w ww . j a v a2s .c o m * @param appCtx * the Spring {@link ApplicationContext} * @param basePackages * packages to be searched */ public RoutingPathResolver(ApplicationContext appCtx, String... basePackages) { env = appCtx.getEnvironment(); Map<String, Object> beans = appCtx.getBeansWithAnnotation(Controller.class); beans.putAll(appCtx.getBeansWithAnnotation(RestController.class)); retainBeansByPackageNames(beans, basePackages); for (Object bean : beans.values()) { List<Method> mappingMethods = getMethodsListWithAnnotation(bean.getClass(), RequestMapping.class); RequestMapping classRM = bean.getClass().getAnnotation(RequestMapping.class); for (Method method : mappingMethods) { RequestMapping methodRM = method.getAnnotation(RequestMapping.class); for (Entry<String, RequestMethod> rawPathAndMethod : computeRawPaths(classRM, methodRM)) { String rawPath = rawPathAndMethod.getKey(); String path = computePath(rawPath); String regexPath = computeRegexPath(path); routingPaths.add(new RoutingPath(rawPathAndMethod.getValue(), rawPath, path, Pattern.compile(regexPath), bean.getClass().getAnnotations(), method.getAnnotations())); } } } }