春季启动:将类转换为bean

时间:2020-06-19 15:34:51

标签: java spring spring-boot

Spring Boot的新手。我有一个正在实现接口的类,我想将该类转换为bean。 有办法吗?

这是课程:

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.
    }
 }

我的主要困惑是如何处理界面部分。

1 个答案:

答案 0 :(得分:2)

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

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