In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve automatic assembly in Spring. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Spring automatic assembly, the so-called automatic assembly, from the literal interpretation, I think it should not be difficult to understand it (of course, not to induce the reader to chew words). So how many assembly types are there in spring automatic assembly (I think people should be most concerned about when we can be lazy and when we can't, and I think the developers' goal is also to simplify the work of programmers). Then we will introduce that spring auto-assembly can not be lazy-- autowire= "no". Before we accept it, we will introduce several assembly methods (not in other assembly methods).
Before the introduction, I would like to ask a simple question (how many kinds of automatic assembly are there). The answer (six) may deviate from what you think, but I personally think it is not without reason.
1. Default-autowire= "the five forms": this is defined globally (that is, in the tag). If the local definition of autowire is conceivable, of course, it is automatically assembled according to the local definition, and if there is no definition, it is assembled according to the default definition of the tag, so here it is taken out separately as a
2. No: specify dependencies through the ref element
3. ByName: look in the container for a Bean (or ID) with the same name as the property that needs to be automatically assembled. If no matching Bean is found, the attribute will not be assembled.
4. ByType: look for a Bean; in the container that is the same as the attribute type that needs to be automatically assembled. If no matching Bean is found, the attribute will not be assembled. If more than one matching Bean is found, an exception org.springframework.beans.factory.UnsatisfiedDependencyException will be thrown (it is hereby declared that the test is aimed at spring2.5.6. There seems to be no exception in spring3.0 and there is no dependency to check this property in spring3.0).
5. Constructor: find one or more Bean in the container that is consistent with the constructor parameters of the Bean that needs to be automatically assembled. If the container is in an uncertain Bean or constructor, the container will throw an exception org.springframework.beans.factory.UnsatisfiedDependencyException.
6. Autodetect: first we try to use constructor to automate the assembly, and then use byType. Uncertain lines are handled in the same way as constuctor and byType
Since our title is not to be lazy, of course we have to introduce no
Java code
/ / Longmanfei.xml xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> class= "cn.csdn.service.GreetingServiceImpl" autowire= "no" > / / GreetingDaoImpl public class GreetingDaoImpl implements GreetingDao {private String say Public void say () {System.out.println ("I said this hello" + say);} public void setSay (String say) {this.say = say;}} / / GreetingServiceImpl public class GreetingServiceImpl implements GreetingService {private GreetingDaoImpl greetingDaoImpl; public void say () {greetingDaoImpl.say ();} public void setGreetingDaoImpl (GreetingDaoImpl gdi) {System.out.println ("I called the set method"); this.greetingDaoImpl = gdi } public GreetingServiceImpl () {super (); System.out.println ("I called the empty constructor");} public GreetingServiceImpl (GreetingDaoImpl greetingDaoImpl) {super (); System.out.println ("I called the parameter constructor"); this.greetingDaoImpl = greetingDaoImpl }} / / junit test @ Test public void test1 () {/ * loading the spring container can parse multiple configuration files and pass * / ApplicationContext ac=new ClassPathXmlApplicationContext ("classpath:Longmanfei.xml") as an array; / / convert directly to the interface to facilitate later modification of data / * Ioc control inversion reflects * / GreetingService gs= (GreetingService) ac.getBean ("greetingServiceImpl"); gs.say () } this is the end of the article on "how to realize automatic assembly in Spring". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.