zoukankan      html  css  js  c++  java
  • spring其他配置 (3)

    一、自动装配 Autowired

    Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 的 autowire 属性里指定自动装配的模式

    • byType: 根据类型进行自动装配. 但要求 IOC 容器中只有一个类型对应的 bean, 若有多个则无法完成自动装配.
    • byName: 若属性名和某一个 bean 的 id 名一致, 即可完成自动装配. 若没有 id 一致的, 则无法完成自动装配
    <bean id="service" class="com.hp.spring.ref.Service" autowire="byName"></bean>
        
    <bean id="action" class="com.hp.spring.ref.Action" autowire="byType"></bean>  
    

    二、bean的作用于singleton,prototype

    默认情况下 bean 是单例的!
    但有的时候, bean 就不能使单例的. 例如: Struts2 的 Action 就不是单例的! 可以通过 scope 属性来指定 bean 的作用域

    • prototype: 原型的. 每次调用 getBean 方法都会返回一个新的 bean. 且在第一次调用 getBean 方法时才创建实例
    • singleton: 单例的. 每次调用 getBean 方法都会返回同一个 bean. 且在 IOC 容器初始化时即创建 bean 的实例. 默认值
    <bean id="dao2" class="com.hp.spring.ref.Dao" scope="prototype"></bean>  
    

    三、引入外部资源properties文件

        <!-- 导入外部的资源文件 -->
        <context:property-placeholder location="classpath:db.properties"/>
        
        <!-- 配置数据源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
            
            <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
            <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
        </bean>  
    

    db.properties

    jdbc.user=root
    jdbc.password=root
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.jdbcUrl=jdbc:mysql:///test
    jdbc.initPoolSize=5
    jdbc.maxPoolSize=10  
    

    四、SpEL表达式 (可以为属性进行动态的赋值)

    例子中,使用#{girl.userName}将bean为girl的userName动态注入到bean为boy的对象中

        <!-- 测试 SpEL: 可以为属性进行动态的赋值 -->
        <bean id="girl" class="com.hp.spring.helloworld.User">
            <property name="userName" value="测试"></property>
        </bean>
        
        <bean id="boy" class="com.hp.spring.helloworld.User" >
            <property name="userName" value="测试姓名"></property>
            <property name="wifeName" value="#{girl.userName}"></property>
        </bean>  
    

    关于SpEL的使用,查看 SpEL的使用.md

    五、通过工厂方式类创建bean

    1 使用静态工厂来创建bean

        <!-- 在 class 中指定静态工厂方法的全类名, 在 factory-method 中指定静态工厂方法的方法名 -->
        <bean id="dateFormat" class="java.text.DateFormat" factory-method="getDateInstance">
            <!-- 可以通过 constructor-arg 子节点为静态工厂方法指定参数 -->
            <constructor-arg value="2"></constructor-arg>
        </bean>  
    

    测试代码

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-auto.xml");  
    DateFormat dateFormat = (DateFormat) ctx.getBean("dateFormat");
    System.out.println(dateFormat.format(new Date()));  
    

    打印出:
    2016-7-3

    2 使用实例工厂来创建bean

        <!-- ①. 创建工厂对应的 bean -->
        <bean id="simpleDateFormat" class="java.text.SimpleDateFormat">
            <constructor-arg value="yyyy-MM-dd hh:mm:ss"></constructor-arg>
        </bean>
        
        <!-- ②. 有实例工厂方法来创建 bean 实例 -->
        <!-- factory-bean 指向工厂 bean, factory-method 指定工厂方法(了解) -->
        <bean id="datetime" factory-bean="simpleDateFormat" factory-method="parse">
            <!-- 通过 constructor-arg 执行调用工厂方法需要传入的参数 -->
            <constructor-arg value="1990-12-12 12:12:12"></constructor-arg>
        </bean>  
    

    测试代码

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-auto.xml");  
    Date date = (Date) ctx.getBean("datetime");
    System.out.println(date);  
    

    打印出:
    Wed Dec 12 00:12:12 CST 1990

    六、通过FactoryBean方式创建bean

        <!-- 配置通过 FactroyBean 的方式来创建 bean 的实例(了解) -->
        <bean id="user" class="com.hp.spring.ref.UserBean"></bean>  
    

    UserBean这个类需要实现FactoryBean借口

    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.beans.factory.FactoryBean;
    import com.hp.spring.helloworld.Car;
    import com.hp.spring.helloworld.User;
    public class UserBean implements FactoryBean<User>{
        /**
         * 返回的 bean 的实例
         */
        @Override
        public User getObject() throws Exception {
            User user = new User();
            user.setUserName("abc");
            user.setWifeName("ABC");
            
            List<Car> cars = new ArrayList<>();
            cars.add(new Car("ShangHai", "BuiKe", 180, 300000));
            cars.add(new Car("ShangHai", "CRUZE", 130, 150000));
            
            user.setCars(cars);
            return user;
        }
        /**
         * 返回的 bean 的类型
         */
        @Override
        public Class<?> getObjectType() {
            return User.class;
        }
        /**
         * 返回的 bean 是否为单例的
         */
        @Override
        public boolean isSingleton() {
            return true;
        }
    }
    

    关于User类跟Car类,则是User引用了一个Car类的集合
    其中,测试代码:

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-auto.xml");  
    User user = (User) ctx.getBean("user");
    System.out.println(user);  
    

    打印出:
    User [userName=abc, cars=[Car [company=ShangHai, brand=BuiKe, maxSpeed=180, price=300000.0], Car [company=ShangHai, brand=CRUZE, maxSpeed=130, price=150000.0]]]

    七、使用init-method="init" destroy-method="destroy"在bean的初始化和销毁的时候调用

        <bean id="user" class="com.hp.spring.helloworld.User" init-method="init" destroy-method="destroy">
        </bean>  
    

    User类中有init和destroy方法

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

    整个系列项目代码: http://git.oschina.net/nmc5/spring

  • 相关阅读:
    Machine learning 第8周编程作业 K-means and PCA
    Machine learning 第7周编程作业 SVM
    Machine learning第6周编程作业
    Machine learning 第5周编程作业
    小M的作物 最小割最大流
    k-近邻算法 python实现
    编辑距离 区间dp
    Machine learning第四周code 编程作业
    MDK5报错missing closing quote
    HDU 5512
  • 原文地址:https://www.cnblogs.com/linhp/p/5881770.html
Copyright © 2011-2022 走看看