Tag: spring

了解DI框架的需求

这可能是一个天真的问题。 我目前正在学习Spring框架和dependency injection 。 虽然DI的基本原理很容易理解,但为什么需要一个精细的框架来实现呢? 考虑以下: public abstract class Saw { public abstract void cut(String wood); } public class HandSaw extends Saw { public void cut(String wood) { // chop it up } } public class ChainSaw extends Saw { public void cut(String wood) { // chop it a lot faster } } public class […]

用于testing的embedded式H2数据库的弹簧configuration

你的Spring集成testingconfiguration看起来像使用embedded式h2数据源和JUnit(可选)? 我第一次尝试SingleConnectionDataSource基本上工作,但在更复杂的testing失败,你需要在同一时间几个连接或暂停交易。 我认为基于TCP的服务器模式下的 h2也可能工作,但这可能不是用于内存中临时embedded式数据库的最快通信模式。 有什么可能性和它们的优点/缺点? 另外,你如何创build表/填充数据库? 更新:让我们指定一些对这些testing非常重要的具体要求。 数据库应该是临时的,并在内存中 对于速度要求,连接可能不应该使用tcp 如果我可以使用数据库工具在debugging过程中检查数据库的内容,那将会很好 我们必须定义一个数据源,因为我们不能在unit testing中使用应用程序服务器数据源

Spring Boot以及如何将连接细节configuration到MongoDB?

作为Spring Boot的新手我想知道如何为MongoDBconfiguration连接细节。 我已经尝试了正常的例子,但没有涵盖连接的细节。 我想指定要使用的数据库以及运行MongoDB的主机的url / port。 任何提示或提示?

设置Spring Profilevariables

我有两个Springconfiguration文件:“dev”和“test”。 我想在服务器环境中设置活动configuration文件,我不想将其设置在我的代码中,以便在我部署应用程序的任何地方,configuration文件都将根据服务器中的configuration文件加载。 我怎样才能做到这一点?

Spring注释之间的区别

问题: 1) @Component和@Configuration之间的区别? 我已经读过,既删除了接线的XML代码的必要性,但没有得到这些之间的区别。 2)@ @Autowired ,@ @Inject和@Resource什么区别? – 什么时候使用? – 每个人有什么优点/缺点?

我如何configurationSpring和SLF4J,以便能够logging日志?

我有一个我想login的maven&spring应用程序。我很想使用SLF4J。 我想把我所有的configuration文件放到一个目录{classpath} / config中,包括log4j.xml,然后使用spring bean进行init。 例如 <bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="org.springframework.util.Log4jConfigurer"/> <property name="targetMethod" value="initLogging"/> <property name="arguments"> <list> <value>classpath:config/log4j.xml</value> </list> </property> </bean> 但是,我得到这个警告,并没有logging。 log4j:WARNlogging器(org.springframework.context.support.ClassPathXmlApplicationContext)找不到appender。 log4j:WARN请正确初始化log4j系统。 log4j:WARN请参阅http://logging.apache.org/log4j/1.2/faq.html#noconfig了解更多信息。 我search了一下,找不到一个简单的例子来设置这个。 有任何想法吗?

如何自定义由Spring Boot隐式使用的Jackson JSON映射器?

我正在使用Spring Boot(1.2.1),与构buildREST风格的Web服务教程的方式类似: @RestController public class EventController { @RequestMapping("/events/all") EventList events() { return proxyService.getAllEvents(); } } 所以上面,Spring MVC隐式地使用Jackson将我的EventList对象序列化为JSON。 但是我想对JSON格式做一些简单的定制,比如: setSerializationInclusion(JsonInclude.Include.NON_NULL) 问题是, 定制隐式JSON映射器的最简单方法是什么? 我在这篇博文中尝试了这个方法,创build了一个CustomObjectMapper等等,但是第3步“在Spring上下文中注册类”却失败了: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jacksonFix': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.acme.project.JacksonFix.setAnnotationMethodHandlerAdapter(org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter] […]

使用Thymeleaf从Spring模型设置一个JavaScriptvariables

我如何从Spring模型读取一个模型variables并将其设置为一个Javascript? 请注意,我正在使用Thymeleaf作为模板引擎。 春方: @RequestMapping(value = "message", method = RequestMethod.GET) public String messages(Model model) { model.addAttribute("message", "hello"); return "index"; } 客户端: <script> …. var m = ${message}; // not working alert(m); … </script>

Spring 3.x JSON status 406“特征根据请求不可接受”accept“headers()”

在使用Spring 3.x试图获得JSON响应的时候,我得到了406 error “这个请求所标识的资源只能根据request生成不可接受的响应,接受”headers()“。 这是我的环境 * Spring 3.2.0.RELEASE * included jackson-mapper-asl-1.7.9.jar, jackson-core-asl-1.7.9.jar * Tomcat 6.x * mvc:annotation-driven in Spring configuration XML file 我的控制器: @RequestMapping("/contest") public class ContestController { @RequestMapping(value="{name}", headers="Accept=*/*", method = RequestMethod.GET) public @ResponseBody Contest getContestInJSON(@PathVariable String name) { Contest contest = new Contest(); contest.setName(name); contest.setStaffName(new String("contestitem1")); return contest; } } 我的Springconfiguration文件 <beans xmlns="http://www.springframework.org/schema/beans" […]

从sql插入通过jdbctemplate的身份

是否有可能从SQL插入的Spring jdbc模板调用中获取@@ identity? 如果是这样,怎么样?