繁体   English   中英

Spring开机:将class转成bean

[英]Spring boot: Convert class into bean

spring 引导的新手。 我有一个正在实现接口的 class,我想将此 class 转换为 bean。 有没有办法做到这一点?

这是 class:

public class UnitTestContextProvider implements MockDataProvider {

    @Override
    public MockResult[] execute(MockExecuteContext ctx) throws SQLException {

        // You might need a DSLContext to create org.jooq.Result and org.jooq.Record objects
        DSLContext create = DSL.using(SQLDialect.POSTGRES);
        MockResult[] mock = new MockResult[1];

        // The execute context contains SQL string(s), bind values, and other meta-data
        String sql = ctx.sql();

        // Exceptions are propagated through the JDBC and jOOQ APIs
        if (sql.toUpperCase().startsWith("DROP")) {
            throw new SQLException("Statement not supported: " + sql);
        }

        // You decide, whether any given statement returns results, and how many
        else if (sql.toUpperCase().startsWith("SELECT")) {

            // Always return one record
            Result<Record2<UUID, String>> result = create.newResult(CLIENT.CLIENT_ID, CLIENT.CLIENT_NAME);
            result.add(create
                    .newRecord(CLIENT.CLIENT_ID,CLIENT.CLIENT_NAME)
                    .values(UUID.fromString("88ccc2c2-492f-4f03-9676-3c39e0c51514"), "Orwell"));
            mock[0] = new MockResult(1, result);
        }

        // You can detect batch statements easily
        else if (ctx.batch()) {
            // [...]
        }

        return mock;
    }
}

这就是我正在尝试的:

 @Configuration
 public class ContextProviders{

    @Bean
    public UnitTestContextProvider unitTestContext() {
      //This is similar to the class described above.
    }
 }

我的主要困惑是如何处理接口部分。

将您的配置更改为以下。 您需要使用@Bean注释对其进行注释。 现在您将能够使用@Autowired@Inject注释将其注入另一个 spring 托管 bean。

@Configuration
public class ContextProviders{
    @Bean
    public MockDataProvider unitTestContext() {
        return new UnitTestContextProvider();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM