SPRING-002-IOC Bean Configuration

Source: Internet
Author: User
Tags getmessage naming convention

First, Bean naming

1. Direct naming

1.1, directly using the spring default naming method, do not specify id;1.2, only the fully qualified class name must be configured by the IOC container for it to generate an identity, 1.3, the client must pass the interface "T Getbean (class<t> requiredtype)" Get Bean
class= "Wang.conge.HelloImpl"/>= Application.getbean (Helloimpl.  Class);

2. ID Name

Each bean can have one or more IDs (or identifiers or names), where we call the first ID "identifier", and the remaining IDs are called "aliases", which must be unique within the IOC container.

class= "Wang.conge.HelloImpl"/>= Application.getbean ("Testbeanid");

3. Name Name

Specify name so that name is "identifier" and must be unique in the IOC container

class= "Wang.conge.HelloImpl"/>= Application.getbean ("Testbeanname");

4, Id&name Mixed

The specified ID and name,id are identifiers, and name is the alias and must be unique in the IOC container.

class= "Wang.conge.HelloImpl"/>= Application.getbean ("Testbeanid");

Summary: Bean naming follows the XML naming convention, but it is best to conform to the Java naming convention, consisting of "letters, numbers, underscores", and should form a good naming habit,

For example, the "hump", that is, the first letter begins, starting with the beginning of the first letter of the second word, which can increase readability. Camel-style recommended

Second , instantiate the bean

1. Constructors

1.1, using the empty constructor 1.2, using a constructor with parameters
class class= "Wang.conge.HelloImpl" >       <constructor-arg index= "0" value= "Hello conge!" />  // If you have more than one parameter, you can pass multiple parameters to construct </bean>= Application.getbean ("TestBeanId1"  = Application.getbean ("TestBeanId2");

2. Static Factory

2.1, in addition to specifying the required factory class property, you also specify the Factory-method property to specify method 2.2 for instantiating the bean, using the static factory method, and allowing the method parameter to be specified, spring The IOC container will call the method specified by this property to get the bean
 Public class helloimplfactory {    publicstatic  helloimpl newinstance (String message) {         returnnew  helloimp (message);}    } class= "Wang.conge.HelloImplFactory" factory-method= "newinstance" >       <constructor-arg index= " 0 "value=" Hello conge! " /></bean>= Application.getbean ("Testbean");

3. Dynamic Factory

3.1. Instantiate the dynamic factory class first, use the Spring IOC container Management 3.2, use the Factory-bean property to specify the configured dynamic factory Bean,factory-method property to specify the method for instantiating the bean, and using instance factory methods allows you to specify method parameters in the same way that you use constructors
 Public class helloimplfactory {    public  helloimpl newinstance (String message) {        return  New  helloimp (message);}    } <bean id= "Beanfactory"  class= "Wang.conge.HelloImplFactory"/><bean id= "Testbean"  Factory-bean= "Beanfactory"  factory-method= "newinstance" >    <constructor-arg index= "0" value= "Hello conge! " /></bean>= Application.getbean ("Testbean");

Three , Dependency Injection bean Spring recommends a combination of classes in which spring is responsible for injecting the bean's dependent resources, which can be beans, external files, constant data, and so on, that are reflected as objects in Java, and that the container is responsible for assembling the dependencies between the beans. The dependencies here refer to the dependencies between the beans, which can be thought of as the "association", "aggregation", "combination" relationship between the traditional classes and the classes. 1. Constructors are implemented by using constructor injection by configuration constructor parameters, which are dependent on the constructor parameters. In addition to the constructor method, there are static factories, instance factory methods can be constructor injection 2, setter injection 2.1, setter injection, through the constructor, static factory or instance factory instance good bean, by invoking the Bean class setter method to inject dependency 2.2, JavaBean: is essentially a Pojo class, but with a limitation:

2.2.1, the class must have a public parameterless constructor

2.2.2, property is private access level, public is not recommended, such as private String message;

2.2.3, attributes are accessed by a set of setter (modifier) and getter (accessor) methods when necessary;

2.2.4, setter method, start with "set", followed by the first capitalization of the property name, such as "Setmesssage", simple properties generally have only one method parameter, the method return value is usually "void";

2.2.5, Getter Methods, the general properties start with "get", for a Boolean type is generally preceded by "is", followed by the first uppercase attribute name, such as "Getmesssage", "isOk";

         2.2.6, there are other special cases, such as the property has two consecutive uppercase letters beginning, such as "URL", then the Setter/getter method is: "SetUrl" and "GetURL", For some other special cases, see the Java Bean naming convention.

 Public class helloimpl{    private  String message;     Private void setmessage (String message) {        this. Message = message;    }       Public String Hello () {        return "Hello:" + message;    }} <bean id= "Testbean"  class= "Wang.conge.HelloImpl" >    <property name= "message" value= " Hello conge! " /> </bean>

3. Reference injection

dependent on other beans, the name of the dependency injection should be the same
 Public classmessageimpl{PrivateString message = "Tom"; PrivateString getMessage () {return  This. Message; }} Public classhelloimpl{PrivateMessageimpl Messageimpl; Private voidSetmessageimpl (Messageimpl messageimpl) { This. Messageimpl =Messageimpl; }     PublicString Hello () {return"Hello:" +Messageimpl.getmessage (); }}<bean id= "Messageimpl"class= "Wang.conge.MessageImpl"/><bean id= "Testbean"class= "Wang.conge.HelloImpl" > <property name= "Messageimpl" ref= "Messageimpl"/> </bean>

4. Set Injection

Map,set,list,collection

Iv. deferred initialization of beans

Lazy initialization, also known as lazy initialization, is to create and initialize a bean only when it is actually used, rather than initializing the bean in advance.

Configuration is simple simply specify the "Lazy-init" property value of "true" on the <bean> tab to defer initialization of the bean.

v. Use of depends-onDepends-on is the order in which bean initialization and destruction is specified. The bean specified with the Depends-on property initializes the current bean before it is initialized, since only the "singleton" Bean can be destroyed by spring management, so when the specified bean is "singleton", The bean specified with the Depends-on property is destroyed after the specified bean. vi. Scope of the bean1. Singleton: The bean that refers to the "singleton" scope only has one instance in each spring IOC container, and its full life cycle is fully managed by the spring container. For all operations that get the bean, the spring container will return only the same bean. 2, Prototype: That is, the prototype, each time to the spring container request to get a bean to return a brand-new bean, relative to "singleton" is not to cache the bean, each time is a bean definition based on the creation of a new bean.

In a Web application, we may need to store the data in the request, session,Global session. So spring provides three web scopes: request, session, Globalsession.

3. Request scope: Indicates that each request requires a container to create a new bean. For example, the data that submits the form must be a new bean for each request to keep the form data, and the request to end the release of the data.

4. Session scope: Indicates that each session requires a container to create a new bean. For example, for each user there will typically be a session, the user's user information needs to be stored in the session, this bean can be configured as a Web scope.

5. Globalsession: Similar to session scope, it is just a Web application for the Portlet environment. If the non-portlet environment is treated as a session scope.

SPRING-002-IOC Bean Configuration

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.