春スターター(A) - IOC



1. IOCの定義

外容器のオブジェクトを作成するために、コンピュータコード間の結合度を低減するための制御の反転は、完成された、新しいを持っていません




2.プロセス


2.1の作成には、Beanのオブジェクト

package bean;

public class User {
    
    private String name;
    private String email;
    private String password;
    
    /**
    * 省略了getters/setters
    * @author Howl
    */
    
    //构造函数
    public User(String name, String email, String password) {
        super();
        this.name = name;
        this.email = email;
        this.password = password;
    }
}


2.2構成のapplicationContext.xmlを

<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容器 -->
    <!-- id 唯一表示 -->
    <!-- class 对应的Bean对象 -->
    <!-- scope 作用域,单例和多例 -->
    <bean id="User" class="bean.User" scope="singleton"></bean>
    
</beans> 


2.3 Beanオブジェクトは、コンテナを通じて取得しました

//通过配置文件获取上下文
applicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过上下文获取Bean对象,不用通过new方式了
User user = ac.getBean("User");




3.その他の詳細


3.1でオブジェクトを作成するための引数のコンストラクタ

<bean id="User" class="bean.User" scope="singleton">
    
    <!--constructor指定构造函数的参数类型、名称、第几个-->
    <constructor-arg index="0" name="name" type="String" value="Howl"></constructor-arg>
    <!--参数为对象时,value改为ref="" -->
</bean>


3.2ロードコレクション

<bean id="userService" class="bb.UserService" >
    <constructor-arg >
        <list>
            //普通类型
            <value></value>
            //引用类型
            <ref></ref>
        </list>
    </constructor-arg>
</bean>


3.3ノート

1.先引入context名称空间
xmlns:context="http://www.springframework.org/schema/context"

2.开启注解扫描器
<context:component-scan base-package=""></context:component-scan>

3.在Bean对象中添加@Component(name ="User"),就不用在配置文件中写<Bean>标签了




おすすめ

転載: www.cnblogs.com/Howlet/p/11979935.html