2013-12-20 18 views
0

我想通過使用Spring框架servlet.xml配置文件注入屬性值在我的控制器內。我的控制器開始這樣注入彈簧定義propery到控制器

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; 
    } 

我的會議-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框架本身來說是新的,所以我們對此表示讚賞。謝謝!

回答

0

您目前正在創建2 HomePageController豆。一位來自

<!-- 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

+0

權,我也有\t @RequestMapping(value =「/ home/meetings」,method = RequestMethod.GET) public String displayMeetings(ModelMap model,HttpSession session){mapping我想使用我定義的屬性。我怎樣才能只選擇一個?我無法刪除 Slammer

+0

@Slammer然後擺脫'HomePageController'的''聲明並找到另一種設置限制的方法。一種是我描述的方式。 –

+0

是的,但提供了屬性佔位符示例的語法,它似乎只對jstl有效 – Slammer

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>