小编blo*_*824的帖子

从Abstract Class获取Concrete Class名称

如果已经提出这个问题,我道歉.我搜索了一些问题但找不到答案.

在Java中,我可以在抽象类中获取扩展它的具体类的实例吗?

如果是这样,我可以看一个代码示例吗?

java abstract-class

60
推荐指数
1
解决办法
3万
查看次数

使用Spring 3注释实现一个简单的工厂模式

我想知道如何用Spring 3注释实现简单的工厂模式.我在文档中看到,您可以创建调用工厂类并运行工厂方法的bean.我想知道这是否可能只使用注释.

我有一个目前正在呼叫的控制器

MyService myService = myServiceFactory.getMyService(test);
result = myService.checkStatus();
Run Code Online (Sandbox Code Playgroud)

MyService是一个名为checkStatus()的方法的接口.

我的工厂类看起来像这样:

@Component
public class MyServiceFactory {

    public static MyService getMyService(String service) {
        MyService myService;

        service = service.toLowerCase();

        if (service.equals("one")) {
            myService = new MyServiceOne();
        } else if (service.equals("two")) {
            myService = new MyServiceTwo();
        } else if (service.equals("three")) {
            myService = new MyServiceThree();
        } else {
            myService = new MyServiceDefault();
        }

        return myService;
    }
}
Run Code Online (Sandbox Code Playgroud)

MyServiceOne类如下所示:

@Autowired
private LocationService locationService;

public boolean checkStatus() {
      //do stuff
}
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,locationService变量为alwasy null.我相信这是因为我在工厂里自己创造了这些物品并且没有发生自动装配.有没有办法添加注释才能使其正常工作?

谢谢

java spring annotations factory-pattern

43
推荐指数
7
解决办法
9万
查看次数

格式化Spring:消息参数

我如何格式化我的参数<spring:message>

我有这样的消息:

 message.myMessage=this is {0} my message {1} with {2} multiple arguments
Run Code Online (Sandbox Code Playgroud)

我的jsp有以下几点:

<spring:message code="message.myMessage" 
                arguments="<fmt:formatNumber value='${value1}' currencySymbol='$' type='currency'/>,${value2},${value3}" 
                htmlEscape="false"/>
Run Code Online (Sandbox Code Playgroud)

不显示value1,这是我想格式化的数字.

我不确定我是否可以在参数列表中添加fmt标记.

jsp jstl spring-mvc

15
推荐指数
1
解决办法
3万
查看次数

用于localhost,开发和生产的Spring数据源配置

我试图弄清楚我的Spring应用程序如何确定它的部署位置并加载适当的数据源.我们有3个环境......我的本地,开发服务器和生产服务器.到目前为止,我有3个属性文件调用

localhost.datasource.properties
development.datasource.properties
production.datasource.properties
Run Code Online (Sandbox Code Playgroud)

我让他们像这样:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/resources/properties/production.datasource.properties</value>
                <value>classpath:/resources/properties/development.datasource.properties</value>
                <value>classpath:/resources/properties/localhost.datasource.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSourceMySQL" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${mysql.jdbc.driver.class.name}"
        p:url="${mysql.jdbc.url}"
        p:username="${mysql.jdbc.username}"
        p:password="${mysql.jdbc.password}" />

</beans>
Run Code Online (Sandbox Code Playgroud)

当我在我的localhost机器上时,这很好用.如果我将war文件部署到开发中,它仍然在读取localhost属性,因为它是列表中的最后一个,我收到错误.实现这个的最佳方法是什么?

谢谢

java configuration spring datasource

12
推荐指数
1
解决办法
2万
查看次数

关于线程安全的困惑 - SimpleDateFormat示例

我有一个关于线程安全的问题.据我所知,SimpleDateFormat不是线程安全的.我想知道如果我在弹簧控制器中使用以下方式会有什么影响:

private final static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd yyyy", Locale.US);
Run Code Online (Sandbox Code Playgroud)

稍后在我的控制器函数中,我使用它如下:

  try {
        changedate = changedate.substring(0, 15);                                                
        calcDate = dateFormat.parse(changedate);
    } catch (ParseException e2) {
        logger.error("Date Parsing Problem", e2); 
    }
Run Code Online (Sandbox Code Playgroud)

然后将calcDate添加到我的模型对象中,并返回ModelAndView.

那么我会用这种方式看到什么样的问题呢?简单地删除static关键字会修复任何问题,因为每个线程都会使用自己的dateFormat实例吗?关于线程安全性的任何关于线程安全性的清晰度都将非常受欢迎.

谢谢

java spring thread-safety

11
推荐指数
2
解决办法
7267
查看次数

Spring属性文件设置默认值

我的war文件之外有一个属性文件,系统管理员使用该文件来关闭某些系统功能.它已经在我的本地机器上工作得很好但是当我们部署到开发环境时,没有上传属性文件并且应用程序无法启动.我想知道是否有办法在applicationContext中为通常来自属性文件的值声明默认值.

我目前有这个阅读属性文件:

<util:properties id="myProperties" location="file:${catalina.home}/webapps/myProperties.properties"/>
Run Code Online (Sandbox Code Playgroud)

只要我们记得将属性文件放在正确的位置,这样就可以正常工作.有没有办法声明默认值,或者如果找不到这个文件,可能会从另一个文件读取?

谢谢

java configuration spring properties

8
推荐指数
1
解决办法
7738
查看次数

Spring绑定列表<Object>到Form:复选框

我似乎无法将我的表单绑定到复选框控件.我在这里阅读了很多帖子并尝试了一些技巧但没有运气.也许一双新鲜的眼睛会有所帮助.

我的控制器:

public String editAccount(@RequestParam("id") String id, Model model) {
    model.addAttribute("account", accountService.getAccount(id));
    model.addAttribute("allRoles", roleService.getRoles());
    return EDIT_ACCOUNT;
}
Run Code Online (Sandbox Code Playgroud)

我的jsp:

<form:form action="" modelAttribute="account">
<form:checkboxes items="${allRoles}" path="roles" itemLabel="name" itemValue="id" delimiter="<br/>"/>
</form>
Run Code Online (Sandbox Code Playgroud)

生成的html:

<span><input id="roles1" name="roles" type="checkbox" value="1"/><label for="roles1">User</label></span><span><br/><input id="roles2" name="roles" type="checkbox" value="2"/><label for="roles2">Admin</label></span><span><br/><input id="roles3" name="roles" type="checkbox" value="3"/><label for="roles3">SuperAdmin</label></span<input type="hidden" name="_roles" value="on"/>
Run Code Online (Sandbox Code Playgroud)

我为每个循环使用了一秒(未显示)以确保模型对象包含角色.确实如此,但是没有选中复选框,当我提交角色对象时总是为空.有人可以告诉我我错过了什么吗?

谢谢

编辑

抱歉,我们意识到查看帐户和角色对象可能会有所帮助:

public class Account {

    private String username, firstName, lastName, email;
    private List<Role> roles;

    @NotNull
    @Size(min = 1, max = 50)
    public String getUsername() {
        return username;
    }

    public …
Run Code Online (Sandbox Code Playgroud)

java checkbox binding spring

8
推荐指数
1
解决办法
3万
查看次数

java.lang.AssertionError:未设置内容类型 - Spring Controller Junit Tests

我正在尝试在我的控制器上进行一些单元测试.无论我做什么,所有控制器测试都会返回

java.lang.AssertionError: Content type not set
Run Code Online (Sandbox Code Playgroud)

我正在测试这些方法返回json和xml数据.

以下是控制器的示例:

@Controller
@RequestMapping("/mypath")

public class MyController {

   @Autowired
   MyService myService;

   @RequestMapping(value="/schema", method = RequestMethod.GET)
   public ResponseEntity<MyObject> getSchema(HttpServletRequest request) {

       return new ResponseEntity<MyObject>(new MyObject(), HttpStatus.OK);

   }

}
Run Code Online (Sandbox Code Playgroud)

单元测试设置如下:

public class ControllerTest() { 

private static final String path = "/mypath/schema";
private static final String jsonPath = "$.myObject.val";
private static final String defaultVal = "HELLO";

MockMvc mockMvc;

@InjectMocks
MyController controller;

@Mock
MyService myService;

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);

    mockMvc = standaloneSetup(controller)
                .setMessageConverters(new MappingJackson2HttpMessageConverter(),
                        new Jaxb2RootElementHttpMessageConverter()).build(); …
Run Code Online (Sandbox Code Playgroud)

java junit spring spring-mvc mockmvc

8
推荐指数
1
解决办法
2万
查看次数

覆盖Spring:带有数据库值的消息标记

我使用Spring来显示属性文件中的消息.我希望能够覆盖<spring:message>标记以使用基于登录用户的数据库中的值.如果此值不存在,我希望它默认为属性文件中的值,就像现在一样.

有人可以帮我这个代码吗?我已经阅读了关于AbstractMessageSource但我不清楚如何实现它.

谢谢

java spring properties messages

7
推荐指数
1
解决办法
8449
查看次数

在Spring中使用Mockito模拟属性文件

我正在尝试在我的控制器中为以下方法编写单元测试.

@Autowired
    private ApplicationContext context;

    private String getProperty() {
        try {
            Properties props = context.getBean("myProperties", Properties.class);
            String val = props.getProperty("myProperty");
......
Run Code Online (Sandbox Code Playgroud)

Bean在我的applicationContext中声明如下:

<util:properties id="myProperties" scope="prototype" location="file:${catalina.base}/webapps/myProperties.properties"/>
Run Code Online (Sandbox Code Playgroud)

我如何模拟这个以便我可以测试val变量的不同值?

我想过创建一个测试属性文件并像这样嘲笑它:

context = Mockito.mock(ApplicationContext.class);
Mocikto.when(context.getBean("myProperties", Properties.class)).thenReturn(some test file)
Run Code Online (Sandbox Code Playgroud)

但是我必须将测试文件声明为某个bean.

我想知道是否有更简单的方法来做到这一点?

谢谢

java spring properties mockito

6
推荐指数
1
解决办法
2万
查看次数