Bean的生命週期和建立方式

 1 package spring;  2 
 3 
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;  5 
 6 import java.util.Calendar;  7 
 8 public class Circle {  9 
10     private double radius; 11     private double peri; 12     private double area; 13 
14     public double getRadius() { 15         return radius; 16  } 17 
18     public void setRadius(double radius) { 19         this.radius = radius; 20  } 21 
22     public double getPeri() { 23         return peri; 24  } 25 
26     public void setPeri(double peri) { 27         this.peri = peri; 28  } 29 
30     public double getArea() { 31 
32         Calendar calendar = Calendar.getInstance(); 33         return area; 34  } 35 
36     public void setArea(double area) { 37         this.area = area; 38         System.out.println("執行Circle的setArea方法..."); 39  } 40 
41 
42 
43     public Circle(double radius) { 44         this.radius = radius; 45         System.out.println("執行Circle構造方法..."); 46  } 47 
48  @Override 49     public String toString() { 50         return "Circle{" +
51                 "radius=" + radius +
52                 ", peri=" + peri +
53                 ", area=" + area +
54                 '}'; 55  } 56 
57 
58     public void init(){ 59         System.out.println("執行Circle對象初始化..."); 60  } 61 
62 
63     public void destroy(){ 64         System.out.println("執行Circle對象銷燬..."); 65  } 66 
67 
68     public static void main(String[] args){ 69         ClassPathXmlApplicationContext cc = new ClassPathXmlApplicationContext("classpath:spel.xml"); 70         Circle life = cc.getBean("life",Circle.class); 71         System.out.println(life); 72  cc.close(); 73  } 74 
75 
76 }
 1 package spring.annotation;  2 
 3 import org.springframework.stereotype.Component;  4 
 5 @Component  6 public class AutoBean {  7 
 8     private String info = "AutoBean";  9 
10     public String getInfo() { 11         return info; 12  } 13 
14     public void setInfo(String info) { 15         this.info = info; 16  } 17 
18  @Override 19     public String toString() { 20         return "AutoBean{" +
21                 "info='" + info + '\'' +
22                 '}'; 23  } 24 }
 1 package spring.annotation;  2 
 3 import org.springframework.stereotype.Component;  4 
 5 
 6 @Component(value = "component")  7 public class DemoComponent {  8 
 9     public void print(){ 10         System.out.println(this.getClass().getName()); 11  } 12 
13 
14 }
 1 package spring.annotation;  2 
 3 import org.springframework.beans.factory.annotation.Autowired;  4 import org.springframework.stereotype.Controller;  5 
 6 @Controller(value = "controller")  7 public class DemoController {  8 
 9 
10  @Autowired 11     private AutoBean bean; 12 
13     public void print(){ 14         System.out.println(this.getClass().getName()); 15  System.out.println(bean); 16  } 17 
18 
19 }
 1 package spring.annotation;  2 
 3 import org.springframework.stereotype.Controller;  4 import org.springframework.stereotype.Repository;  5 
 6 @Repository(value = "repository")  7 public class DemoRepository{  8 
 9     public void print(){ 10         System.out.println(this.getClass().getName()); 11  } 12 
13 
14 }
 1 package spring.annotation;  2 
 3 import org.springframework.stereotype.Controller;  4 import org.springframework.stereotype.Service;  5 
 6 @Service(value = "service")  7 public class DemoService {  8 
 9     public void print(){ 10         System.out.println(this.getClass().getName()); 11  } 12 
13 
14 }

spring配置文件java

<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- bean的生命週期[Spring IOC容器管理] - 經過構造器或者工廠方法建立Bean的實例 - 爲Bean的屬性設置值或者引用其餘的Bean - 調用Bean的初始化方法 - 使用Bean - 容器關閉時,調用Bean的銷燬方法 -->
    <!--定義Bean的初始化和銷燬方法-->
    <!--構造方法建立Bean-->
    <!--<bean id="life" class="spring.Circle" init-method="init" destroy-method="destroy">-->
        <!--<constructor-arg name="radius" value="40"/>-->
        <!--<property name="area" value="#{T(java.lang.Math).PI * circle.radius^2}"/>-->
    <!--</bean>-->


    <!--靜態方法建立Bean-->
    <bean id="calendar" class="java.util.Calendar" factory-method="getInstance"/>
    <!--經過工廠bean的普通方法建立Bean-->
    <bean id="time" factory-bean="calendar" factory-method="getTime"/>

    <!-- 經過FactoryBean配置Bean的實例 class指向FactoryBean的全類名 property配置FactoryBean的屬性 實際返回的是FactoryBean的getObject()方法返回的實例 -->

    <bean id="factoryBean" class="spring.DemoFactoryBean">
        <property name="mod" value="#{T(java.lang.Math).ceil(T(java.lang.Math).random()*101)}"/>
    </bean>


    <!--經過註解掃描建立Bean-->
    <!--resource-pattern 只掃描特定的文件或者包-->
    <!--<context:component-scan base-package="spring" resource-pattern="annotation/DemoService.class" />-->

    <context:component-scan base-package="spring.annotation" use-default-filters="true">
        <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>-->
        <!--<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>-->

        <!--從掃描包中排除指定的類或者接口-->
        <context:exclude-filter type="assignable" expression="spring.annotation.DemoComponent"/>
        <!--<context:include-filter type="assignable" expression="spring.annotation.DemoComponent"/>-->

    </context:component-scan>

</beans>
相關文章
相關標籤/搜索