go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  2 Ways to Use Spring Bean
 
Subject: 2 Ways to Use Spring Bean
Author: WebSpider
In response to: Way#3 Spring Bean by Annotation
Posted on: 10/21/2019 05:34:31 AM


The life cycle (from instantiation to garbage collection) of Spring beans are controller by Spring IoC Container, but those beans are always standby and ready for use. There are two way to use Spring beans:

  • External Use -- Retrieve from the IoC Container by application
  • Internal Use -- Fulfill the dependency need for another bean from the IoC container.


     

    > On 10/20/2019 10:58:10 PM WebSpider wrote:


    The POJO class
    package com.example.spring.model;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Sex_Annotation_Bean {
    	
    	private String sex;
    	
    	public String getSex() {
    		return sex;
    	}
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    }
    


    The auto-detect configuration component-scan
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
        <bean name="myXmlBean" class="com.example.spring.model.Name_XML_Bean"></bean>
        
        <context:component-scan base-package="com.example.spring" />
    	
    </beans>
    






    References:

  •  


     
    Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.