春(b)の説明

春ロードコンテナ1、三つの方法

public class ServiceTest {
        public static void main(String[] args) {
            //Spring容器加载有3种方式

            //第一种:ClassPathXmlApplicationContext ClassPath类路径加载,指的就是classes路径
            //第一种:最常用,spring的配置文件路径以后就直接放在src
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

            //第二种方式:文件系统路径获得配置文件【绝对路径】
            //ApplicationContext context = new FileSystemXmlApplicationContext("C:\\Users\\Desktop\\IDEAWorkspace\\spring-01\\src\\com\\rookie\\beans.xml");

            //第三种方式:使用BeanFactory(了解)
            //String path = "C:\\Users\\Desktop\\IDEAWorkspace\\spring-01\\src\\com\\rookie\\beans.xml";
            //BeanFactory factory = new XmlBeanFactory(new FileSystemResource(path));
            //IUserService user = (IUserService) factory.getBean("userService");
            //user.add();

            IUserService user = (IUserService) context.getBean("userService");
            user.add();
        }
    }

春の内側にオブジェクトを作成するための原則

。xmlファイルの解析、その上のクラス名、ID、財産とを取得します。

B。徹底的反射型のオブジェクトを作成するために使用されます。

C。オブジェクトの割り当てを作成します。

2、たBeanFactoryとApplicationContextの比較

遅延ロードを取るたBeanFactory、Beanが初めてgetBeanを初期化します。

ApplicationContextのは熱心たBeanFactoryにロードされ、より多くの機能を提供するように拡張。

  • (に基づいて、第1のコードで)ケース・プレゼンテーション

    public class UserServiceImpl implements UserService {
    
        private String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public void add() {
            System.out.println("创建用户...." + name);
        }
    
        public UserServiceImpl(){
            System.out.println("UserServiceImpl() 调用了");
        }
    }
    public class ServiceTest {
    
        public static void main(String[] args) {
            //1.加载beans.xml 这个spring的配置文件,内部就会创建对象
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        }
    }

    コンソールログを出力します:UserServiceImpl()の呼び出し

    public class ServiceTest {
    
        public static void main(String[] args) {
            String path = "C:\\Users\\Desktop\\IDEAWorkspace\\spring-01\\src\\com\\rookie\\beans.xml";
    BeanFactory factory = new XmlBeanFactory(new FileSystemResource(path));
    
            // 要使调用空参构造可以放开这段代码即可
            // IUserService user = (IUserService) factory.getBean("userService");
        }
    }

    コンソールは、コンストラクタが呼び出されていない空の引数を示し、ログを印刷しません。

    UserServiceImpl()の呼び出し:コード上のリリースノートでは、あなたは空の引数のコンストラクタ、コンソールの印刷ログを呼び出すことができます。

3つの方法の2、春豆容器アッセンブリー

いわゆるアセンブリは、XMLで豆豆のタグを記述することです。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--装配bean的三种方式,所谓的装配bean就是在xml写一个bean标签-->
    
    <!-- 第一种方式: new 实现类-->
    <bean id="userService1" class="com.example.demo.service.impl.UserServiceImpl">
        <property name="name" value="zhangsan"></property>
    </bean>

    <!-- 第二种方式:通过静态工厂方法 -->
    <bean id="userService2" class="com.example.demo.service.UserServiceFactory" factory-method="createUserService"/>

    <!--第三种方式:通过实例工厂方法 -->
    <bean id="factory2" class="com.example.demo.service.UserServiceFactory2"/>
    <bean id="userService3" factory-bean="factory2" factory-method="createUserService"/>
</beans>

試験アセンブリ法上のどのような豆、あなたは他の豆の組立方法をコメントする必要があります。

最初は、前回の話されてきた、そして今、彼らは、第二の3例について話しています。

public class UserServiceFactory {
    public static UserService createUserService() {
        return new UserServiceImpl();
    }
}

=========================================================================================
public class UserServiceFactory2 {
    public UserService createUserService() {
        return new UserServiceImpl();
    }
}

テスト関数を実行します

public class ServiceTest {

    public static void main(String[] args) {

        //new 对象
        ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService1 = (UserService) context1.getBean("userService1");
        userService1.add();


        //静态工厂
        ApplicationContext context2 = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService2 = (UserService) context2.getBean("userService2");
        userService2.add();


        //实例工厂
        ApplicationContext context3 = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService3 = (UserService) context3.getBean("userService3");
        userService3.add();

    }
}

その結果、自分自身を見ることができます興味があります。3つの結果は、BeanがSpringコンテナ管理によってインスタンス化されていることを証明しました。

3、豆スコープ(マスタシングルトン、プロトタイプ)

カテゴリ 説明
シングルトン 豆のインスタンスが1つだけ存在するスプリングIoCコンテナ、単一の実施形態の存在下でビーンよう、デフォルト値
プロトタイプ 毎回コール豆は、容器の新しいインスタンスを返す、すなわち、実装新しいXxxBeanに相当getBean()を呼び出すたびに、()
要求 新しいBeanを作成します。各HTTPリクエストは、その適用範囲は、環境だけに適用されますWebApplicationContext
セッション 同じHTTPセッションBeanを共有し、別のセッションが異なる豆を使用し、環境だけに適用されますWebApplicationContext
globalSession 一般的な環境のためのポートレット・アプリケーションは、スコープは、環境だけに適用されますWebApplicationContext

ケースコードが実証します

bean.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="userService1" class="com.example.demo.service.impl.UserServiceImpl" scope="prototype">
    </bean>
</beans>
public class ServiceTest {

    public static void main(String[] args) {
       
        ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService1 = (UserService) context1.getBean("userService1");
        UserService userService2 = (UserService) context1.getBean("userService1");
        System.out.println(userService1);
        System.out.println(userService2);
    }
}

次のようにコンソールの情報は次のとおりです。

com.example.demo.service.impl.UserServiceImpl@2a556333
com.example.demo.service.impl.UserServiceImpl@7d70d1b1

あなたは、ファイル=「プロトタイプ」bean.xml範囲を削除する場合は、以下の情報を印刷します:

com.example.demo.service.impl.UserServiceImpl@7a187f14
com.example.demo.service.impl.UserServiceImpl@7a187f14

したがって、豆春のIoCコンテナは、単一のマナーデフォルトで存在します!コンフィギュレーションの言葉自体は、単一または複数の例例に応じて設定する必要があります

おすすめ

転載: www.cnblogs.com/miantiao312/p/11790089.html