将Spring定义属性注入控制器

时间:2013-12-20 14:22:05

标签: spring-mvc

我试图通过使用Spring Framework servlet.xml配置文件在我的控制器中注入带有值的属性。我的控制器像这样启动

package lv.lu.meetings.portal.mvc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import lv.lu.meetings.domain.jpa.User;
import lv.lu.meetings.domain.jpa.meeting.Attendance;
import lv.lu.meetings.domain.jpa.meeting.Invite;
import lv.lu.meetings.domain.jpa.meeting.InviteStatus;
import lv.lu.meetings.domain.jpa.meeting.Meeting;
import lv.lu.meetings.domain.jpa.notification.Notification;
import lv.lu.meetings.domain.redis.Friend;
import lv.lu.meetings.interfaces.service.NotificationService;
import lv.lu.meetings.interfaces.service.UserService;
import lv.lu.meetings.portal.mvc.WebConst;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Controller for displaying application home page.
 * 
 * Supports multiple tab views: Main, Friends, Notifications, Meetings. 
 */


@Controller
public class HomePageController {

    // limit meeting count
    // defined in meetings-servlet.xml
    private String limit;
    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

我的meetings-servlet.xml文件就像这样

<?xml version="1.0" encoding="UTF-8"?>

<!-- Spring Web application configuration file -->

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

    <!-- Support for component autowiring -->
    <context:component-scan base-package="lv.lu.meetings"/>

    <!-- URL mapping for annotation-based Spring Web MVC controllers -->
    <bean id="urlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="loginInterceptor"/>
            </list>
        </property>
    </bean>

    <!-- Limit output data -->
    <bean id="HomePageController" class="lv.lu.meetings.portal.mvc.controller.HomePageController">
        <property name="limit" value="10" />
    </bean> 

我收到错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMapping' defined in ServletContext resource [/WEB-INF/meetings-servlet.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'HomePageController' to URL path [/home]: There is already handler of type [class lv.lu.meetings.portal.mvc.controller.HomePageController] mapped.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)

它绝对容易配置,但我是Java和Spring Framework本身的新手,所以非常感谢帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

您目前正在创建2个HomePageController bean。一个来自

<!-- Limit output data -->
<bean id="HomePageController" class="lv.lu.meetings.portal.mvc.controller.HomePageController">
    <property name="limit" value="10" />
</bean> 

和另一个来自

<context:component-scan base-package="lv.lu.meetings"/>

上的@Controller注释
@Controller
public class HomePageController {

假设您在此类中有一个@RequestMapping处理程序方法,Spring将尝试将其注册两次并失败,因为您不能为同一个URL提供两个处理程序。

选择一个或另一个bean。如果你想使用注释方式,你可以直接注入值

@Value("10")
private String limit;

或使用property placeholder

答案 1 :(得分:0)

您可以使用@Value注释从属性文件(或其他)读取值来设置属性:

@Value("${my.limit}")
private String limit;

您还可以从组件扫描中排除HomePageController

<context:component-scan base-package="lv.lu.meetings">
  <context:exclude-filter type="regex" expression="HomePageController$"/>
</context:component-scan>