spring_組立ベルン


組立beanのタイプ:
1.注入基本タイプ:int,double,String,url,Class…など.
2.注入参照タイプ
3.注入リストと配列
4.注入セット
5.リソースファイルタイプのpropertiesを注入する
6.MAP注入
7.Resourceを注入する
8.注入日付タイプ(java.util.Date)
spring容器依存注入の方式は、構造方法とセット注入をセットし、セットセットで注入をセットする場合があります.
 
javabean(User類の例:)
 
/*        */
public class User {
	private int u_id;
	private String userName;
	private Set<String> Favorates;
	private List<String>lovers;
//score     
	private Map<String, Score> score;
          private Properties MOU;
	private Date birthday;
	private Resource pic;
	public void setU_id(int u_id) {
		this.u_id = u_id;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setFavorates(Set<String> favorates) {
		Favorates = favorates;
	}

	public void setScore(Map<String, Score> score) {
		this.score = score;
	}

	public void setMOU(Properties mou) {
		MOU = mou;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public void setPic(Resource pic) {
		this.pic = pic;
	}

	public Resource getPic() {
		return pic;
	}

	public int getU_id() {
		return u_id;
	}

	public String getUserName() {
		return userName;
	}

	public Set<String> getFavorates() {
		return Favorates;
	}

	public Map<String, Score> getScore() {
		return score;
	}

	public Properties getMOU() {
		return MOU;
	}

	public Date getBirthday() {
		return birthday;
	}

	public List<String> getLovers() {
		return lovers;
	}

	public void setLovers(List<String> lovers) {
		this.lovers = lovers;
	}

}
 
  スコアクラスのjava
 
public class Score {
	private int s_id;
	private int score;

	public int getS_id() {
		return s_id;
	}

	public void setS_id(int s_id) {
		this.s_id = s_id;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

}
  設定ファイルは以下の通りです
まず基本タイプと集合タイプを見て、dateとresourceの特殊な点の後に補充します.
 
<!--      -->
                      <bean class="com.fisher.domain.User" id="user">
		<property name="userName" value="fisher"></property>
		<property name="u_id" value="1"></property>
		<!-- list       -->
		<property name="lovers">
			<list>
				<value>susan</value>
				<value>Angle</value>
			</list>
		</property>
		<!-- set  , list     -->
		<property name="favorates">
			<set>
				<value>computer</value>
				<value>basketball</value>
			</set>
		</property>
		<!-- map              -->
		<property name="score">
			<map>
				<entry key="  " value-ref="score"></entry>
			</map>
		</property>
		<!-- properties      -->
		<property name="MOU">
			<props>
				<prop key="today" >rain!</prop>
				<prop key="tomorrow">nice weather!</prop>
			</props>
		</property>
</bean>
 <!--Score-->
   <bean class="com.fisher.domain.Score" id="score">
		<property name="s_id" value="1"></property>
		<property name="score" value="90"></property>
	</bean>
 
  今はDateとResourceを解決します.
 spring読み取り構成の場合、valueの値は文字列タイプであり、dateタイプは直接にxmlファイルに値を注入することができません.
spring 2.0は非常にプログレッシブな容器を内蔵しています.多くはすでにspring容器に登録されています.例えば、Resource資源、url
残念なことに、日付タイプが登録されていないので、他の手段を通じて登録せざるを得ません.
jdkはjava.beans.PropertyEditors Supportというクラスを提供しています.これはProptyEditorというインターフェースを実現しています.このクラスを通じて自分で属性の編集を完成することができます.
 具体的な実現:
 
public class DatePropertyEditor extends PropertyEditorSupport {

	@Override
	public void setAsText(String str) throws IllegalArgumentException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");//    1900/2/22    
		try {
			Date date = format.parse(str);//     ,          
			this.setValue(date);//  date    
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
 
public class DatePropertyEditor extends PropertyEditorSupport {

	@Override
	public void setAsText(String str) throws IllegalArgumentException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");//    1900/2/22    
		try {
			Date date = format.parse(str);//     ,          
			this.setValue(date);//  date    
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
  xmlファイルの対応する構成は以下の通りです.
 DatePropertyEditorのクラス:
 
<!-- dateEditor -->
	<bean class="com.fisher.util.DatePropertyEditor" id="dateEditor">
	</bean>
 userにおける注入属性の設定は以下の通りである.
 
<!-- date       -->
                     <bean class="com.fisher.domain.User" id="user">
		<property name="birthday" value="1987/2/12"></property>
                     </bean>
 また、springが提供するクラスを紹介します.org.springframe ebork.beans.factory.co-fig.ctomEditoConfigrer:
このように、spring容器が読み込まれ、XMLファイルを読み込むと、実例クラスのときに属性エディタが検出され、bean属性エディタCustomによって、xmlに属性が注入されたstringタイプが異なるデータタイプに変換されます.
 
 
	<!-- editor  
	           Spring    CustomEditor  String                   。
               java.util.Date  ,          CustomEditor             。
	-->
	<bean
		class="org.springframework.beans.factory.config.CustomEditorConfigurer">
                        <!--customEditor map    -->
		<property name="customEditors">
			<map>
                                               <!--key       ,value         dateEditor -->
				<entry key="java.util.Date" value-ref="dateEditor">
				</entry>
			</map>
		</property>
	</bean>
  Resourceは簡単になりました.
 
<!-- resource            value         --><!--        ,classpath:,file:////
			  url-->
		<property name="pic" value="file:////c:/RBK.jpg"></property>
 リソースの読み取り方法はどうなりますか?
ここにテストクラスがあります.
 
	public void testUser() throws IOException {
		bf = new ClassPathXmlApplicationContext("/bean.xml");
		User user = (User) bf.getBean("User");
		Resource res = user.getPic();
		InputStream is = res.getInputStream();
		byte[] by = new byte[is.available()];
		is.read(by);
		OutputStream os = new FileOutputStream("f:/rbk.jpg");
		os.write(by);
		os.close();

	}