0

我在快速启动应用程序中使用了 spring-data-mongodb 1.2.1-RELEASE。它工作正常,我可以连接到 mongo db 我可以创建、更新和删除集合现在我想将 wicket-framework 与这个应用程序集成。

领域类

@Document
public class Student
{
    @Id
    private String id;
    private String firstName;
    private int age;

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

}

学生存储库类

@Repository
public class StudentService 
{

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void create(Student student)
    {
        mongoTemplate.insert(student);
    }

    @Override
    public void update(Query query, Update update)
    {
        mongoTemplate.updateFirst(query, update, Student.class);
    }

    @Override
    public List<Student> findAll()
    {
        List<Student> students = mongoTemplate.findAll(Student.class);
        logger.debug("Student: {}", students);
        return students;
    }

    @Override
    public void delete(Student student)
    {
        mongoTemplate.remove(student);
    }

    @Override
    public void deleteAll()
    {
        Query searchUserQuery = new Query(Criteria.where("age").gt(0));
        mongoTemplate.remove(searchUserQuery, Student.class);
    }
}

Mongo配置类

 @Configuration
    @EnableMongoRepositories
    @ComponentScan(basePackageClasses = {MongoDBApp.class})
    @PropertySource("classpath:application.properties")
    public class MongoConfiguration extends AbstractMongoConfiguration
    {
        @Override
        protected String getDatabaseName()
        {
            return "demo";
        }  

        @Override
        public Mongo mongo() throws Exception
        {
           return new Mongo("localhost", 27017);
        }

        @Override
        protected String getMappingBasePackage()
        {
            return "mypackage";
        }
   }

Mongo Db 主类

public class MongoDBApp
{

    static final Logger logger = LoggerFactory.getLogger(MongoDBApp.class);

    public static void main(String[] args)
    {
        logger.info("Demo application");

        ApplicationContext context = new AnnotationConfigApplicationContext(MongoConfiguration.class);

        StudentService studentService = context.getBean(StudentService.class);

        Student student = new Student();
        student.setFirstName("foo");
        student.setAge(24);
        studentService.create(student);

        List<Student> students = studentService.findAll();
        logger.info("No. of students: {}", students.size());


        studentService.delete(student);
        logger.info("Deleted student: {}", student);
   }
}

pom.xml 中的依赖项

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.2.1.RELEASE</version>
</dependency>

<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2</version>
</dependency>

对于此代码,我想与 wicket 框架集成。

请帮我怎么做?

4

2 回答 2

0

如果您的 Spring/MongoDb 运行良好,您只需使用模块 wicket-spring 将其集成到 Wicket 中。您可以在此处找到此集成的示例:

http://wicketguide.comsysto.com/guide/chapter18.html#chapter18_2

源代码可在此处获得:

https://github.com/bitstorm/Wicket-tutorial-examples/tree/master/SpringInjectionExample

于 2013-10-04T12:14:25.207 回答
0

谢谢 Andrea del bence,我在 MyWicketApplication 中确实喜欢这个

@Override
public void init()
{
        super.init();
        getComponentInstantiationListeners().add(new SpringComponentInjector(this, getSpringContext()));
        // add your configuration here
}

public ApplicationContext getSpringContext()
{
        return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
}
于 2013-10-10T12:18:49.653 回答