zoukankan      html  css  js  c++  java
  • 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;
        
    }
  • 相关阅读:
    TypeScript02 方法特性【参数种类、参数个数】、generate方法、析构表达式、箭头表达式、循环
    TypeScript01 编译环境的搭建、字符串特性、类型特性
    Angular04 组件动态地从外部接收值、在组件中使用组件
    Angular03 将数据添加到组件中
    Angular02 通过angular-cli来搭建web前端项目
    Angular01 利用grunt搭建自动web前端开发环境、利用angular-cli搭建web前端项目
    IDEA01 创建java项目、创建web项目
    Struts2框架07 Struts2 + Spring + Mybatis 整合
    素数应用
    二重指针实现排序
  • 原文地址:https://www.cnblogs.com/javabeginer/p/6655248.html
Copyright © 2011-2022 走看看