我正在使用Gradle spring-boot插件,我需要为测试运行选择一个弹簧活动配置文件.
如何将spring.profiles.active系统属性传递给bootRun插件的任务?
什么已经失败:
task bootRunLocal {
systemProperty "spring.profiles.active", "local"
System.setProperty("spring.profiles.active", "local")
tasks.bootRun.execute() // I suspect that this task is executed in a separate JVM
}
Run Code Online (Sandbox Code Playgroud)
并且一些命令行魔法也失败了:
./gradle -Dspring.profiles.active=local bootRun
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我解决我的烦恼吗?
从答案和评论中更新:
我可以设置systemProperty并通过执行以下操作将其传递给spring容器:
run {
systemProperty "spring.profiles.active", "local"
}
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,正在为bootRun任务和bootRunLocal任务设置本地配置文件.我需要一种方法来为bootRunLocal任务和调用booRun任务设置此属性bootRunLocal.
这可能听起来很简单,但我来自Maven结构化世界的和平.
Validator尝试使用JSR-303(hibernate-validator)验证模型时,我在注入spring应用程序bean时遇到问题
我的主要配置类是:
@EnableAutoConfiguration
@EnableWebMvc // <---
@EnableJpaRepositories("com.example")
@EntityScan("com.example")
public class MainConfiguration {
Run Code Online (Sandbox Code Playgroud)
根据javadocs:
/**
* Provide a custom {@link Validator} instead of the one created by default.
* The default implementation, assuming JSR-303 is on the classpath, is:
* {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}.
* Leave the return value as {@code null} to keep the default.
*/
Validator getValidator();
Run Code Online (Sandbox Code Playgroud)
Hibernate-validator在类路径上.我正在尝试将其注入存储库:
@Repository
public class UserRepositoryImpl implements UserRepositoryCustom {
@Autowired
private Validator validator;
Run Code Online (Sandbox Code Playgroud)
抛出异常:
No qualifying bean of type [javax.validation.Validator] found for dependency:
Run Code Online (Sandbox Code Playgroud)
更新: …
我正在寻找一个完全启用/禁用所有 JAVA断点的快捷方式(类似于在Debug视图中按"Skip all breakpoints"按钮),而不是逐行启用/禁用它们.
使用JavaConfig我在查找@RepositorySpring bean时遇到问题.
存储库接口定义如下:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
Run Code Online (Sandbox Code Playgroud)
配置定义如下:
@Configuration
@ComponentScan("com.example")
@EnableAutoConfiguration
@EnableJpaRepositories("com.example")
public class SampleApplication extends SpringBootServletInitializer {
...
Run Code Online (Sandbox Code Playgroud)
包结构如下所示:
com.example
configuration
SampleApplication
repository
UserRepository
Run Code Online (Sandbox Code Playgroud)
在日志文件中,我看到存储库被认为是bean定义的候选者,但是:
ClassPathBeanDefinitionScanner | Ignored because not a concrete top-level class:
Run Code Online (Sandbox Code Playgroud)
趣味事实
如果我将SampleApplication类移动到com.example包,一切都开始工作.
我缺少什么想法?
我们有一个基于Java的客户端/服务器项目.最近,其中一位测试人员在测试应用程序时发现了SQL注入漏洞.
我们没有足够的资源来手动检查SQL注入的应用程序.
是否有任何SQL注入查找器/静态代码分析器可以在Java代码中找到SQL漏洞?
我在处理 Grails 中的代理对象时遇到了困难。假设我有以下内容
class Order {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="xxx", joinColumns = {@JoinColumn(name = "xxx")}, inverseJoinColumns = {@JoinColumn(name = "yyy")})
@OrderBy("id")
@Fetch(FetchMode.SUBSELECT)
private List<OrderItem> items;
}
class Customer {
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "xxx",insertable = false, nullable = false)
private OrderItem lastItem;
private Long lastOrderId;
}
Run Code Online (Sandbox Code Playgroud)
在一些控制器类中
//this all happens during one hibernate session.
def currentCustomer = Customer.findById(id)
//at this point currentCustomer.lastItem is a javassist proxy
def lastOrder = Order.findById(current.lastOrderId)
//lastOrder.items is a …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 CDI 将 EntityManager 注入到我的应用程序中,但在尝试使用它时 EntityManager 为空。
这是我的代码,我遵循了几个有关如何注入 EntityManager 的教程,并且我使用与这些教程中相同的代码。
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface DevDatabase {
}
@Singleton
public class JPAResourceProducer {
@Produces
@PersistenceContext(unitName = "DevPU")
@DevDatabase
private EntityManager em;
}
Run Code Online (Sandbox Code Playgroud)
持久化.xml
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="DevPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entity.MyEntity</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/MyDB"/>
<property name="javax.persistence.jdbc.user" value="appuser"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.password" value="apppassword"/>
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
这就是我在 DAO 中使用它的方式
public abstract class GenericDAO<T> {
@DevDatabase
@Inject
private EntityManager em;
private final Class<T> entityClass;
public GenericDAO(Class<T> …Run Code Online (Sandbox Code Playgroud) 我在Utility类中有一个静态方法:
public static final <T extends Foo & IBar> Foo1<T> getBaz(Class<T> fooAndIBarClazz)
Run Code Online (Sandbox Code Playgroud)
我有另一个班:
public class FooBar<T extends Foo> {
private Class<T> fooClazz;
//...
}
Run Code Online (Sandbox Code Playgroud)
从FooBar我要调用的实例内部Utility.getBaz()
public void aMethod() {
Utility.getBaz(fooClazz); // fails with not a valid substitute for the bounded parameter
//Utility.<IBar>getBaz(fooClazz); // fails as well
}
Run Code Online (Sandbox Code Playgroud)
是否可以在没有额外投射的情况下以这种通用方式调用实用程序方法?
java ×7
spring ×3
spring-boot ×3
cdi ×1
eclipse ×1
eclipse-jdt ×1
editor ×1
generics ×1
gradle ×1
grails ×1
hibernate ×1
jpa ×1
security ×1
spring-data ×1
sql ×1
validation ×1
weld ×1