詳細な使用IDEAスプリング依存性注入タイプ()
スプリング実装IoCコンテナのアプローチがある依存性注入、注射依存効果は、Spring Frameworkがあるオブジェクトを作成するときに動的にそれらに依存するオブジェクト(例えば、属性値)注入Beanコンポーネントの。
スプリングフレームワークの依存性注入は、通常、2つの方法で実装され、一つは使用することであるコンストラクタ注入を他のプロパティを使用することで、セッター注入法。
使用コンストラクタ・インジェクション
スプリングフレームワークは、使用することができるJavaリフレクションをすることにより、コンストラクタ完全な依存性注入。
プロジェクトの作成とモジュールのMavenのインポートプロセスを参照してください「を使用する春のIDEAは、エントリー手続きを開発し、この詳細には触れないで」。この継続的なプロジェクトの前には、追加するには、以下の手順に従います。
作成したエンティティパッケージ、Personクラスを作成します
package entity;
public class Person {
private String name;
private String sex;
public Person() {
System.out.println("无参构造调用了...");
}
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
复制代码
コンストラクタインジェクションA方式
書き込み設定ファイル
では 、SRCルートディレクトリ Spring構成ファイルを作成 applicationContext.xmlをします 。コンフィギュレーション・ファイル最初に entity.Personの にホスティングクラス 春 、春はそれを作成してみましょう オブジェクトを コンストラクタにしながら、 パス引数 。詳細なコードの設定ファイルは次のとおりです。
<?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">
<!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring创建其实例-->
<!--
一个Bean标签可以注册一个组件(对象、类)
class:写要注册的组件的全类名
id:这个对象的唯一标识
-->
<bean id="test" class="dao.TestDaoImpl"/>
<bean id="person1" class="entity.Person"/>
<!--使用构造方法注入-->
<bean id="person2" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg name="name" value="泰斗贤若如"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
</bean>
</beans>
复制代码
クラスTestDemoでテストテスト
package test;
import dao.TestDao;
import entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
public class TestDemo {
@Test
public void test4(){
//初始化spring容器ApplicationContext,加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取test实例
Person person2 =(Person) applicationContext.getBean("person2");
System.out.println("姓名:"+person2.getName()+";"+"性别:"+person2.getSex());
}
}
复制代码
テスト結果
コンストラクタ・インジェクションモード2
書き込み設定ファイル
<?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">
<!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring创建其实例-->
<!--
一个Bean标签可以注册一个组件(对象、类)
class:写要注册的组件的全类名
id:这个对象的唯一标识
-->
<bean id="test" class="dao.TestDaoImpl"/>
<bean id="person1" class="entity.Person"/>
<!--使用构造方法注入-->
<bean id="person2" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg name="name" value="泰斗贤若如"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
</bean>
<!--可以省略name属性,严格按照构造器参数的位置赋值-->
<bean id="person3" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg value="泰斗贤若如"></constructor-arg>
<constructor-arg value="男"></constructor-arg>
</bean>
</beans>
复制代码
クラスTestDemoでテストテスト
package test;
import dao.TestDao;
import entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
public class TestDemo {
@Test
public void test5(){
//初始化spring容器ApplicationContext,加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取test实例
Person person3 =(Person) applicationContext.getBean("person3");
System.out.println("姓名:"+person3.getName()+";"+"性别:"+person3.getSex());
}
}
复制代码
テスト結果
この方法を使用する場合は、できることを注意厳密にコンストラクタのパラメータ割り付けの位置に応じて名前などの割り当てがそうでない場合は、もちろん、文句はありませんが、割り当て混乱になりますが、もちろん私たちのしている、セックスに割り当てますあなたは非割り当て(ビット極端な、革の表情)の要件に従わないかどうかを確認したい、あなたが避けることができる方法があります割り当ての混乱は、以下のコードを考えてみます。
書き込み設定ファイル
<?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">
<!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring创建其实例-->
<!--
一个Bean标签可以注册一个组件(对象、类)
class:写要注册的组件的全类名
id:这个对象的唯一标识
-->
<bean id="test" class="dao.TestDaoImpl"/>
<bean id="person1" class="entity.Person"/>
<!--使用构造方法注入-->
<bean id="person2" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg name="name" value="泰斗贤若如"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
</bean>
<!--可以省略name属性,严格按照构造器参数的位置赋值-->
<bean id="person3" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg value="泰斗贤若如"></constructor-arg>
<constructor-arg value="男"></constructor-arg>
</bean>
<bean id="person4" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<!--index="1",为参数指定索引,从0开始-->
<constructor-arg value="男" index="1"></constructor-arg>
<constructor-arg value="泰斗贤若如" ></constructor-arg>
</bean>
</beans>
复制代码
クラスTestDemoでテストテスト
package test;
import dao.TestDao;
import entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
public class TestDemo {
@Test
public void test5(){
//初始化spring容器ApplicationContext,加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取test实例
Person person4 =(Person) applicationContext.getBean("person4");
System.out.println("姓名:"+person4.getName()+";"+"性别:"+person4.getSex());
}
}
复制代码
テスト結果
行うにはどのように、高負荷が発生した場合、私は思っていた、これは完成だろうと思いませんか?分解ダウン私を見て:
次のようにPersonクラスのパケットの下のエンティティが変更されました
package entity;
public class Person {
private String name;
private String sex;
private Integer age;
private String email;
public Person() {
System.out.println("无参构造调用了...");
System.out.println("Person创建了...");
}
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
System.out.println("有参构造器");
}
public Person(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public Person(String name, String sex, String email) {
this.name = name;
this.sex = sex;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
复制代码
書き込み設定ファイル
<?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">
<!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring创建其实例-->
<!--
一个Bean标签可以注册一个组件(对象、类)
class:写要注册的组件的全类名
id:这个对象的唯一标识
-->
<bean id="test" class="dao.TestDaoImpl"/>
<bean id="person1" class="entity.Person"/>
<!--使用构造方法注入-->
<bean id="person2" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg name="name" value="泰斗贤若如"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
</bean>
<!--可以省略name属性,严格按照构造器参数的位置赋值-->
<bean id="person3" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg value="泰斗贤若如"></constructor-arg>
<constructor-arg value="男"></constructor-arg>
</bean>
<bean id="person4" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<!--index="1",为参数指定索引,从0开始-->
<constructor-arg value="男" index="1"></constructor-arg>
<constructor-arg value="泰斗贤若如" ></constructor-arg>
</bean>
<bean id="person5" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!--public Person(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public Person(String name, String sex, String email) {
this.name = name;
this.sex = sex;
this.email = email;
} -->
<!--重载的情况下type可以指定参数的类型-->
<constructor-arg value="男" index="1"></constructor-arg>
<constructor-arg value="泰斗贤若如" index="0"></constructor-arg>
<constructor-arg value="22" index="2" type="java.lang.Integer"></constructor-arg>
</bean>
</beans>
复制代码
クラスTestDemoでテストテスト
package test;
import dao.TestDao;
import entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
public class TestDemo {
@Test
public void test6(){
//初始化spring容器ApplicationContext,加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取test实例
Person person5 =(Person) applicationContext.getBean("person5");
System.out.println("姓名:"+person5.getName()+";"+"性别:"+person5.getSex()+";"+"年龄:"+person5.getAge());
}
}
复制代码
テスト結果
しかし、その後、彼は来た、と明らかに名前がようなものがそのように、複雑になるなぜそれを行うことができます一般的にはある道のA
使用してプロパティのセッターメソッドを注入
この部分は、少しより多くのスペースを、それを説明するの隣に配置、ご期待ください!