Java:如何在不知道包名的情况下扫描类?

时间:2018-02-15 08:18:51

标签: java spring reflection packages classloading

我正在编写一个API,它根据类的注释提供功能。 API将被导入,因此它不了解客户端的包。所以问题是:如何在不知道包名的情况下扫描类?

我找到了这些方法:

ClassPathScanningCandidateComponentProvider.findCandidateComponents(basePackageName)

但它只返回一个包的子集。

*

但不接受通配符,例如.@Component public class Application { @Autowired public ApplicationContext appContext; public static void main(String[] args) { Application a = new Application(); String[] beanNames=a.appContext.getBeanNamesForAnnotation(Run.class); for (String beanName : beanNames) { System.out.println(beanName); Object bean = a.appContext.getBean(beanName); Class<? extends Object> class1 = bean.getClass(); System.out.println(class1); } } }

编辑回答@Rakesh

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
%matplotlib inline
import matplotlib.pyplot as plt

#Network
tf.reset_default_graph()

noise_imgs = tf.placeholder(tf.float32, [None, 28, 28, 1])
imgs = tf.placeholder(tf.float32, [None, 28, 28, 1])

# Building the encoder
def encoder(x):
   out1 = tf.layers.conv2d(x, 32, [3, 3], padding="valid", activation=tf.nn.relu) #26*26*32 
   out1pool = tf.layers.max_pooling2d(inputs=out1, pool_size=[2, 2], strides=2) #13*13*32
   out2 = tf.layers.conv2d(out1pool, 64, [3, 3], padding="valid", activation=tf.nn.relu) #11*11*64
   out1pool2 = tf.layers.max_pooling2d(inputs=out2, pool_size=[2, 2], strides=2) #5*5*64

   flat_inputs = tf.contrib.layers.flatten(out1pool2)
   hundred = tf.layers.dense(flat_inputs, units=100)
   return hundred

   # Building the decoder
def decoder(x):
   img = tf.reshape(x, [-1, 10, 10, 1])

   l1 = tf.layers.conv2d_transpose(img, 32, [7, 7], padding="valid", activation=tf.nn.relu)
   l2 = tf.layers.conv2d_transpose(l1, 1, [13, 13], padding="valid", activation=tf.nn.relu)          
   return l2 

   # Construct model
   encoder_op = encoder(noise_imgs)
   decoder_op = decoder(encoder_op)

loss = tf.sqrt(tf.reduce_sum(tf.square(imgs-decoder_op)))
optim = tf.train.AdamOptimizer(learning_rate = 0.001).minimize(loss)


# Start Training
noise_constant=0.2
num_iter = 1000
batch_size = 128

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

# Training
for i in range(num_iter):  
    batch_x, _ = data.train.next_batch(batch_size)

    #shape (64, 784)
    batch = batch_x.reshape([batch_size, 28, 28, 1])

    noise_matrix = noise_constant * np.random.randn(batch_size, 784)
    noise_matrix = noise_matrix.reshape([batch_size, 28, 28, 1])

    batch_img_noise = batch_x + noise_matrix
    batch_img_noise = batch_img_noise.reshape([64, 28, 28, 1])


    # Run optimization op (backprop) and cost op (to get loss value)
     _, l = sess.run([optim, L], feed_dict={noise_imgs: batch_img_noise , imgs: batch})
   print(l)

1 个答案:

答案 0 :(得分:0)

假设您有一个自定义注释@CustomAnnotation,并且您已经使用它注释了一个类A

@CustomAnnotation
@Component
public class A{
 ...
}

现在,如果注入应用程序上下文,可以获得这样的类

@Autowired
ApplicationContext appContext;
.
.
.
someMethod(){

  string[] beanNames=appContext.getBeanNamesForAnnotation(CustomAnnotation.class);
   for (val beanName : beanNames) {
      Object bean = appContext.getBean(beanName);
      Class<? extends Object> class1 = bean.getClass();
   }
}