記事のディレクトリ
SpringIoCコンテナの概要
- SpringコンテナはIoCの原則を具体化しています
- Springコンテナーは、構成メタデータを読み取ることにより、Beanのインスタンス化、構成、およびアセンブルを行います。
- 構成メタデータは、XML、Javaアノテーション、またはJavaコードで記述できます。
Springコンテナは、構成ファイルを読み取り、オブジェクトをコンテナに配置して、オブジェクトを自動的に生成します。
BeanFactory
使用するオブジェクトのインスタンス化を支援するために、反射テクノロジーが使用されます。
BeanFactoryは実際にはファクトリモデルです。
クラスの例...
ApplicationContext
これはBeanFactoryのサブインターフェースであり、BeanFactoryよりも多くの機能を備えています。
スーパーです。
4つの実装クラスがあります。(ApplicationContextの実装)
- ClassPathXmlApplicationContextは
、ファイルのルートディレクトリに配置されたXMLファイルの構成に基づいています。 - FileSystemXmlApplicationContext
XMLファイルはオペレーティングシステムのパスに配置されます(C:/ D:何)
WEBプログラム構成に基づくXmlWebApplicationContext
完全に注釈が付けられたAnnotationConfigApplicationContext
スーパーセット=より多くのものを提供する
Beanの概要
コンストラクターのインスタンス化
プロジェクトの構造
Desk.java
package com;
public class Desk {
private static Desk desk;
public Desk() {
System.out.println("Desk构造方法");
}
public static Desk createInstance() {
System.out.println("createInstance");
if(desk==null)
desk = new Desk();
return desk;
}
}
Test.java
package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Desk desk = (Desk)ctx.getBean("Desk");
}
}
運転結果
静的ファクトリメソッドのインスタンス化
工法の具体化に基づいて変更を加えることができます
Bean.xmlファイルを変更します
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
增加Factory-method属性即可
<bean id="Desk" class="com.Desk" factory-method="createInstance"/>
</beans>
インスタンスファクトリのインスタンス化
プロジェクトの構造
Chair.java
package com;
public class Chair {
public Chair() {
System.out.println("Chair无参数构造");
}
}
ChairFactory.java
package com;
public class ChairFactory {
public Chair create() {
return new Chair();
}
}
Desk.java
package com;
public class Desk {
private static Desk desk;
public Desk() {
System.out.println("Desk构造方法");
}
public static Desk createInstance() {
System.out.println("createInstance");
if(desk==null)
desk = new Desk();
return desk;
}
}
Test.java
package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Chair chair=(Chair)ctx.getBean("Chair");
}
}
Beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="Desk" class="com.Desk" factory-method="createInstance"/>
<bean id="ChairFactory" class="com.ChairFactory"></bean>
<!-- factory-bean的属性值是上面的id值,不是类名 -->
<!-- factory-methid的属性值是工厂类的方法名 -->
<bean id="Chair" factory-bean="ChairFactory" factory-method="create"></bean>
</beans>
運転結果
Beanのライフサイクル
Awareインターフェースを実装し、カスタムメソッド構成を使用します
体系的な方法とユーザー定義の方法です
プロジェクトの構造
BeanLife.java
package com;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
//实现接口
public class BeanLife implements ApplicationContextAware, InitializingBean, DisposableBean{
private ApplicationContext ctx;
private String name;
public void setName(String name) {
this.name = name;
System.out.println("setName属性的赋值");
}
public BeanLife() {
System.out.println("BeanLife的无参构造方法");
}
public void show() {
System.out.println(name);
}
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
ctx = arg0;//让自己的类可以用传来的ApplicationContext参数
System.out.println("applicationAware");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
}
public void init1() {
System.out.println("init1");
}
public void destroy1() {
System.out.println("destroy1");
}
}
Test.java
package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// BeanLife bl = (BeanLife)ctx.getBean("BeanLife");
// bl.show();
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
BeanLife bl = (BeanLife)ctx.getBean("BeanLife");
bl.show();
ctx.close();//关闭的方法在实现类里面,不再容器里面
}
}
Beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="BeanLife" class="com.BeanLife" init-method="init1" destroy-method="destroy1">
<property name="name" value="ZhangSan"></property>
</bean>
</beans>
運転結果
注釈を使用して構成する
上記と比較すると、Bean.xmlファイルのbean属性のみが削除され、アノテーションフォームに置き換えられ、一部のアノテーションがBeanFactoryに追加されます。
プロジェクトの構造
BeanLife
package com;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
//实现接口
@Component("BeanLife")
public class BeanLife implements ApplicationContextAware, InitializingBean, DisposableBean{
private ApplicationContext ctx;
private String name;
public void setName(String name) {
this.name = name;
System.out.println("setName属性的赋值");
}
public BeanLife() {
System.out.println("BeanLife的无参构造方法");
}
public void show() {
System.out.println(name);
}
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
ctx = arg0;//让自己的类可以用传来的ApplicationContext参数
System.out.println("applicationAware");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
}
@PostConstruct
public void init1() {
System.out.println("init1");
}
@PreDestroy
public void destroy1() {
System.out.println("destroy1");
}
}
Beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com">
</context:component-scan>
</beans>
Beanスコープ