拒绝的bean名称'propertyConfigurer':未标识任何URL路径

时间:2014-02-01 16:02:01

标签: java spring hibernate spring-mvc servlets

我正在玩spring(顺便说一句,我曾经在使用配置而不是注释时知道spring)。

的pom.xml

<properties>
    <spring.version>3.0.5.RELEASE</spring.version>
    <hibernate.version>3.6.10.Final</hibernate.version>
    <junit.version>4.11</junit.version>
    <jdk.version>1.6</jdk.version>
</properties>

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      version="2.5">    

<display-name>Demo Web Application</display-name>

<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>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

MVC-调度-servlet.xml中

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

<context:component-scan base-package="com.company.controller" />
<context:component-scan base-package="com.company.service" />
<context:component-scan base-package="com.company.dao" />
<context:component-scan base-package="com.company.hibernate" />

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

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

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>/WEB-INF/hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

</beans>

jdbc.properties

jdbc.driverClassName= com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://127.7.195.2:3306/tomcat2
jdbc.username=xxxx
jdbc.password=yyyy

hibernate.cfg.xml中

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <mapping class="com.company.hibernate.Person" />
</session-factory>

</hibernate-configuration>

PersonServiceImpl.java

@Service
public class PersonServiceImpl implements PersonService {

@Autowired
private PersonDAO personDAO;

public void addPerson(Person person) {
    personDAO.addPerson(person);
}

public List<Person> listPerson() {
    return personDAO.listPerson();
}

public void removePerson(Integer id) {
    personDAO.removePerson(id);
}
}

PersonDAOImpl.java

@Repository
public class PersonDAOImpl implements PersonDAO {
@Autowired
private SessionFactory sessionFactory;

    @Transactional
public void addPerson(Person person) {
    sessionFactory.getCurrentSession().save(person);
}
    @Transactional
public List<Person> listPerson() {
    return sessionFactory.getCurrentSession().createQuery("from Person").list();
}
    @Transactional
public void removePerson(Integer id) {
    Person person = (Person) sessionFactory.getCurrentSession().load(Person.class, id);
    if (null != person) {
        sessionFactory.getCurrentSession().delete(person);
    }
}
}

PersonController.java

@Controller
public class PersonController {

private PersonService personService = new PersonServiceImpl();

@RequestMapping("/list")
public String listContacts(Map<String, Object> map) {

    map.put("person", new Person());
    map.put("personList", personService.listPerson());

    return "person";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addPerson(@ModelAttribute("person")
    Person person, BindingResult result) {

    personService.addPerson(person);

    return "redirect:/list";
}

@RequestMapping("/delete/{contactId}")
public String deleteContact(@PathVariable("personId")
Integer personId) {

    personService.removePerson(personId);

    return "redirect:/list";
}
}

Person.java

@Entity
@Table(name = "PERSON")
public class Person {

@Id
@Column(name="ID")
private int id;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

public Person() {
}

public Person(String fname, String lname) {
    this.firstName = fname;
    this.lastName = lname;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String first_name) {
    this.firstName = first_name;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String last_name) {
    this.lastName = last_name;
}
}

Person.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
 <class name="Person" table="PERSON">
  <meta attribute="class-description">
     This class contains the person detail. 
  </meta>
  <id name="id" type="int" column="id">
     <generator class="native"/>
  </id>
  <property name="firstName" column="first_name" type="string"/>
  <property name="lastName" column="last_name" type="string"/>
 </class>
</hibernate-mapping>

不知何故,Autowired不起作用。记录tomcat日志,没有错误。唯一可疑的是“拒绝”行:

Jan 31, 2014 5:52:26 PM org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping detectHandlers
FINE: Rejected bean name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': no URL paths identified
Jan 31, 2014 5:52:26 PM org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping detectHandlers
FINE: Rejected bean name 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor': no URL paths identified
...
Jan 31, 2014 5:52:27 PM org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping detectHandlers
FINE: Rejected bean name 'personServiceImpl': no URL paths identified
Jan 31, 2014 5:52:27 PM org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping detectHandlers
FINE: Rejected bean name 'personDAOImpl': no URL paths identified
Jan 31, 2014 5:52:27 PM org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping detectHandlers
FINE: Rejected bean name 'propertyConfigurer': no URL paths identified
Jan 31, 2014 5:52:27 PM org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping detectHandlers
FINE: Rejected bean name 'dataSource': no URL paths identified
Jan 31, 2014 5:52:27 PM org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping detectHandlers
FINE: Rejected bean name 'sessionFactory': no URL paths identified
Jan 31, 2014 5:52:27 PM org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping detectHandlers
FINE: Rejected bean name 'transactionManager': no URL paths identified

但是当我访问http://tomcat2-gyw97.rhcloud.com/myprj/list时,日志会吐出NPE:

FINE: Resolving exception from handler [com.company.controller.PersonController@1cfa07a]: java.lang.NullPointerException
Feb 01, 2014 10:57:09 AM org.springframework.web.servlet.FrameworkServlet processRequest
FINE: Could not complete request
java.lang.NullPointerException
        at com.company.service.PersonServiceImpl.listPerson(PersonServiceImpl.java:35)
        at com.company.controller.PersonController.listContacts(PersonController.java:27)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...

非常感谢任何帮助。感谢

3 个答案:

答案 0 :(得分:3)

第一个问题

private PersonService personService = new PersonServiceImpl();

您正在自己创建对象,而不是让Spring为您注入它。 Spring无法将bean自动装配到它无法管理的对象中。

@Autowired
private PersonService personService;

第二个问题,添加此

<mvc:annotation-driven />

到你的mvc-dispatcher-servlet.xml,否则Spring使用默认的DispatcherServlet配置,这可能不是你想要的。

另外,这个

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
由于您没有ContextLoaderListener

目前没有用处。


对于拒绝行,请忽略它们。 AbstractDetectingUrlHandlerMapping遍历所有bean,看看它们是否可以用作请求处理程序。

答案 1 :(得分:3)

我的Spring Boot应用程序无法运行,我注意到了调试消息Rejected bean name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': no URL paths identified。事实证明这根本不是问题.Spring正在寻找注册bean来寻找URL映射 - 并且毫不奇怪它没有在许多bean中找到映射。

因此,如果您的Spring Web应用程序未显示网页,请检查您的控制器是否已注释,方法是否具有相应的RequestMapping,并且已对组件进行了组件扫描。

答案 2 :(得分:1)

在我的情况下,另一个解决方案dint工作,因为我dint autowire一个服务方法,并在我的servlet配置中有mvc注释配置。显示您已将日志记录级别设置为&#34; debug&#34;它用于调试目的。要从日志中隐藏这些,只需禁用org.springframework类的DEBUG日志。