这是如何从属性文件中在控制器中注入属性的最简单方法吗?然后需要在每个需要一些属性的控制器上导入属性文件.在像我这样的项目中有大约30个控制器,其中10个需要这个国家属性,我觉得它看起来很混乱.我是否理解@Value正确的使用方法?
@Controller
@RequestMapping(value = "/simple")
@ImportResource("classpath:/META-INF/properties-config.xml")
public class SimpleController {
private @Value("#{exampleProperties['simple.country']}") String country;
}
Run Code Online (Sandbox Code Playgroud)
properties-config.xml (跳过xml和schema的东西)
<beans>
<util:properties id="exampleProperties" location="classpath:/simple.properties" />
</beans>
Run Code Online (Sandbox Code Playgroud)
此外,当尝试在多个控制器中导入properties-config.xml资源时,我会收到此类消息.它似乎不是正确的方法,但我无法找到一个更好的..
01 Apr 2011 04:52:29,859 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory []: Overriding bean definition for bean 'exampleProperties': replacing [Generic bean: class [org.springframework.beans.factory.config.PropertiesFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.springframework.beans.factory.config.PropertiesFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习Spring MVC基础知识,并解决URL被解析为控制器的问题.我正在使用本教程作为跳板.我可以在tomcat下运行它并且url /context/welcome.htm解析为注释为的控制器:
@Controller
@RequestMapping("/welcome")
public class HelloWorldController
Run Code Online (Sandbox Code Playgroud)
使用配置web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
我希望能够键入/ context/welcome并解析HelloWorldController,但这不起作用.我已经尝试设置*但这会导致servlet无法加载.如果有人可以解释为什么这个url模式无效,以及如何配置此控制器以处理所有请求,例如/ welcome和/ welcome,而不仅仅是那些以*.htm结尾的请求,我们将不胜感激.在ASP.NET MVC中,提供了一个路由页面来调试此类控制器映射问题.使用Spring的最佳方法是什么?
我正在使用 @Pattern 来验证字段:
@Pattern(regexp = Patterns.ZIP_CODE, message="validation.ZIP_CODE")
private String zip;
Run Code Online (Sandbox Code Playgroud)
在我的 messages.properties 文件中,我有以下内容:
validation.ZIP_CODE=Must match NNNNN or NNNNN-NNNN
Run Code Online (Sandbox Code Playgroud)
javadocs似乎暗示它应该查找validation.ZIP_CODE消息:
Returns:
The error message template.
Default:
"{javax.validation.constraints.Pattern.message}"
Run Code Online (Sandbox Code Playgroud)
但我得到的却是这样的文字:
validation.ZIP_CODE
Run Code Online (Sandbox Code Playgroud)
我是否误解了javadoc,或者我是否错误地实现了它?
我在Spring应用程序中遇到了奇怪的行为.
我正在尝试创建一个这样的JUnit测试类:
public class AppTest {
@Before
public void setUp() {
// Create the application from the configuration
ApplicationContext context = new AnnotationConfigApplicationContext( ApplicationConfig.class )
// Look up the application service interface
service = (TransferService) context.getBean(TransferService.class);
}
}
Run Code Online (Sandbox Code Playgroud)
主要问题是我在setUp()方法的@Before注释上获得了一个错误.它对我说:以前无法解决一个类型
似乎无法找到@Before注释的依赖项.
这是我在pom.xml文件中的依赖项:
<properties>
<org.springframework.version>3.2.16.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define
this if you use Spring Bean APIs (org.springframework.beans.*) --> …Run Code Online (Sandbox Code Playgroud) 我在使用JUnit的Spring Boot 2.2.5.RELEASE项目上遇到了一个奇怪的问题。
我尝试详细解释我的问题:
1)我定义了一个服务。首先,我定义了一个名为OrderService的接口,如下所示:
package com.dgs.soc.service;
import java.util.List;
import com.dgs.soc.excelapi.dto.Order;
public interface OrderService {
public List<Order> getOrdersList();
}
Run Code Online (Sandbox Code Playgroud)
然后我定义了它的实现,目前名为OrderServiceImpl 的东西非常简单:
package com.dgs.soc.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.dgs.soc.excelapi.dto.Order;
import com.dgs.soc.repository.OrderRepository;
@Service
public class OrderServiceImpl {
public List<Order> getOrdersList() {
List<Order> result = new ArrayList<Order>();
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,此类由@Service注释进行注释。
问题是使用JUnit,我有这个测试类:
package com.dgs.soc.excelapi.integration;
// IMPORTS LIST
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Application.class })
@WebAppConfiguration
@ActiveProfiles(profiles = { …Run Code Online (Sandbox Code Playgroud) 我是Java/Spring的新手.我需要从配置中读取一些值,但如果它不存在则应该失败.现在我有以下代码:
public class SomeClass {
@Value("${some.property:#{null}}")
private String someProperty;
public void someMethod() throws Exception {
if (someProperty == null) throw new AopConfigException("The property must be set");
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我需要添加额外的if块.我可以写那样的东西吗?
@Value("${some.property:#{throw new AopConfigException(\"The property must be set\")}}")
private String someProperty;
Run Code Online (Sandbox Code Playgroud)
要么
@Value("${some.property:#{throwException()}}")
private String someProperty;
private static void throwException() {
throw new AopConfigException("The property must be set");
}
Run Code Online (Sandbox Code Playgroud)
立即失败
更新:
如果我没有使用下面建议的某个默认值,那么它对我来说仍然不会失败.我没有java.lang.IllegalArgumentException:
java ×5
spring ×3
java-ee ×1
jsp ×1
regex ×1
spring-3 ×1
spring-aop ×1
spring-boot ×1
spring-junit ×1
spring-mvc ×1