Java 被拒绝的 bean 名称“propertyConfigurer”:未识别 URL 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21500286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:01:59  来源:igfitidea点击:

Rejected bean name 'propertyConfigurer': no URL paths identified

javaspringhibernatespring-mvcservlets

提问by user3260768

I am playing with spring (btw I used to know spring when it uses configuration instead of annotation).

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

pom.xml

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

网页.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-dispatcher-servlet.xml

mvc-dispatcher-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.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

休眠文件.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

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

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

个人控制器.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

人.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

人员.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>

Somehow the Autowired doesn't work. Logging at tomcat log, there is no error. The only thing looks suspicious is the "Reject" lines:

不知何故自动连线不起作用。在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

But when I access http://tomcat2-gyw97.rhcloud.com/myprj/list, the log spits out NPE:

但是当我访问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)
...

Any help would be greatly appreciated. Thanks

任何帮助将不胜感激。谢谢

采纳答案by Sotirios Delimanolis

First problem

第一个问题

private PersonService personService = new PersonServiceImpl();

You are creating the object yourself instead of having Spring inject it for you. Spring can't autowire beans into objects it doesn't manage.

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

@Autowired
private PersonService personService;

Second problem, add this

第二个问题,添加这个

<mvc:annotation-driven />

to your mvc-dispatcher-servlet.xml, otherwise Spring uses the default DispatcherServletconfiguration which might not be what you want.

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

Also, this

还有,这

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

serves no purpose at the moment since you don't have a ContextLoaderListener.

目前没有任何用途,因为您没有ContextLoaderListener.



For the reject lines, ignore them. The AbstractDetectingUrlHandlerMappinggoes through all your beans to see if they could be used as request handlers.

对于拒绝行,请忽略它们。将AbstractDetectingUrlHandlerMapping通过所有的豆子去看看他们是否可以作为请求处理。

回答by Lucky

In my case the other solution dint work since I dint autowire a service method and had the mvc annotation config in my servlet config. It is displayed you have set the logging level to "debug" and its for debugging purpose. To hide these from the logs just disable the DEBUG log for the org.springframeworkclasses.

在我的情况下,另一个解决方案可以工作,因为我不需要自动装配服务方法并且在我的 servlet 配置中有 mvc 注释配置。显示您已将日志记录级别设置为“调试”及其用于调试目的。要将这些从日志中隐藏,只需禁用类的调试日志org.springframework

回答by Tony Murphy

My Spring Boot application wasn't working, and I noticed the debug message Rejected bean name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': no URL paths identified. Turns out that this wasn't a problem at all.. Spring is looking in registered beans for URL mappings - and it's no surprise it doesn't find mappings in many of beans.

我的 Spring Boot 应用程序不工作,我注意到调试消息Rejected bean name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': no URL paths identified。事实证明,这根本不是问题.. Spring 正在注册的 bean 中查找 URL 映射 - 并且它在许多 bean 中找不到映射也就不足为奇了。

So if your Spring web application isn't displaying a webpage, check your controller is annotated, a method has appropriate RequestMapping and class has been component scanned ok.

因此,如果您的 Spring Web 应用程序没有显示网页,请检查您的控制器是否已注释,方法是否具有适当的 RequestMapping 并且类是否已被组件扫描。