目前我有一个使用Spring Data REST的Spring Boot应用程序.我有一个与另一个域实体Post
有@OneToMany
关系的域实体Comment
.这些类的结构如下:
Post.java:
@Entity
public class Post {
@Id
@GeneratedValue
private long id;
private String author;
private String content;
private String title;
@OneToMany
private List<Comment> comments;
// Standard getters and setters...
}
Run Code Online (Sandbox Code Playgroud)
Comment.java:
@Entity
public class Comment {
@Id
@GeneratedValue
private long id;
private String author;
private String content;
@ManyToOne
private Post post;
// Standard getters and setters...
}
Run Code Online (Sandbox Code Playgroud)
他们的Spring Data REST JPA存储库是以下基本实现CrudRepository
:
PostRepository.java:
public interface PostRepository extends CrudRepository<Post, Long> …
Run Code Online (Sandbox Code Playgroud) 我想利用XML配置文件中的一些Spring Boot自动配置的bean,但是当我尝试这样做时,我仍然遇到异常和错误.
例如,如果我的类路径上有数据相关的库,Spring Boot将自动配置一个DataSource
对象,我可以将其自动装入我自己的bean和类中,如下所示:
@Configuration
@ImportResource("classpath:xmlconfig.xml")
public class Config {
// This works!!
@Autowired
private DataSource dataSource;
@Bean
public ClassThatRequiresADataSource() {
ClassThatRequiresADataSource foo = new ClassThatRequiresADataSource();
foo.setDataSource(dataSource);
return foo;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试在XML配置文件中执行相同操作,我将得到一个例外.我已经通过添加@ImportResource("classpath:xmlconfig.xml")
到我的主配置类来引导XML配置文件.这是我正在谈论的一个例子......内部xmlconfig.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- THIS DOES NOT WORK! -->
<bean id="anotherClassThatRequiresADataSource" class="my.package.AnotherClassThatRequiresADataSource">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
尽管dataSource
是一个有效的,自动配置的Bean名称,上面将在运行Spring Boot应用程序时给出异常.我也尝试过自动配置ConnectionFactory
(在类路径上使用ActiveMQ)和类路径上EntityManagerFactory
使用Hibernate和JPA,但这些都不起作用.
基本上,我要问的是:什么相当于将Spring Boot自动配置的bean自动装配到XML配置文件中?
这是我的主要Spring Boot入口点,只是所有文档中列出的标准类:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public …
Run Code Online (Sandbox Code Playgroud) 我们假设我有一个集合(让我们使用一个集合):
scala> val x = Set(1, 2, 3)
x: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
我可以使用以下代码获得所有成对组合:
scala> for {
| a <- x
| b <- x
| if a != b
| } yield (a, b)
res9: scala.collection.immutable.Set[(Int, Int)] = Set((3,1), (3,2), (1,3), (2,3), (1,2), (2,1))
Run Code Online (Sandbox Code Playgroud)
问题是我只想获得忽略顺序的所有成对组合(因此组合(1, 2)
相当于(2, 1)
).所以我想回来Set((3, 1), (3, 2), (1, 2))
.
不要假设集合的元素是整数.它们可以是任意类型.
有任何想法吗?
编辑:Python itertools.combinations
执行我正在寻找的确切功能.我只想在Scala中使用惯用的方法:)
我有以下(简化)条件需要验证我正在编写的表单:
a> b
a> c
a> d
b> c
b> d
c> d
从图形上看,这可以看作:
用户可以自由输入a,b,c和d的值,这就是为什么需要对它们进行验证以确保它们遵守这些规则的原因.我遇到的问题是写一些清楚有效地评估每个陈述的东西.当然,最明显的方法是将每个语句分别作为if语句进行评估.不幸的是,这会占用很多代码行,而且我宁愿避免使用一堆if-blocks来完成同样的事情.我确实使用数组提出了以下嵌套的for循环解决方案.我构建了一个值数组,并将其循环两次(在类似Python的伪代码中演示):
A = [a, b, c, d]
for i in range(3):
for j in range(i, 4):
if i > j and A[i] >= A[j]:
print("A[i] must be less than A[j]")
else if i < j and A[i] <= A[j]:
print("A[j] must be greater than A[i]")
Run Code Online (Sandbox Code Playgroud)
我对这个解决方案的问题是难以阅读和理解 - 解决方案并不清楚.
我有这种唠叨的感觉,那里有一个更好,更清晰的答案,但我无法想到它对我的生活.这不是家庭作业或任何事情 - 我实际上正在研究一个项目,这个问题(或它的微妙变化)不止一次出现,所以我想制作一个清晰有效的可重用解决方案.任何输入将不胜感激.谢谢!
spring ×2
spring-boot ×2
algorithm ×1
c# ×1
java ×1
nested-loops ×1
performance ×1
scala ×1
validation ×1