我想新建一个项目,例如mybatis-spring-boot-starter,我使用springboot 2.3.2,但是出了点问题。 首先,我定义如下的BatisProperties.java:
@ConfigurationProperties(prefix = BatisProperties.MYBATIS_PREFIX)
public class BatisProperties {
public static final String MYBATIS_PREFIX = "batis";
private String MyClassName;
...
}
然后是BatisAutoConfiguration.java
@Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@EnableConfigurationProperties(BatisProperties.class)
public class BatisAutoConfiguration implements InitializingBean {
private final BatisProperties properties;
@Override
public void afterPropertiesSet() throws Exception {
checkConfigFileExists();
}
private void checkConfigFileExists() {
System.out.println(this.properties.getMyClassName());//null here
if (this.properties.isCheckConfigLocation() && //code from mybatis-spring-booot-starter
StringUtils.hasText(this.properties.getConfigLocation())) {
Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
Assert.state(resource.exists(),
"Cannot find config location: " + resource + " (please add config file or check your Mybatis configuration)");
}
}
在/resource/META_INF/spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
xxx.BatisAutoConfiguration
上面所有内容都在一个启动程序中,我将其编译为一个jar文件,并在另一个项目(项目TWO)中使用此jar文件,在项目TWO中,我在/ resource目录中定义了.properties或.yml,内容为:< / p>
batis.my-class-name=xxxx.xxxx
最后是一个DemoApplication.java或Test.java,如下所示:
DemoApplication.java
@SpringBootApplication
public class DemoApplication {
@Autowired
private BatisProperties properties;
private static BatisProperties batisProperties;
@PostConstruct
public void init() {
batisProperties = properties;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println(batisProperties.toString());//xxxx.BatisProperties@436390f4
System.out.println(batisProperties.getMyClassName());//null
}
}
Test.java
@SpringBootTest
class Test {
@Autowired
private BatisProperties properties;
@Test
void contextLoads() {
System.out.println(properties);// some bean in Spring IOC
System.out.println(properties.getDatasourceConfigProviderClassName());// null
}
}
上面的注释是结果:我们可以在Spring IOC中找到BatisProperties Bean,但是所有属性都为空。
那么有人可以提供帮助吗?我不知道这是否是由SpringBoot版本引起的
答案 0 :(得分:0)
我将BatisProperties类改进为:
@Configuration
@ConfigurationProperties(prefix = "batis")
public class BatisProperties {
private String MyClassName;
...
// Make sure you have getters and setter for properties!
// If you use Lombok, put @Data on this class.
...
}
不需要显式具有BatisAutoConfiguration.java。只需让Spring为您初始化它即可。完全删除类BatisAutoConfiguration.java。另外,请勿对属性类使用静态字段。
尝试以下操作:
@SpringBootApplication
@EnableAutoConfiguration
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
或更明确,但不需要:
@SpringBootApplication
@ComponentScan(basePackages = {"my.app.org"}) // prefix, where BatisProperties is
@EnableAutoConfiguration
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
或:
@SpringBootApplication
@EnableAutoConfiguration
@Import(BatisProperties.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
BatisProperties应该在您的应用中的任何位置都可用。