如何将Spring bean注入DynamoDBMarshaller <t>的实现?

时间:2017-07-11 01:23:31

标签: java spring amazon-dynamodb

这是一个人为的例子,但它说明了我的问题。我想创建一个自定义<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>MAHASISWA</title> <link rel="shortcut icon" href="code.ico"> <style> #data tr:hover{ background-color: #aaa; cursor: pointer; } #data tr:nth-child(even){ background-color: #ccc; } #data th{ background: #fff; } </style> </head> <body style="background-color:cornsilk"> <form action="<?php echo "mahasiswa.php"?>" method="post"> <fieldset align="center" style="border:5px solid black;margin:0px 500px"> <legend>Data</legend> <table> <tr> <td>NIM</td> <td><input min="0" type="number" name="nim"></td> </tr> <tr> <td>Nama</td> <td><input type="text" name="nama"></td> </tr> <tr> <td>Jurusan</td> <td><select name="jurusan" style="width:173px"> <option value="Sistem Informasi">Sistem Informasi</option> <option value="Teknik Industri">Teknik Industri</option> <option value="Teknik Informatika">Teknik Informatika</option> <option value="Ilmu Komputasi">Ilmu Komputasi</option> <option value="Sistem Komputer">Sistem Komputer</option> </select></td> </tr> <tr> <td><input type="submit" name="submit" value="INPUT"></td> <td><input type="button" name="update" value="DELETE" disabled></td> <td><input type="button" name="delete" value="UPDATE" disabled></td> </tr> </table> </fieldset> </form> <table id="data" border="1" style="margin-top:50px" align="center"> <caption align="center">Tabel Data Mahasiswa</caption> <tr style="text-align:center"> <strong> <th width="150px">NIM</th> <th width="250px">Nama</th> <th width="250px">Jurusan</th> </strong> </tr> <?php $conn = mysqli_connect("localhost", "root", "root", "belajar"); $query = "select * from mahasiswa"; $result = mysqli_query($conn, $query); if($result){ while($row = mysqli_fetch_row($result)){ ?> <tr onclick="settext(this.index)"> <td><?php echo $row[0]?></td> <td><?php echo $row[1]?></td> <td><?php echo $row[2]?></td> </tr> <?php } mysqli_free_result($result); mysqli_close($conn); } ?> </table> <script> function settext(x) { var table = document.getElementById('id'); var a = table.rows[x].cells[0].innerHTML; var b = table.rows[x].cells[1].innerHTML; var c = table.rows[x].cells[2].innerHTML; document.getElementByName('nim').value = parseInt(a); document.getElementById('nama').value = b; var opt = document.createElement('option'); opt.value = c; opt.innerHTML = c; document.getElementById('jurusan').appendChild(opt); } </script> </body> </html> 来对数据库进行编组/解组我的对象。它需要DynamoDBMarshaller<T> Spring bean,它需要在实例化后注入MyAttributeMarshaller类。但是,由于此类是通过@DynamoDBMarshalling()注释创建的,因此它不受Spring管理。有没有办法让我的CustomMarshaller spring bean在MyAttributeMarshaller类中可用?

CustomMarshaller

我的marsher班..

@DynamoDBTable(tableName = "MyTable")
public class ObjectInTable 
{
  @DynamoDBAttribute(attributeName = "myAttribute")
  @DynamoDBMarshalling(marshallerClass = MyAttributeMarshaller.class)
  public MyAttribute getMyAttribute() { ... }
  public MyAttribute setMyAttribute(MyAttribute o) { ... }
}

** 更新 **

我找到了一个似乎首先创建一个Spring bean的解决方案:

@Named
public class MyAttributeMarshaller implements DynamoDBMarshaller<MyAttribute>
{
    @Override
    public String marshall(MyAttribute o)
    {
        return marshaller.marshall(o);
    }
    @Override
    public ProviderContentReference unmarshall(Class<ProviderContentReference> clazz, String obj)
    {
        return marshaller.unmarshall(o);
    }
    @Inject
    private CustomMarshaller marshaller; // ERROR: null
}

然后通过以下方式检索非Spring管理@Named public class SpringContext implements ApplicationContextAware { private static ApplicationContext context; public void setApplicationContext(ApplicationContext context) throws BeansException { this.context = context; } public static ApplicationContext getApplicationContext() { return context; } } 中的自定义Marshaller

MyAttributeMarshaller

1 个答案:

答案 0 :(得分:0)

我不敢。您要求DynamoDB使用该Marshaller类的特定实例,该实例已由Spring注入CustomMarshaller。

你可以将一个CustomMarshaller实例放在一个静态的地方/字段中,然后从MyAttributeMarshaller中取出它,但这是一个黑暗的镜头,因为发布的代码不足以说肯定