我正在使用注释来配置我的弹簧环境,如下所示:
@Configuration
...
@PropertySource("classpath:/config/default.properties")
...
public class GeneralApplicationConfiguration implements WebApplicationInitializer
{
@Autowired
Environment env;
}
Run Code Online (Sandbox Code Playgroud)
这导致我的default.properties属性成为Environment的一部分.我想在这里使用@PropertySource机制,因为它已经提供了基于环境设置(例如config_dir位置)通过几个回退层和不同动态位置来重载属性的可能性.我只是删除了后备以使示例更容易.
但是,我现在的问题是我想在default.properties中配置我的数据源属性.您可以将设置传递给数据源,而无需详细了解数据源期望使用的设置
Properties p = ...
datasource.setProperties(p);
Run Code Online (Sandbox Code Playgroud)
但问题是,Environment对象既不是Properties对象也不是Map,也不是任何可比较的对象.从我的角度来看,根本无法访问环境的所有值,因为没有keySet或iterator方法或任何可比较的方法.
Properties p <=== Environment env?
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?是否有可能以某种方式访问Environment对象的所有条目?如果是,我可以将条目映射到Map或Properties对象,我甚至可以通过前缀过滤或映射它们 - 创建子集作为标准java Map ...这就是我想要做的.有什么建议?
如果在TestCase类中有这样的注释:
@SpringApplicationConfiguration(classes = {Application.class})
Run Code Online (Sandbox Code Playgroud)
这将导致Application.class实现CommandLineRunner接口运行所需的方法
public void run(String... args) throws Exception
Run Code Online (Sandbox Code Playgroud)
我仍然认为这主要是一种不想要的行为,因为在您的测试环境中,您可能不想启动整个应用程序.
我想到了解决这个问题的两个解决方案:
CommandLineRunner从我的Application班级中删除界面这个解决方案都需要大量的编码.你有更方便的解决方案吗?
实现BeanPostProcessor接口和在Spring中的XML配置文件中使用init/destroy方法属性或实现InitializingBean/DisposableBean接口有什么区别?
这个春季启动错误意味着什么?
2016-07-04 21:53:53 [restartedMain] ERROR o.s.boot.SpringApplication - Application startup failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name org.springframework.boot.autoconfigure.session.SessionAutoConfiguration$SessionRepositoryValidator': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No session repository could be auto-configured, check your configuration (session store type is 'null')
Run Code Online (Sandbox Code Playgroud) 我试图理解之间的差异BeanFactoryPostProcessor和BeanPostProcessor.
我理解它BeanFactoryPostProcessor在bean定义上运行,即在bean实例创建之前,它会被执行并BeanPostProcessor在实例化bean并调用生命周期事件后执行.
这是否意味着BeanFactoryPostProcessor不是BeanPostProcessorSpring生命周期事件的一部分,因为它是在实例化之前调用的,而Spring是Spring生命周期事件的一部分吗?请核实我的理解是否正确.
我是Spring Boot的新手,在运行我的应用程序时遇到错误.我正在关注一个教程,我相信我有适当的父母和POM的依赖,请帮助我
主要课程:
package com.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, "hello");
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat servlet container
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:165) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at com.boot.App.main(App.java:18) [classes/:na]Caused …Run Code Online (Sandbox Code Playgroud) 我想使用spring boot来启动我的应用程序,但是在我在pom.xml中添加一些相关jar后,它会返回此错误:我感谢它可能是由一些冲突罐引起的?
Application.java
package com.mm.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@ComponentScan("com.mm.controller")
@Configuration
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
//spring properties xml
ApplicationContext context = new ClassPathXmlApplicationContext(
"Spring-Module.xml");
SpringApplication.run(Application.class, args);
}
@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
Error starting ApplicationContext. To display the auto-configuration report enabled debug logging (start with --debug)
Exception in thread …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用Google guice,现在我尝试将框架完全转换为SpringBoot.
我为persistence.xml配置了Bean,如下所示
@Autowired
@Bean(name = "transactionManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
{
LocalContainerEntityManagerFactoryBean lEMF = new LocalContainerEntityManagerFactoryBean();
lEMF.setPersistenceUnitName("leaseManagementPU");
lEMF.setPersistenceXmlLocation("persistence.xml");
return lEMF;
}
Run Code Online (Sandbox Code Playgroud)
现在我需要配置(注入)EntityManager em,以执行像em.persist(),em.find等JPA操作...我如何配置,也有人试图用示例代码解释这个
有什么用的BeanNameAware和BeanFactoryAware?我正在研究spring并遇到了这两个接口.我用谷歌搜索了他们但没有任何用处.请告诉我什么是BeanNameAware和BeanFactoryAware接口的功能以及何时使用它们?
我使用keycloak保证我的休息API,我跟着这个教程以编程方式添加用户,但我得到这个错误信息:
ERROR [io.undertow.request] (default task-9) UT005023: Exception handling request to /service/secured: org.jboss.resteasy.spi.UnhandledException: javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "access_token" (class org.keycloak.representations.AccessTokenResponse), not marked as ignorable (9 known properties: "notBeforePolicy", "otherClaims", "tokenType", "token", "expiresIn", "sessionState", "refreshExpiresIn", "idToken", "refreshToken"])
at [Source: org.apache.http.conn.EofSensorInputStream@9d6aba2; line: 1, column: 18] (through reference chain: org.keycloak.representations.AccessTokenResponse["access_token"])
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76)
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:149)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:372)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.keycloak.adapters.undertow.UndertowAuthenticatedActionsHandler.handleRequest(UndertowAuthenticatedActionsHandler.java:66)
at …Run Code Online (Sandbox Code Playgroud) spring ×7
java ×5
spring-boot ×5
spring-mvc ×2
jackson ×1
keycloak ×1
repository ×1
resteasy ×1
session ×1
tomcat ×1