Identify id and name in bean s in Spring MVC

Spring MVC is a follow-up product of Spring Framework Work and has been integrated into the Spring Web Flow.The Spring framework provides a full-featured MVC module for building Web applications.Using Spring pluggable MVC architecture, you can choose to use Spring's Spring MVC framework or integrate other MVC development frameworks such as Struts1, Struts2, etc. when developing WEB with Spring.As far as functionality is concerned, SpringMvc Similar to what Struts2 does, it takes access processing and returns a response.

When I first got to know Spring MVC, I wrote a little thing.The function is that hello.do, the controller that accesses SpringMvc, returns to jump to a page.(Simple validation of SpringMvc functionality, so only beans and web.servlet are imported packages)

Top Code:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringMVCDemo</display-name>
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- Load Profile -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc-servlet.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
</web-app>

Controller: It is simple to implement the Controller interface directly and then implement its handleRequest method.

public class HelloAction implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		System.out.println("==================================");
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("aaa", "firstSpringMvc");
		mav.setViewName("success.jsp");
		return mav;
	}
}

Finally, configure springmvc-servlet.xml

Then there was a problem with the id='XX'used for the identifier of the bean s I had previously configured while learning spring.But when I set the path to access here, I can't use the ID but the name attribute.So here's my understanding: springMvc is functionally similar to struts2, so it uses the name attribute as well, and if you use the ID attribute, you won't be able to access it.

<?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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    <!-- access controller
    	//The path accessed when configuring Springmvc is placed in name
     -->
    <bean name="/hello.do" class="com.nyist.controller.HelloAction"> 
   
    </bean>
</beans>

Finally, let's talk about the difference between the id and name attributes of bean s in Spring

Simply put: the id identifies the bean, which is unique and has only one; the name defines the alias of the bean, which can have multiple and possibly duplicate the names of other beans.

In detail:

The id is the unique identifier for the bean. Special characters cannot be used: *#@, and numbers cannot start.When a bean is referenced, only the id can be used to point to the bean you need.

Name can be a special character, and a bean can have multiple names: name= "bean1,bean2,bean3", separated by commas.

The Bean can be removed by id and name.

BeanFactory factory=new XmlBeanFactory(new ClassPathResource("config.xml"));

Dao dao=(Dao)factory.getBean("thisbean");

Dao dao1=(Dao)factory.getBean("bean1");

Dao dao2=(Dao)factory.getBean("bean2");     .........

//Verify that it points to the same Bean
          System.out.println((dao== dao1)); 
          System.out.println((dao1== dao2)); 
* Alias of //output bean s
          String[] thisbean= factory.getAliases("thisbean"); 
          for (String strbean: thisbean) { 
              System.out.println(strbean); 
          }

Two identical IDs are not allowed in the configuration file, otherwise an error will occur at initialization; however, two identical names are allowed in the configuration file. When get Bean () is used to return an instance, the latter one is returned, which should be overwritten by the latter one.For this reason, to avoid accidental name override, try using the id attribute instead of the name attribute.

If neither id nor name is specified, use the full class name as the name. For example, you can use the
getBean("com.stamen.BeanLifeCycleImpl") returns the instance.



Keywords: Spring xml Attribute JavaEE

Added by EddieFoyJr on Sun, 09 Jun 2019 21:15:44 +0300