Spring's bean Management

Source: Internet
Author: User

1. All the classes can be given to spring management

2, how to give a class to bean management?

(1) Configuration Applicationcontext.xml

(2) Write the bean node configuration to the bean-managed class in XML

3. Program Test

(1) To import the Spring core container jar package, the spring kernel contains the following packages:

(2) Create a new Applicationcontext.xml file, this file will be placed under the SRC folder, otherwise cannot find the file, configure the root node beans, and specify the namespace:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >    <Bean></Bean></Beans>

(3) Configure the Bean node, assuming that there is a user entity class

 Public classUser {intID;    String name;    String password;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }    }

(4) configuration to bean-managed format You can use the ID to specify an object name,

You can also use name to specify multiple aliases.

At the same time, class specifies the fully qualified name of the class that the bean is associated with,

If you want to initialize, you can specify Init-method,

If you need to work when destroying, specify Destory-method

The default is singleton mode, if not singleton mode, you need to specify Scope= "prototype" in scope

If you want to specify lazy loading, specify Lazy-init

<id= "user1"  class= "Com.spring1.entity.User"  Init-method  = "Init"  destroy-method= "destory"/>

Test in JUnit:

@Test      Public void Test () {        new classpathxmlapplicationcontext ("Applicationcontext.xml");         = (User) ac.getbean ("user1");    }

You can see that the Get object succeeds.

Spring's bean Management

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.