Three ways to instantiate a Spring Bean manager bean

Source: Internet
Author: User
Tags assert

Three ways to implement bean instantiation

First: Create using the parameterless construction method of the class (common importance)

The first instantiation method is most commonly used, and the class is instantiated by calling the parameterless construction method to create it. The sample code is as follows:

Package Spring.com.userservice;public class UserService {public UserService () {//The method is an argument-free method}public void AddUser () { System.out.println ("Add ..... ................)

XML configuration:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "><bean id=" UserService "class=" Spring.com.UserService.UserService "></bean> </beans>

Unit tests:

Package Spring.com.test;import static Org.junit.assert.*;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Spring.com.userservice.userservice;public class Test {@org. junit.testpublic void Test () {ApplicationContext context= New Classpathxmlapplicationcontext ("Applicationcontext.xml"); UserService s= (UserService) Context.getbean ("UserService"); S.adduser ();}}

Results:

Method performs normally

If you manually add a method that has a parameter constructor, you will not find an parameterless construction method when you create it; Sample code:

Package Spring.com.userservice;public class UserService {public UserService (String name) {//The method is an argument method}public void AddUser () {System.out.println ("Add ... ..... ...............)

Results:

The parameterless constructor method cannot be found, so it cannot be instantiated. You must manually add an parameterless construction method

Package Spring.com.userservice;public class UserService {public UserService () {//The method is an argument-free method}public UserService (String Name) {//The method is an argument method}public void AddUser () {System.out.println ("Add ... ...... .............

  

Second: Create with static factory (you know)

Example code:

Create a factory class to use to create an instance of a class

Package Spring.com.userservice;public class Beanfactory {public static UserService getbeanfactory () {return new UserService ();}}

XML configuration:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "><bean id=" Factory "class=" Spring.com.UserService.BeanFactory "factory-method=" Getbeanfactory "></bean><!--Create a factory Create object--></beans>

  

Unit tests:

Package Spring.com.test;import static Org.junit.assert.*;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Spring.com.userservice.beanfactory;import Spring.com.userservice.userservice;public class Test {@ org.junit.Testpublic void Test () {ApplicationContext context=new classpathxmlapplicationcontext (" Applicationcontext.xml "); UserService s= (UserService) Context.getbean ("Factory"); S.adduser ();}}

  

Results:

Third: Create with instance factory (you know)

Example code:

Package Spring.com.userservice;public class Beanfactory {//Common factory method public UserService Getbeanfactory () {return new UserService ();}}

XML configuration:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "><bean id=" Bean "class=" spring.com.UserService.BeanFactory "></bean><bean Id= "Factory" factory-bean= "Bean" factory-method= "getbeanfactory" ></bean></beans>

Unit tests:

Package Spring.com.test;import static Org.junit.assert.*;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Spring.com.userservice.beanfactory;import Spring.com.userservice.userservice;public class Test {@ org.junit.Testpublic void Test () {ApplicationContext context=new classpathxmlapplicationcontext (" Applicationcontext.xml "); UserService s= (UserService) Context.getbean ("Factory"); S.adduser ();}}

Results:

Three ways to instantiate a Spring Bean manager bean

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.