我正在使用Intellij IDE来编写spring Boot代码.
Spring Initializr在new project
选项中不适用于我.
您可以在下面找到我的IDE的屏幕截图.请让我知道我在这里缺少什么?
我有这个Spring Data CrudRepository
来处理数据库上的CRUD操作.
@Repository
public interface IUserRepository extends CrudRepository<User, String> {
}
Run Code Online (Sandbox Code Playgroud)
User
是我的数据库的用户表的实体.CrudRepository
将以下操作添加到存储库:
delete(String ID)
findOne(String ID)
save(User user)
如文档中所述IllegalArgumentException
,如果给定的id为null ,则delete和find操作抛出,而save操作不会抛出任何异常.
问题是CrudRepository的javadoc没有提到这些操作抛出的其他异常.例如,如果DB中不存在提供的ID ,则不会告诉delete(String ID)
操作抛出该操作EmptyResultDataAccessException
.
在save(User user)
操作的javadoc中,如果插入一个破坏一个数据完整性约束的新用户(在唯一字段和外键上),则不清楚抛出哪些异常.此外,它不会警告您是否正在编写新用户或现有用户:它只是创建一个新用户或覆盖(如果存在)(因此它是一个插入+更新操作).
在企业应用程序中,我应该能够捕获操作可以抛出的每个可抛出的异常,我应该在操作的javadoc中读到它.
您是否知道有关CrudRepository异常的任何明确文档?
谢谢
我开始玩弹簧靴,我有新的安装问题.
我使用http://start.spring.io创建了初始项目..这是我的build.gradle:
buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'springboot-demo1'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-jdbc")
runtime("mysql:mysql-connector-java")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
Run Code Online (Sandbox Code Playgroud)
当我在命令行中执行时: …
我正在使用Spring和EhCache
我有以下方法
@Override
@Cacheable(value="products", key="#root.target.PRODUCTS")
public Set<Product> findAll() {
return new LinkedHashSet<>(this.productRepository.findAll());
}
Run Code Online (Sandbox Code Playgroud)
我有其他使用@Cacheable和@CachePut以及@CacheEvict的方法.
现在,假设数据库返回100个产品并且它们被缓存key="#root.target.PRODUCTS"
,然后其他方法将插入 - 更新 - 删除项目到数据库中.因此,通过它缓存的产品key="#root.target.PRODUCTS"
不再相同,例如数据库.
我的意思是,检查以下两种方法,他们能够更新/删除一个项目,同一项目缓存在另一个key="#root.target.PRODUCTS"
@Override
@CachePut(value="products", key="#product.id")
public Product update(Product product) {
return this.productRepository.save(product);
}
@Override
@CacheEvict(value="products", key="#id")
public void delete(Integer id) {
this.productRepository.delete(id);
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以通过更新/删除位于缓存中的项目key="#root.target.PRODUCTS"
,如果产品已更新,则为100,如果删除了产品,则为499.
我的观点是,我想避免以下情况:
@Override
@CachePut(value="products", key="#product.id")
@CacheEvict(value="products", key="#root.target.PRODUCTS")
public Product update(Product product) {
return this.productRepository.save(product);
}
@Override
@Caching(evict={
@CacheEvict(value="products", key="#id"),
@CacheEvict(value="products", key="#root.target.PRODUCTS")
})
public void delete(Integer id) {
this.productRepository.delete(id); …
Run Code Online (Sandbox Code Playgroud) 当我的缓存键使用@Cacheable
注释在Spring中发生冲突时,我遇到了问题.例如,使用以下两种方法:
@Cacheable("doOneThing")
public void doOneThing(String name) {
// do something with name
}
@Cacheable("doAnotherThing")
public void doAnotherThing(String name) {
// do some other thing with name
}
Run Code Online (Sandbox Code Playgroud)
这是我的缓存配置,其中我添加了一个keyGenerator
和一个cacheManager
bean:
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public JedisConnectionFactory redisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(cf);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
return new RedisCacheManager(redisTemplate);
}
@Bean
public KeyGenerator …
Run Code Online (Sandbox Code Playgroud) 与spring框架中的提交有关https://github.com/spring-projects/spring-framework/commit/5aefcc802ef05abc51bbfbeb4a78b3032ff9eee3
初始化设置为从afterPropertiesSet()到afterSingletonsInstantiated()的后续阶段
简而言之:这可以防止在@PostConstruct用例中使用缓存时缓存工作.
更长的版本:这可以防止您使用的用例
在methodB上使用@Cacheable创建serviceB
使用@PostConstruct调用serviceB.methodB创建serviceA
@Component
public class ServiceA{
@Autowired
private ServiceB serviceB;
@PostConstruct
public void init() {
List<String> list = serviceB.loadSomething();
}
Run Code Online (Sandbox Code Playgroud)这导致org.springframework.cache.interceptor.CacheAspectSupport现在不进行初始化,因此不会缓存结果.
protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args) {
// check whether aspect is enabled
// to cope with cases where the AJ is pulled in automatically
if (this.initialized) {
//>>>>>>>>>>>> NOT Being called
Class<?> targetClass = getTargetClass(target);
Collection<CacheOperation> operations = getCacheOperationSource().getCacheOperations(method, targetClass);
if (!CollectionUtils.isEmpty(operations)) { …
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚当前打开了多少个连接,但我似乎找不到使用 Hikari 的明显方法。
HikariPool
公开该信息 ( getActiveConnections
) 但我没有看到从HikariDataSource
.
我有一个返回列表的方法,我想根据传递的参数缓存它。参数是 4 和整数,我如何使用 SpEL 对其进行配置?我正在使用 spring 版本 4.0.6.RELEASE。
我已经建立了一个通过注释触发的注释处理器com.foo.FooEntity
。有必要能够创建更多的构造型,以触发该注释处理器。
例如,控制器还应该触发此注释处理器。我想知道是否有一种方法可以@FooEntity
在上面放置注释。就像是:
@FooEntity
@Target(TYPE)
@Retention(RUNTIME)
public @interface Controller {}
Run Code Online (Sandbox Code Playgroud)
并使用它,以便此类触发注释处理
@Controller
public class MyController { ... }
Run Code Online (Sandbox Code Playgroud)
当然,这里的想法是我想添加新的构造型,而不必接触注释处理器本身。
我已经安装了sts但是创建了一个新的roo项目并不存在,我需要做些什么才能添加spring roo?在之前的版本中我可以创建它但不在3.5.0中
java ×8
spring ×7
spring-cache ×4
caching ×2
annotations ×1
ehcache ×1
gradle ×1
hikaricp ×1
spring-3 ×1
spring-boot ×1
spring-data ×1
spring-el ×1
spring-roo ×1