我真的不确定使用Spring 3.2 MVC是否可行.
我的控制器有一个声明如下的方法:
@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody List<Foo> getAll(){
return service.getAll();
}
Run Code Online (Sandbox Code Playgroud)
问题:
@ResponseStatus(HttpStatus.OK)?HttpStatus.OK状态代码.我有一个util模块,可以生成一个可以在其他应用程序中使用的jar.我希望这个模块使用缓存,并且更喜欢使用Spring的annotation-driven缓存.
所以Util-Module会有这样的事情:
DataManager.java
...
@Cacheable(cacheName="getDataCache")
public DataObject getData(String key) { ... }
...
Run Code Online (Sandbox Code Playgroud)
数据管理器 - ehcache.xml中
...
<cache name="getDataCache" maxElementsInMemory="100" eternal="true" />
...
Run Code Online (Sandbox Code Playgroud)
数据管理器弹簧-config.xml中
...
<cache:annotation-driven cache-manager="data-manager-cacheManager" />
<!-- ???? --->
<bean id="data-manager-cacheManager"
class="org.springframework.cache.ehcache.EhcacheCacheManager"
p:cache-manager="data-manager-ehcache"/>
<bean id="data-manager-ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="data-manager-ehcache.xml"/>
...
Run Code Online (Sandbox Code Playgroud)
我还希望我的可部署单元通过Spring注释进行缓存,同时将上面的jar包含为依赖项.所以我Deployable-Unit会有这样的事情:
MyApp.java
...
@Cacheable(cacheName="getMyAppObjectCache")
public MyAppObject getMyAppObject(String key) { ... }
...
Run Code Online (Sandbox Code Playgroud)
我-APP-ehcache.xml中
...
<cache name="getMyAppObjectCache" maxElementsInMemory="100" eternal="true" />
...
Run Code Online (Sandbox Code Playgroud)
我的应用程序内弹簧-config.xml中
...
<cache:annotation-driven cache-manager="my-app-cacheManager" />
<!-- ???? --->
<bean id="my-app-cacheManager"
class="org.springframework.cache.ehcache.EhcacheCacheManager"
p:cache-manager="my-app-ehcache"/>
<bean id="my-app-ehcache" …Run Code Online (Sandbox Code Playgroud) 有没有办法使用注释使用查找方法注入?
鉴于以下课程:
@Service
public abstract class A {
protected abstract createB();
}
Run Code Online (Sandbox Code Playgroud)
为了使它工作,我必须在spring applicationContext.xml中声明以下内容:
<bean id="b" class="com.xyz.B">
</bean>
<bean id="a" class="com.xyz.A">
<lookup-method name="createB" bean="b"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
即使我正在使用,<context:component-scan base>我也必须在XML中声明它.我认为这不是一个好方法.
如何用注释做到这一点?
我有一个Spring Boot应用程序,在其中一个类中,我尝试使用application.properties文件引用属性@Value.但是,该财产没有得到解决.我查看了类似的帖子,并尝试按照建议,但这没有帮助.这堂课是:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class PrintProperty {
@Value("${file.directory}")
private String fileDirectory;
public void print() {
System.out.println(fileDirectory);
}
}
Run Code Online (Sandbox Code Playgroud)
我有属性file.directory application.properties.我也有其他领域.
在Spring Framework中,AbstractWizardFormController似乎已被弃用.如何在Spring MVC Framework中实现多个页面表单.(我不使用webflow)
任何示例或指针都有助于考虑我在Spring中的有限知识.
我的maven spring项目目录结构如下所示.我正在使用基于Spring-4注释的配置.我配置如下的资源.我尝试了许多Stackoverflow问题和其他网站中提出的方法
http://imwill.com/spring-mvc-4-add-static-resources-by-annotation/#.U5GZlXKs9i4
但是jsp文件无法加载资源,所有静态内容请求都返回404错误.我在jsp中试过这些东西,
<link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">
<link href="/resources/css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
Run Code Online (Sandbox Code Playgroud)
编辑:我正在使用servlet 2.5,因为到目前为止我无法将我的项目从JBoss 5升级到更高版本.JBoss5不支持servlet 3,那重要吗?
@Configuration
@ComponentScan("com.mgage.mvoice")
public class MyAppWebConfig extends WebMvcConfigurerAdapter {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// I tried these many combinations separately.
ResourceHandlerRegistration resourceRegistration = registry
.addResourceHandler("resources/**");
resourceRegistration.addResourceLocations("/resources/**");
registry.addResourceHandler("/css/**").addResourceLocations("/css/**");
registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
registry.addResourceHandler("/resources/**")
.addResourceLocations("classpath:/resources/");
// do the classpath works with the directory under webapp?
}
}
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何在使用注释配置bean时将bean指定为非延迟?
@PathVariable如果路径变量不在url中,是否可以使返回null?否则我需要做两个处理程序.一个用于/simple另一个/simple/{game},但两者都做同样的事情,如果没有定义游戏我从列表中选择第一个然而如果有游戏参数定义然后我使用它.
@RequestMapping(value = {"/simple", "/simple/{game}"}, method = RequestMethod.GET)
public ModelAndView gameHandler(@PathVariable("example") String example,
HttpServletRequest request) {
Run Code Online (Sandbox Code Playgroud)
这是我在尝试打开页面时得到的/simple:
引起:java.lang.IllegalStateException:在@RequestMapping中找不到@PathVariable [example]
我正在使用@Configuration注释来配置spring而不是xml文件.我正在配置具有不同会话工厂和不同事务管理器的2个数据源.我在这里遇到了一个@EnableTransactionManagement注释问题.我在其文档中读到,
@EnableTransactionManagement更灵活; 它将回退到PlatformTransactionManager容器中任何bean 的类型查找.因此,名称可以是"txManager","transactionManager"或"tm":它无关紧要.
这意味着我给方法的任何名称,它总是会搜索返回PlatformTransactionManager对象的方法,而我有2个transactionmanagers.现在问题是,当我测试这个类时,它给了我错误:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有org.springframework.transaction.PlatformTransactionManager定义类型为[ ]的唯一bean :期望的单个bean但找到2
我甚至试图拥有2个不同的配置类但是徒劳无功.在xml配置中,情况并非如此.我用两个<tx:annotation-driven transaction-manager="" />标签注册了我的两个交易管理器,它运行正常.但是这里不能用注释做同样的事情.
如果我想在Spring注释配置类中配置2个具有2个不同事务管理器的数据源,我该怎么办?
我有一个返回JSON的控制器.它采用一种形式,通过弹簧注释验证自己.我可以从BindingResult获取FieldError列表,但它们不包含JSP将在标记中显示的文本.如何将错误文本发送回JSON?
@RequestMapping(method = RequestMethod.POST)
public
@ResponseBody
JSONResponse submit(@Valid AnswerForm answerForm, BindingResult result, Model model, HttpServletRequest request, HttpServletResponse response) {
if (result.hasErrors()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
JSONResponse r = new JSONResponse();
r.setStatus(JSONResponseStatus.ERROR);
//HOW DO I GET ERROR MESSAGES OUT OF BindingResult???
} else {
JSONResponse r = new JSONResponse();
r.setStatus(JSONResponseStatus.OK);
return r;
}
}
Run Code Online (Sandbox Code Playgroud)
JSONREsponse类只是一个POJO
public class JSONResponse implements Serializable {
private JSONResponseStatus status;
private String error;
private Map<String,String> errors;
private Map<String,Object> data;
...getters and setters...
}
Run Code Online (Sandbox Code Playgroud)
调用BindingResult.getAllErrors()返回FieldError对象的数组,但它没有实际的错误.
spring ×8
java ×5
spring-mvc ×4
annotations ×2
spring-3 ×2
caching ×1
ehcache ×1
json ×1
spring-4 ×1
spring-boot ×1