标签: spring-annotations

在控制器中注入属性值的最佳方法是什么?

这是如何从属性文件中在控制器中注入属性的最简单方法吗?然后需要在每个需要一些属性的控制器上导入属性文件.在像我这样的项目中有大约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)

java spring-annotations spring-3

0
推荐指数
1
解决办法
4198
查看次数

为REST样式URL配置DispatcherServlet

我正在尝试学习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的最佳方法是什么?

jsp spring-mvc spring-annotations

0
推荐指数
1
解决办法
708
查看次数

使用@Pattern时显示属性文件消息?

我正在使用 @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,或者我是否错误地实现了它?

java regex spring-annotations

0
推荐指数
1
解决办法
7191
查看次数

为什么我不能在项目中使用@Before注释?"以前无法解决一个类型"

我在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)

java spring java-ee spring-annotations spring-junit

0
推荐指数
1
解决办法
3868
查看次数

为什么 Spring Boot 没有找到试图执行自动装配的 @Service bean?bean 存在但找不到它

我在使用JUnitSpring 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 spring-annotations spring-boot

0
推荐指数
2
解决办法
1812
查看次数

作为Spring @Value()的默认值的异常

我是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 spring spring-aop spring-annotations

-3
推荐指数
1
解决办法
1569
查看次数