Spring Getting Started 05 - Constructor Injection

xiaoxiao2021-03-06  41

Getting started 05 - Constructor injection

Spring encourages SETTER INJECTION, but also allows you to use Constructor Injection, use setter or constructor to inject dependencies depending on your needs, let's take a look at how to use construtor injection, first look at Hellobean:

Hellobean.java

Package online.

Public class hellobean {

Private string helloword = "hello";

Private string user = "nobody";

Public Hellobean (String HelloWord, String User) {

THIS.HELLOWORD = HelloWord;

THIS.USER = User;

}

Public String Sayhellotouser () {

Return HelloWord "!" User "!";

}

}

In order to highlight the construction of the form, our Hellobean design is simple, only provides the construction of a letter and the necessary Sayhellotouser (), let's take a look at the definition of Bean:

Bean.xml

Greeting

Justin

In the defined file of Bean, we use to indicate that we will use the constructor inJection, because it is not as easy as setXXXX () when using the constructor inJection, we must specify the location index of the parameter. The index property is to specify which parameters that our object will inject into the constructive function, the index value starts from 0, conforms to the practice of Java indexes. Take a look at the test program:

Test.java

Package online.

Import java.io. *;

Import org.springframework.beans.Factory.BeanFactory;

Import org.springframework.beans.Factory.xml.xmlbeanfactory;

Public class test {

Public static void main (string [] args) throws ioException {inputStream is = new fileinputstream ("bean.xml");

Beanfactory factory = new xmlbeanfactory (IS);

Hellobean Hello = (Hellobean) Factory.getBean ("Hellobean");

System.out.println (Hello.SAYHELLOTOUSER ());

}

}

The actual execution is as follows:

2004/10/21 03:53:58 Org.springframework.beans.Factory.xml.xmlbeandefinitionReader loadingBeandefinitions

Information: Loading XML Bean Definitions from Resource for InputStream

2004/10/21 03:54:02 Org.springframework.beans.Factory.support.AbstractBeanFactory GetBean

Information: Creating Shared Instance of Singleton Bean 'Hellobean'

2004/10/21 PM 03:54:03 Org.springframework.beans.Factory.Support.AbstractautowirecapableBeanFactory Autowireconstructor

Information: Bean 'HelloBean' Instantiaated Via Constructor [Public Onlyfun.caterpillar.Hellobean (Java.lang.String, Java.lang.String)]

Greeting! Justin!

To discuss using constructor or setter to complete dependency injection, it is equivalent to discussing an ancient issue, ready for all resources when the object is established, or after the object is established, use setter to make settings. One of the benefits of using the Constructor is that you can build a dependency on the construction of the object, if you want to build a lot of objects, use constructorinjection to leave a long string of parameters on the construction of the constructive, use Setter will be a good choice. On the other hand, Setter can have a clear name to understand what the injected object is, like setXXX () This name is better than a certain parameter location on the memory constructor. However, using Setter Since the setxxx () method is provided, the relevant field member or resource cannot be guaranteed is not changed during the execution period, so if you want some Field member or resource to be read-only or private, use Constructor Injection will be a simple choice.

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

New Post(0)