我正在尝试设置spring xml配置,而不必创建更多persistence.xml.但是我经常遇到以下异常,即使我在数据库中包含了数据库属性spring.xml
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [C:\Users\me\workspace\app\src\main\webapp\WEB-INF\applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
Run Code Online (Sandbox Code Playgroud)
spring.xml:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
我正在使用spring-boot并想自动导入src/main/resources/applicationContext.xml文件.
到目前为止它只有在我明确告诉spring导入它时才有效:
@EnableAutoConfiguration
@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
Run Code Online (Sandbox Code Playgroud)
但是spring-boot有很多默认值,也许有人知道app.xml文件的"默认"名称,所以默认情况下它会被spring-boot选中?
我正在使用spring-boot并添加spring-web依赖于maven pom,以便利用RestTemplate.
现在春天试图初始化一个EmbeddedServletContext.我该怎样预防呢?
Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
... 8 more
Run Code Online (Sandbox Code Playgroud) 我正在使用spring-boot和@Scheduled注释来执行某些任务.
如何在spring-boot中找出默认的计划任务的默认池大小?
原因:以下类不并行执行作业,而是一个接一个地执行.也许默认情况下只配置一个线程执行程序?
@Service
public class ZipFileTesterAsync {
@Scheduled(fixedDelay = 60000, initialDelay = 500)
public void run() throws Exception {
System.out.println("import 1");
TimeUnit.MINUTES.sleep(1);
System.out.println("import 1 finished");
}
@Scheduled(fixedDelay = 60000, initialDelay = 1000)
public void run2() throws Exception {
System.out.println("import 2");
TimeUnit.MINUTES.sleep(1);
}
}
Run Code Online (Sandbox Code Playgroud)
结果:第一个完成后执行第二个作业.
我有一个LocalContainerEntityManagerFactoryBean为EntityManager实例.
要快速删除整个表的内容,我想运行以下代码:
@Service
public class DatabaseService {
@Autowired
private EntityManager em;
@Transactional
public void clear() {
em.createNativeQuery("TRUNCATE TABLE MyTable").executeUpdate();
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
ERROR org.springframework.integration.handler.LoggingHandler: javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Run Code Online (Sandbox Code Playgroud)
如果我做了这个改变:
public …Run Code Online (Sandbox Code Playgroud) 我正在使用spring-boot-starter-security依赖项,以使用随附的几个类spring-security.但是,由于我想将它集成到现有的vaadin应用程序中,我只想使用类,而不是使用spring的默认登录/验证屏幕.
如何禁用此屏幕?
我不能通过扩展WebSecurityConfigurerAdapter作为我的主要入门类来进行任何配置extends SpringBootServletInitializer.此外,vaadin应用程序基本上始终在相同的URL路径上运行,并使用内部导航.
@EnableAutoConfiguration
public class MyApp extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApp.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
那么,我该怎么做才能禁用登录界面,但是虽然使用了弹簧安全功能?
我在CrudRepository整个申请过程中都使用了spring .现在我想创建一个@Entity没有的@Id.这有可能吗?
//probably ID is always required?
public interface Repository<T, ID extends Serializable>
Run Code Online (Sandbox Code Playgroud) 我正在启动嵌入式tomcat via,spring-boot并希望将静态index.html页面作为正在运行的应用程序的一部分.
但以下不起作用:
@SpringBootApplication
public class HMyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@RestController
public class HomeContoller {
@RequestMapping("/")
public String index() {
return "index";
}
}
src/main/resources/static/index.html
Run Code Online (Sandbox Code Playgroud)
结果:当我打电话时localhost:8080,我只看到"索引"这个词,但不是我的html页面.为什么?
@org.hibernate.annotations.ForeignKey 已被弃用,但我找不到任何例子JPA 2.1等价物的样子?
@ManyToOne
@ForeignKey(name = "FK_USER") //@deprecated Prefer the JPA 2.1 introduced {@link javax.persistence.ForeignKey} instead.
private User user;
Run Code Online (Sandbox Code Playgroud)
如果没有弃用的注释,如何实现?
我String.format用来创建带参数的格式化字符串.是否有可能告诉格式化程序多次重用一个参数?
String.format(%s FOO %s %s, "test"); //desired output: "test FOO test test"
Run Code Online (Sandbox Code Playgroud) java ×10
spring ×8
spring-boot ×5
jpa ×3
hibernate ×2
spring-data ×1
spring-mvc ×1
spring-web ×1
string ×1
vaadin ×1