Java-spring Management Bean Example

Source: Internet
Author: User

Spring manages beans in 2 different ways

The first thing to do is to import Spring packages (Spring.jar and Commonslogging.jar) or load separate ...

Build Applicationcontext.xml (Spring Management Bean configuration file) in src directory

<? XML version= "1.0" encoding= "UTF-8" ?> <! DOCTYPE Beans Public "-//speing//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd " > < Beans >    <  ID= "User"  class= "Com.cc8w.entiey.User"/> </ Beans >

1. Managing Beans with Beanfactory

2. Managing Beans with ApplicationContext

 Packagecom.cc8w;Importorg.springframework.beans.factory.BeanFactory;Importorg.springframework.beans.factory.xml.XmlBeanFactory;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportOrg.springframework.core.io.ClassPathResource;ImportOrg.springframework.core.io.Resource;ImportCom.cc8w.entiey.User; Public classTestbean { Public Static voidMain (string[] args) {//beanfactory Management BeanResource Resource =NewClasspathresource ("Applicationcontext.xml"); Beanfactory Factory=Newxmlbeanfactory (Resource); User User= (user) Factory.getbean ("user"));                SYSTEM.OUT.PRINTLN (user); //ApplicationContext Management BeanApplicationContext context =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); User User1= (user) Context.getbean ("user"));            System.out.println (user1); }}

Data types for Java

 PackageCom.cc8w.entiey; Public classUser {PrivateString name; Private intAge ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }    }

Results:

Well, it's wrong, it should be the entity.

Spring Control inversion Dependency Injection (configuration initialization parameters-instantiation parameters)

1. Interface Injection

2.Setter Injection (recommended)

3. Constructor injection

Java-spring Management Bean Example

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.