Getting Started 09 - Property Reference and Automatic Binding

xiaoxiao2021-03-06  48

Getting Started 09 - Attribute Reference and Automatic Binding

When defining the bean, in addition to direct specifying the value to the attribute value, you can directly refer to other beans in the definition file, such as Hellobean, this is:

Hellobean.java

Package online.

Import java.util.date;

Public class hellobean {

Private string helloword = "Hello! World!";

PRIVATE DATE;

Public void sethelloword (string helloword) {

THIS.HELLOWORD = HelloWord;

}

Public string getHelloword () {

Return HelloWord ":" Date.toString ();

}

Public void setdate (Date Date) {

THIS.DATE = DATE;

}

Public Date getdate () {

Return Date;

}

}

In our bean definition, first define a DateBean, then Hellobean can be referred to DateBean, Spring will help us complete this dependency:

Hello! Justin!

Hello! Justin!

Directly specify the value or use to specify the reference to other beans, this explicit relationship is specified, but Spring also supports implicit automatic binding, you can pass the type (ByType) or name (BYNAME) Binds the bean to the corresponding properties on other beans, the following is an example of a ByType:

Hello! Justin!

On here, we don't specify the Hellobean's Date property, but through the automatic binding, because autowire specifies the ByType, it will determine whether there is similar type object in BeanFactory based on the type of Date property. Bind it to the DATE property. You can also specify BYNAME to bind, and Spring will perform automatic binding according to whether the aliasing of the bean is in line with the property name, and give an example. If it is byname, the Date property is to complete the dependent injection, you must modify the A bean's ID value is Date:

Hello! Justin!

Implicit automatic binding is not available from the definition file, clearly see if each property is completed, we can join the dependence check, join Dependency-Check on , there are four dependence inspections: Simple , Objects, All, None. The first only checks simple properties, like a primitive data type or string object, Objects checks object properties, all checks all properties, NONE is a preset, indicating that it does not check the dependency. Here is an example of setting:

Hello! Justin!

If you find that there is an unfinished dependency, you will run the unsatisfieddependencyexception.

转载请注明原文地址:https://www.9cbs.com/read-60918.html

New Post(0)