春天 3.2。当我这样做时一切正常:
@Controller
public class MyController {
@Inject
Provider<MyBean> provider;
@RequestMapping("/chart")
public void getChart(HttpServletResponse resp) {
provider.get();
}
}
Run Code Online (Sandbox Code Playgroud)
但当我将 MyBean 设置为 getChart 的参数时,它不起作用:
@Controller
public class MyController {
@RequestMapping("/chart")
public void getChart(HttpServletResponse resp, MyBean myBean) {
// No such method MyBean.<init>()
}
}
Run Code Online (Sandbox Code Playgroud)
因此 Spring 尝试创建 myBean 的一个新实例,而不是使用已经绑定的实例。
配置:
@Configuration
public class Config {
@Inject
@Bean @Scope("request")
public MyBean provideMyBean(MyOtherBean myOtherBean) {
return myOtherBean.getMyBean();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将控制器请求限定范围,并将 @Inject/@Autowired 添加到getChart(). 然后它无法找到 HttpServletResponse 实例 (NoSuchBeanDefinitionException),尽管请求范围内肯定存在一个实例。
也许它只是没有在 Spring 中实现?
我有一个现有的控制器方法
@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public ModelAndView getProduct(@PathVariable String productId){
// code in this method does not matter
}
Run Code Online (Sandbox Code Playgroud)
我需要通过分析用户的 cookie 来增强这条路线的现有行为。如果用户有名为xyz的 cookie,我需要制作不同的东西。因此,我通过添加第二个参数@CookieValue("xyz") final String xyz创建了getProduct方法 的重载版本:
@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public ModelAndView getProduct(@PathVariable String productId, @CookieValue("xyz") final String xyz){
// make some different logic
}
Run Code Online (Sandbox Code Playgroud)
当我尝试访问此路由时,spring 总是抛出异常
org.springframework.web.bind.ServletRequestBindingException: Missing cookie named 'xyz' for method parameter type [java.lang.String]
Run Code Online (Sandbox Code Playgroud)
显然,方法 getProduct 的重载版本总是被调用。
我该如何修复这个错误?是否有任何注释表明 cookie 值是可选的?
我有一个非常简单的测试:
@SpringBootTest(properties = {"instance.role=GATEWAY", "instance.name=test-instance", "debug=true"})
@TestPropertySource(locations = {"classpath:application.yaml"})
class GatewayWiringTest {
@Autowired
private MySpringBootApplication application;
@Test
void gatewayConfigLoads() {
assertThat(application).isNotNull();
}
}
Run Code Online (Sandbox Code Playgroud)
由于我的应用程序以不同的 bean 配置启动,根据分配的角色,我使用此类测试来测试每个配置是否可加载。
它确实在 Spring Boot 2.7 上运行没有问题。它不能与 Spring Boot 3.0 一起运行(显然,在将 Java 也更新到版本 17 后,而不仅仅是 spring/spring boot 依赖项)。错误的根本原因是:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。
我已将问题追溯到GatewayAutoConfiguration未评估加载的 Spring Cloud Gateway 服务器依赖项中的类 - 这是应该提供缺少的 bean 的自动配置类。
我还通过运行测试并查看类路径来验证执行测试时该类位于类路径上。mvn -X问题不在于加载类的两个条件之一评估为 false,问题在于这些条件从一开始就没有得到评估 - 使用生成的--debug条件评估报告运行测试,并且该类GatewayAutoConfiguration没有出现在两个列表中的任何一个中 - 匹配和不匹配的条件。
为什么不被处理?与 Spring Boot 2.7 相比,在这方面我需要对 Spring Boot …
我在Spring 3中有一个简单的测试项目,基本上是控制器中的一个方法,它从arraylist中获取数据,并且"应该"将它们传递给视图Thsi是该方法的样子:
@RequestMapping(value="/showUsers")
public String showUsers(Model model){
ArrayList<User> users=this.copy();
model.addAttribute(users);
return "showUsers";
}
Run Code Online (Sandbox Code Playgroud)
这是jsp(showUsers.jsp)
它们都执行没有日志或警告显示视图但没有ArrayList<User>数据:(
<table align="center" border="1">
<tr>
<td>Nr:</td><td>Name:</td><td>Email</td><td>Modify?</td>
</tr>
<c:forEach var="user" items="${users}" varStatus="status">
<tr>
<td><c:out value="${status.count}"/></td><td><c:out value="${user.name}"/></td>
<td><c:out value="${user.email}"/></td><td>Modify</td>
</tr>
</c:forEach>
</table>
Run Code Online (Sandbox Code Playgroud)
有什么建议?谢谢!
我想使用Spring 3进行验证.
在参考文档中,第6.2节讨论了Validator我们可以为要验证的类实现的接口.它解释了如何创建Validator类,而不是如何验证对象.
第6.7节涉及JSR-303API 的使用和附带的注释.它似乎是一种验证对象的不同方式(在这种情况下,使用注释而不创建Validator类).
有两种方法可以使用Spring验证,或者我错过了什么?
一个简单的问题,它还规定JSR-303了类路径上必须存在API 的实现.一个提议的实施是Hibernate-Validator.Spring是否提供了实现?
谢谢
我使用Spring 3.1开发了一个Web应用程序
在其中一个模块中,我需要保存一个具有许多OperationParameter对象的Operation对象.因此,在视图中,我为用户提供了添加按钮,以便为特定操作创建OperationParameters.
两个模型都有hibernate映射,Operation和OperationParameter之间有一对多的关系.在操作模型中,我将具有操作参数列表,当用户动态添加操作参数创建新操作时,将在数据库中插入操作参数列表.
当我不使用验证时,它工作正常.当我为Operation model插入操作时,OperationParameters列表也将插入OperationParameter表中.
我的问题是如何为OperationParameter字段进行服务器端验证?如果验证是错误的,那么我如何显示特定OperationParameter字段的错误?
Operation.java
package com.abcprocure.servicerepo.model;
// Generated Feb 9, 2012 11:30:06 AM by Hibernate Tools 3.2.1.GA
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.list.LazyList;
@Entity
@Table(name="Operations"
,schema="dbo"
)
public class Operations implements java.io.Serializable {
private int operationId;
@Embedded
private Services services;
private String operationName;
private String isHqlsql;
private String isMultipleTables;
private String listOfTablesAffected; …Run Code Online (Sandbox Code Playgroud) Spring 3具有类型转换这样一个很好的功能.它提供了一个转换器SPI(Converter<S, T>),用于实现差分转换逻辑.Converter类型的子类允许定义单向转换(仅从S到T),因此如果我还希望从T到SI执行转换,则需要定义另一个实现的转换器类Converter<T, S>.如果我有许多可以转换的类,我需要定义许多转换器.是否有可能在一个转换器中定义双向转换逻辑(从S到T和从T到S)?以及它将如何使用?
PS.现在我通过ConversionServiceFactoryBean在配置文件中定义/注入它们来使用我的转换器
我想创建一个Spring BeanFactoryPostProcessor,它将bean添加到当前的ApplicationContext中.
我有很多Web服务定义,我spring-ws-config.xml希望尽可能减少.
配置如下:
<bean id="menu"
class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition"
lazy-init="true">
<property name="schemaCollection">
<bean
class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
<property name="inline" value="true" />
<property name="xsds">
<list>
<value>classpath:xsd.xsd</value>
</list>
</property>
</bean>
</property>
<property name="portTypeName" value="portType" />
<property name="serviceName" value="serviceName" />
<property name="locationUri" value="/endpoints" />
</bean>
Run Code Online (Sandbox Code Playgroud)
因此,我使用以下bean定义创建一个@Configuration类:
@Bean
@Lazy
public DefaultWsdl11Definition webService() throws IOException {
logger.info("Creating Web Service");
DefaultWsdl11Definition toRet = new DefaultWsdl11Definition();
toRet.setPortTypeName("portType");
toRet.setServiceName("serviceName");
CommonsXsdSchemaCollection collection = new CommonsXsdSchemaCollection();
collection.setInline(true);
collection.setXsds(new Resource[] { new ClassPathResource("path1") });
collection.afterPropertiesSet();
toRet.setSchemaCollection(collection);
toRet.setLocationUri("/endpoints");
return toRet;
} …Run Code Online (Sandbox Code Playgroud) 我有这门课
@Service
@Profile("async")
public class MyServiceImplAsync implements MyService {
..
@Async
@Override
public void printGreetings(String name) {
//do something and sleep 10 seconds
}
@Async
@Override
public Future<String> doSomething(String text) {
//do something and sleep 25 seconds
return new AsyncResult<String>(text);
}
}
Run Code Online (Sandbox Code Playgroud)
观察,两种方法使用 @Async
现在,我也有以下内容:
@Component
public class MySchedule {
..
@Scheduled(cron="*/5 * * * * ?")
public void schedule(){
logger.info("Schedule working at {}", simpleDateFormat.format( new Date() ));
this.myService.printGreetings("Manolito");
}
@Scheduled(cron="*/30 * * * * ?")
public void scheduleFuture(){
logger.info("Schedule …Run Code Online (Sandbox Code Playgroud) 我是Spring MVC的新手,并试图从互联网教程中学习,比如来自journaldev,springsource和codejava的这些好教程.
所有这些都告诉我使用STS作为IDE并继续使用New Project - Spring Template Project - Spring MVC Project.
虽然在我的情况下,使用STS的版本3.6.1.RELEASE,我找不到任何Spring MVC Project选项.
你使用STS有同样的问题吗?
PS
我在这里找到了以前版本的STS 的解决方案,尽管它不适用于版本3.6.1.
看不到MVC模板快照.

spring spring-mvc spring-3 sts-springsourcetoolsuite spring-tool-suite
spring-3 ×10
spring ×6
spring-mvc ×5
java ×3
autowired ×1
cookies ×1
jsp ×1
mapping ×1
model ×1
validation ×1