`
fisherhe
  • 浏览: 52253 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

spring_装配bean

阅读更多

装配bean的类型:

1.注入基本类型:int,double,String,url,Class....等 .

2.注入引用类型

3.注入list和数组

4.注入set集合

5.注入资源文件类型properties

6.注入MAP

7.注入Resources

8.注入日期类型(java.util.Date)

spring容器依赖注入的方式有两种:构造方法和set设置注入,以set设置注入为例:

 

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;
	}

}
 

  Score类中的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文件中注入值来完成。

spring2.0内置了很都propertyEditor容器的实现,很多已经注册到了spring容器中,比如可以访问Resource资源,和url

,遗憾的是日期类型没有被注册 ,这就不得不不通过其他手段

jdk提供了java.beans.PropertyEditorSupport这样一个类它实现了PropertyEditor这个接口,那么就可以通过这个类来自己完成对属性的编辑。

 具体的实现:

 

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.springframework.beans.factory.config.CustomEditorConfigurer:

这样做是当spring容器被加载,读取XML文件,实例化类时会检测到属性编辑器,并根据bean属性编辑器CustomEditor,将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();

	}

 

1
2
分享到:
评论

相关推荐

    Spring中的Bean的管理_Bean的装配方式_基于注解的装配_项目

    目的:Spring容器已经成功获取了UserController实例,并通过调用实例中的方法执行了各层中的输出语句。 运行结果为: User [id=1, name=张三, password=123] userDao say hello world! UserService say hello world ...

    spring装配bean实例代码

    博客地址:https://blog.csdn.net/u010476739/article/details/76732201 spring装配bean的方式实例

    spring装配bean的3种方式总结

    主要给大家介绍了关于spring装配bean的3种方式,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    spring自动装配例子

    ean的自动装配,有4种 (1)no:不做任何操作 (2)byName:根据属性 名 自动装配,设值注入 &lt;bean id="xxx" class="xxx" &gt;&lt;/bean&gt; (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 &lt;bean...

    Spring自动装配Bean实现过程详解

    主要介绍了Spring自动装配Bean实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Spring基于@Conditional条件化装配bean

    主要介绍了Spring @Conditional条件化装配bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Spring IOC装配Bean过程解析

    主要介绍了Spring IOC装配Bean过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Spring装配bean方法实例总结

    主要介绍了spring装配bean方法实例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    spring入门教程之bean的继承与自动装配详解

    众所周知Spring里面的bean就类似是定义的一个组件,而这个组件的作用就是实现某个功能的,下面这篇文章主要给大家介绍了关于spring入门教程之bean继承与自动装配的相关资料,需要的朋友可以参考借鉴,下面随着小编来...

    基于框架的Web开发-装配Bean自动装配.doc

    1.4 装配Bean-自动装配(重要!) Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。 自动装配(autowiring):Spring自动满足bean之间的依赖。 1 ...

    Spring装配Bean教程之XML安装配置bean详解

    大家都知道spring有多重配置方式,基于XML,基于注解,基于java类的配置,其中基于XML是最强大的一种,下面这篇文章主要给大家介绍了关于Spring装配Bean之XML安装配置bean的相关资料,需要的朋友可以参考借鉴,下面...

    spring在IoC容器中装配Bean详解

    主要介绍了spring在IoC容器中装配Bean详解,具有一定借鉴价值,需要的朋友可以参考下

    JSP Spring 自动化装配Bean实例详解

    主要介绍了JSP Spring 自动化装配Bean实例详解的相关资料,需要的朋友可以参考下

    spring2.5学习PPT 传智博客

    08_编码剖析Spring装配基本属性的原理 09_Spring如何装配各种集合类型的属性 10_使用构造器装配属性 11_用@Resource注解完成属性装配 12_编码剖析@Resource注解的实现原理 13.@Autowire注解与自动装配 14.让...

    day38 16-Spring的Bean的装配:注解的方式

    NULL 博文链接:https://364232252.iteye.com/blog/2369853

    浅谈Spring装配Bean之组件扫描和自动装配

    本篇文章主要介绍了浅谈Spring装配Bean之组件扫描和自动装配,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring学习之Bean的装配多种方法

    本篇文章主要介绍了Spring学习之Bean的装配三种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    spring boot中的条件装配bean的实现

    主要介绍了spring boot中的条件装配bean的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    在Spring中自动装配Bean的属性

    今天小编就为大家分享一篇关于在Spring中自动装配Bean的属性,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

Global site tag (gtag.js) - Google Analytics