小编mem*_*und的帖子

如何在没有persistence.xml的情况下配置Spring?

我正在尝试设置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)

我在这里错过了什么?

java configuration spring hibernate jpa

31
推荐指数
2
解决办法
5万
查看次数

Spring-boot自动导入applicationContext.xml?

我正在使用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选中?

java spring spring-boot

30
推荐指数
1
解决办法
5万
查看次数

如何防止弹簧网的弹簧自动配置?

我正在使用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)

java spring spring-boot spring-web

30
推荐指数
4
解决办法
4万
查看次数

spring-boot中的默认调度程序池大小是多少?

我正在使用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)

结果:第一个完成后执行第二个作业.

java spring spring-boot

27
推荐指数
3
解决办法
2万
查看次数

如何在Spring中的共享EntityManager上手动启动事务?

我有一个LocalContainerEntityManagerFactoryBeanEntityManager实例.

要快速删除整个表的内容,我想运行以下代码:

@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)

java spring entitymanager spring-data-jpa

26
推荐指数
3
解决办法
3万
查看次数

如何禁用spring-security登录界面?

我正在使用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)

那么,我该怎么做才能禁用登录界面,但是虽然使用了弹簧安全功能?

java spring spring-security vaadin spring-boot

24
推荐指数
6
解决办法
3万
查看次数

如何在没有@Id的情况下使用spring Repository?

我在CrudRepository整个申请过程中都使用了spring .现在我想创建一个@Entity没有的@Id.这有可能吗?

//probably ID is always required?
public interface Repository<T, ID extends Serializable>
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-data

24
推荐指数
1
解决办法
3万
查看次数

如何在spring-boot中提供静态html内容页面

我正在启动嵌入式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页面.为什么?

java spring spring-mvc spring-boot

24
推荐指数
2
解决办法
4万
查看次数

自JPA 2.1以来如何命名ManyToOne引用的外键约束?

@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)

如果没有弃用的注释,如何实现?

java hibernate jpa

22
推荐指数
2
解决办法
3万
查看次数

Stringformatter可以重用参数吗?

String.format用来创建带参数的格式化字符串.是否有可能告诉格式化程序多次重用一个参数?

String.format(%s FOO %s %s, "test"); //desired output: "test FOO test test"
Run Code Online (Sandbox Code Playgroud)

java string

21
推荐指数
3
解决办法
4049
查看次数