• Spring Bean


    一、获取Bean

    spring-config.xml
    <bean id="myBean" class="com.lzp.mySpring.MyBean"/>

     Main.java

    ApplicationContext context =
    new ClassPathXmlApplicationContext("spring-config.xml");
    
    MyBean myBean = (MyBean) context.getBean("myBean");

    二、构造函数

    MyBean2.java
    public class MyBean2 {
        
        private MyBean myBean;
        
        public MyBean getMyBean() {
            return myBean;
        }
    
        public void setMyBean(MyBean myBean) {
            this.myBean = myBean;
        }
    
        private int intProperty;
    
        public int getIntProperty() {
            return intProperty;
        }
    
        public void setIntProperty(int intProperty) {
            this.intProperty = intProperty;
        }
    
        public MyBean2(int intProperty,MyBean myBean) {
            this.intProperty = intProperty;
            this.myBean=myBean;
            
        }
    }
    spring-config.xml
     <bean id="myBean2" class="com.lzp.mySpring.MyBean2">
             <constructor-arg value="12"></constructor-arg>
             <constructor-arg type="com.lzp.mySpring.MyBean" ref="myBean"></constructor-arg>
         </bean>

    三、静态工厂方法

    FactoryMethodBean.java
    public class FactoryMethodBean {
        private FactoryMethodBean() {
        }
    
        private static class InnerClass {
            static FactoryMethodBean instance = new FactoryMethodBean();
        }
    
        public static FactoryMethodBean getInstance() {
            return InnerClass.instance;
    
        }
    }
    spring-config.xml
    <bean id="factoryMethodBean" class="com.lzp.mySpring.FactoryMethodBean" factory-method="getInstance"></bean>

     四、Bean声明周期

    singleton 单例(默认)

    prototype每次实例化都需要实例

    request单请求 session global-session  session基于web

    五、初始化和销毁

    spring-config.xml

       <!-- 注意这句话,如果是singleton或者没有该句(默认情况)时,才会执行destroy-method指定的方法,如果是当前的prototype不会触发destroymethod的执行 -->
    
    <bean id="myBean" class="com.lzp.mySpring.MyBean" init-method="init" destroy-method="destroy" />

    MyBean.java

    public class MyBean {
        public void init(){System.out.println("init");}
        public void destroy(){System.out.println("destroy");}
    }

     Main.java

    public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
            MyBean myBean = (MyBean) context.getBean("myBean");
            
             
             ((ClassPathXmlApplicationContext) context).close();
          //加上close才能调用destroy方法
        }

    六、属性装配

    属性装配和构造状态类似,除此之外多出了一种p标签,p标签比较简洁,spring2.5之后可用

    spring-config

    xmlns:p="http://www.springframework.org/schema/p" 需要自行添加
    <bean id="myBean3" class="com.lzp.mySpring.MyBean3">
            <property name="simpleProperty" value="12"></property>
            <property name="myBean" ref="myBean"></property>
            <property name="list">
                <list>
                    <ref bean="myBean" />
                </list>
            </property>
            <property name="map">
                <map>
                    <entry key="one" value-ref="myBean"></entry>
                </map>
            </property>
            <property name="set">
                <set>
                    <ref bean="myBean" />
                </set>
            </property>
            <property name="properties">
                <props>
                    <prop key="a">a</prop>
                </props>
            </property>
        </bean>
    MyBean3.java
    
    
    public class MyBean3 {
         private Integer simpleProperty;
    
        public Integer getSimpleProperty() {
            return simpleProperty;
        }
    
        public void setSimpleProperty(Integer simpleProperty) {
            this.simpleProperty = simpleProperty;
        }
        private MyBean myBean;
    
        public MyBean getMyBean() {
            return myBean;
        }
    
        public void setMyBean(MyBean myBean) {
            this.myBean = myBean;
        }
        
        private ArrayList<MyBean> list;
        private HashMap<String,MyBean> map;
        public ArrayList<MyBean> getList() {
            return list;
        }
    
        public void setList(ArrayList<MyBean> list) {
            this.list = list;
        }
    
        public HashMap<String, MyBean> getMap() {
            return map;
        }
    
        public void setMap(HashMap<String, MyBean> map) {
            this.map = map;
        }
    
        public HashSet<MyBean> getSet() {
            return set;
        }
    
        public void setSet(HashSet<MyBean> set) {
            this.set = set;
        }
    
        public Properties getProperties() {
            return properties;
        }
    
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
        private HashSet<MyBean> set;
        private Properties properties;
        
    }
  • 相关阅读:
    ES6常用新特性
    jquery基础总结 -- 转载
    正则验证
    prop attr 到底哪里不一样?
    分页导航 获取当前页码 的 分页导航哦
    使用Bootatrap的心得
    使用padding来合理布局自己的容器类
    使用angular-ui-router替代ng-router
    使用jitpack来获取github上的开源项目
    关于移动端的UI事件分类
  • 原文地址:https://www.cnblogs.com/javabeginer/p/6655248.html
Copyright © 2020-2023  润新知