AutoWire and AutoWire)
Select an Assembly policy based on autowire Configuration
ByName: select the bean with the same name as the property name for assembly;
ByType is selected based on the type. If the corresponding type matches multiple beans, an error is returned, as shown in the following Configuration:
1
Error:
You can also configure the Assembly policy
attribute, it is too cumbersome to generate a sqlsessionfactory code. It is more convenient to use the method configured in Application.properties.2.mybatis Management @mapper DAO, when using @autowire automatic injection, IDEA has red error "could not Autowire", but the actual operation is normal, how to remove the error?According to my understanding, this error is because the DAO interface only added MyB
General Javapojo in applications are managed by spring, so injecting with autowire annotations does not create problems, but there are two exceptions, one is Filter, the other is a servlet, These two things are managed by the servlet container, so if you want to inject it with autowire like any other bean, you need to do some extra work.
Delegatingfilterproxy is provided for filter,spring, so this article
= ' Com.yiibai.common.Customer ' > class= "Com.yiibai.common.Person"/>2. auto-wiring ' ByName 'Auto assemble by attribute name. In this case, because the name of the "person" Bean is the same as the property ("person") name of the "Customer" bean, spring will automatically assemble it through the setter method – "Setperson" “.class= "Com.yiibai.common.Customer" autowire= "byname"/> class= "Com.yiibai.common.Person "/>3. auto-wiring
The official definition of automatic assembly is as follows:The Spring IoC container can automatically assemble (autowire) the association between beans that collaborate with each other. Therefore, if possible
Let spring specify the bean collaborators (other dependent beans) by checking the content in beanfactory ). Because
Autowire can be set for a single bean, so some beans can use
http://tjc.javaeye.com/blog/341430
Today to tidy up the Spring Automatic assembly Autowire section, here we want to solve the following problems:§1 What is automatic assembly?What is the meaning of §2 automatic assembly?§3 are there several types of automatic assembly?§4 How do I enable automatic assembly?§5 will the problem be caused by automatic assembly?§1 What is automatic assembly?Spring reference wrote the spring container is able to
Typically, when we use spring, the attributes injected are the specified class instances or list or array, and for this type of application we usually use spring to look in the container by default based on the bean's ID or name. But sometimes in special applications where one of the bean's properties is not fixed, another injection of spring can be autowire= "Bytype". The following example:
An interface class and several implementation classes Myidpr
1 Error descriptionOrg.springframework.beans.factory.BeanCreationException:Error creating bean with Name ' Com.sc.oa.test.timer.dao.TimerDaoTest ': Injection of autowired dependencies failed; Nested exception is org.springframework.beans.factory.BeanCreationException:Could not Autowire field:private Com.sc.oa.timer.dao.TimerDao Com.sc.oa.test.timer.dao.TimerDaoTest.timerDao; Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionExc
First, the range of @resource is larger than the @autowire.
First find all the beans that meet the criteria by type
Determine the bean length, if not, to determine if an exception is thrown based on the required property in @autowired (default is True)
If more than one, then try to find the optimal one, if the optimal is not found, then throw an exception
If there is only one, use this bean directly
If @
Default-autowire = "X"X has four options: byname, bytype, constructor, and autodetect.
1. byname:
Service. Java
public class Service{ Source source;public void setSource(Source source) {this.source = source; }}
Applicationcontext. xml
... default-autowire="byName">
CN. HH. Spring. dbcpsource implements the source interface.The source attribute is not configured for bean service in XML, but
Let's sort out the automatic assembly autowire section of spring today. Here we want to solve the following problems:1. What is automatic assembly?2 What is the significance of automatic assembly?§ 3 what types of automatic assembly are available?§ 4 how to enable automatic assembly?§ 5 will Automatic Assembly cause problems?
1. What is automatic assembly?The Spring IoC container can automatically assemble (autowi
Previous we have seen Constructore injection:https://www.cnblogs.com/answer1215/p/9484872.htmlIt would is easier to using autowire to reduce the code, and Autowite have four different types:
Bytype
ByName
Constructor
No
First let's see how to use ' autowire= ' constructor "': name= "CustomerService" class= " Com.pluralsight.service.CustomerServiceImpl "
@Autowire @InjectSame point:The same can be injected into an object, injected in the domain above, injected on a set method or other method that needs to be injected, injected on the constructorDifferent points:@Autowire has a @required tag that allows the object to be empty@Inject no @required tag, mandatory object cannot be empty@Qualifier @NamedSame point:is used as a qualifier, you can use either the ta
Autowire = "byname" automatically assembles beans with the same attributes as bean IDs. For example, there are two beans,
The userservice class has a userdao attribute:
Private iuserdao userdao;
Public iuserdao getuserdao (){Return userdao;}
Public void setuserdao (iuserdao userdao ){This. userdao = userdao;}
In this way, when the bean userservice3 is created, byname will find the bean userdao and setter will be injected into the bean instance userse
Intellij Idea development tool in @autowired or @resource injection Xxxmapper interface times the following error:Could not autowire. No beans of ' tbitemmapper ' type found. Less ... (CTRL+F1) Checks autowiring ProbThe solution is as follows, set in Intellij idea:Settings-editor-inspections-spring-spring core-code-autowiring for Bean class-disable (as if to remove √) and saveCould not autowire. No beans of
In the xml configuration file, there are five types of autowire, which can be specified using the autowire attribute in the
Description
No does not use automatic assembly. You must use the ref element to specify the dependency. The default value is set.
Byname is automatically assembled Based on the attribute name. This option will check the container and find
Automatically assemble the bean with
The first thing to note is that although both @Resource and @autowire are used for dependency injection annotation, there are differences.1 resource does not depend on spring, the latter is the opposite, so as far as possible to reduce, use resource;2 Resource is preferred to match by variable name, and @resource (name= "") can also be used to specify the variable name to inject. Autowire is preferred by ty
classstudenttest {@Test Public voidtest01 () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); /** Get container give us the student object created by the IOC's embodiment * itself created by itself the process of object creation, the right to create objects handed over to the spring container! IOC Control Inversion * * before Student student=new Student (); * Now Context.getbean ("student"); * Context container to create objects **/Stude
In writing unit test class, error, waste a lot of effort just to give debugging good, to share under.The complete error is as follows:caused by:org.springframework.beans.factory.BeanCreationException:Error creating bean with Name ' Buskjjlzjwyhpscontroller ': Injection of autowired dependencies failed; Nested exception is org.springframework.beans.factory.BeanCreationException:Could not Autowire field:private Javax.servlet.http.HttpServletRequest com.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.