博客换主题啦!!!

feed订阅 腾讯微博 你好,欢迎光临! 

Tag Archives: Spring

Spring中Bean的作用域

2009.01.27 , , No Comments , 763 浏览

在Spring的标签中有一scope属性,用于指定创建出的实例的作用域。在默认情况下该值为singleton,也就是同一个实体对象在内存中始终都只存在一个。另一个值为prototype,当然,不用说其意与前一个恰恰相反。

在Java中“==”使用于判断两个对象所指的内存地址是否相等的,所以写段简单的代码即可查看两种作用域的效果。

public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean bean1 = (MyBean) factory.getBean("myBean");
MyBean bean2 = (MyBean) factory.getBean("myBean");
if(bean1 == bean2){
System.out.println("sigleton");
}else{
System.out.println("prototype");
}
}

不指定scope运行一下看看,然后将scope改为prototype看看吧。

Spring公共属性注入

2009.01.26 , , No Comments , 989 浏览

假设现在有两个实体类,它们的属性如下图所示。现在只有两个,我们可以很轻易的进行注入配置。可是,倘若项目中的实体类很多呢?那岂不又得在配置上花费大量时间了?有没有什么好办法减少编写配置文件呢?
首先来分析一下这两个类,他们两个都有相同的属性id和name。为了避免重复性的编写这些属性配置,可以这么来做:
在applicationContext.xml文件中使用标签将两个类的相同属性抽取出来配置到abstractBean中,与其他Bean所不同的是不需要配置class属性,而需要设置它的abstract属性为true。

<bean id="abstractBean" abstract="true" >
<property name="id" value="1"/>
<property name="name" value="neeke"/>
</bean>

接下来,配置Bean1。class属性指定为实体类Bean1,指定它的parent为abstractBean。这就意味着Bean1继承了abstractBean,也就是说Bean1拥有了abstractBean的所有属性了。所以,接下来仅仅需要配置email即可。

<bean id="bean1" class="cn.ineeke.spring.Bean1" parent="abstractBean">
<property name="email" value="neeke@ineeke.com"/>
</bean>

同理对Bean2进行配置。完整applicationContext.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-2.5.xsd">
<bean id="abstractBean" abstract="true" >
<property name="id" value="1"/>
<property name="name" value="neeke"/>
</bean>
<bean id="bean1" class="cn.ineeke.spring.Bean1" parent="abstractBean">
<property name="email" value="neeke@ineeke.com"/>
</bean>
<bean id="bean2" class="cn.ineeke.spring.Bean2" parent="abstractBean">
<property name="password" value="123456"/>
</bean>
</beans>

Why Spring

2009.01.26 , , No Comments , 573 浏览

Spring确实给人一种格外清新,爽朗的感觉。仿佛微雨后的绿草从,讨人喜欢,又蕴藏着勃勃生机。他大大简化了Java企业级开发,提供了强大、稳定的功能,又没有带来额外的负担。让人们使用Spring做每一件事情的时候都有得体而优雅的感觉。Spring有两个主要目标:一是让现有技术更易于使用,二是促进良好的编程习惯。
Spring是一个全面的解决方案。但他坚持一个原则:不重新造轮子。已经有较好解决方案的领域,Spring绝不做重复性的实现,比如对象持久化和OR映射,Spring只是对现有JDBC、Hibernate、JPA等技术提供支持,使之更易用,而不是重新做一个实现。
Spring依然在不断发展和完善,但基本与核心的部分已经相当稳定,包括Spring的依赖注入容器、AOP实现和对持久化层的支持。

上图描述了SpringFramework包含的内容。其中最基础的是Spring Core,即Spring作为依赖注入容器的部分。Spring AOP是基于Spring Core的,典型的一个应用即声明式事务。Spring DAO对JDBC提供了支持,简化了JDBC编码,同时使代码更健壮。Spring ORM部分对Hibernate等OR映射框架提供了支持。Spring可以在Java SE中使用,也可以在Java EE中使用,Spring Context为企业级并发提供了便利和集成的工具。Spring Web是为Spring在Web应用程序中使用提供的支持。Struts作为MVC实现是现在实际上的标准,但编码略显笨拙,Spring提供了一个稍微简单的Spring Web MVC框架,但应用不多。
到这里,大家应该对Spring有了一个大致的印象了,但只有了解其依赖注入实现的时候,才可以说开始了解Spring了,只有对其AOP实现有一定认识了的时候,才能说自己“学会”Spring了。

Spring依赖注入

2009.01.25 , , 3 Comments , 1,427 浏览

O(∩_∩)O哈哈~过新年了,我的Hibernate学习也至此告一段落了,Hibernate是冬眠,也刚好过完冬天了,过春节了,我该学习Spring了。Spring I'm coming...

首先要有一个实体。

package cn.ineeke.spring;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MyBean {
private String strValue;
private int intValue;
private List listValue;
private Set setValue;
private String[] arrayValue;
private Map mapValue;
public String getStrValue() {
return strValue;
}
public void setStrValue(String strValue) {
this.strValue = strValue;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public List getListValue() {
return listValue;
}
public void setListValue(List listValue) {
this.listValue = listValue;
}
public Set getSetValue() {
return setValue;
}
public void setSetValue(Set setValue) {
this.setValue = setValue;
}
public String[] getArrayValue() {
return arrayValue;
}
public void setArrayValue(String[] arrayValue) {
this.arrayValue = arrayValue;
}
public Map getMapValue() {
return mapValue;
}
public void setMapValue(Map mapValue) {
this.mapValue = mapValue;
}
}

有了实体就可以开始配置Spring了。在crc下创建applicationContext.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-2.5.xsd">
<bean id="myBean" class="cn.ineeke.spring.MyBean">
<property name="strValue" value="Hello" />
<property name="intValue">
<value>21</value>
</property>
<property name="listValue">
<list>
<value>neeke1</value>
<value>neeke2</value>
</list>
</property>
<property name="setValue">
<set>
<value>ineeke1</value>
<value>ineeke2</value>
</set>
</property>
<property name="arrayValue">
<list>
<value>array1</value>
<value>array2</value>
</list>
</property>
<property name="mapValue">
<map>
<entry key="ek1" value="ek1"/>
<entry key="ek2" value="ek2"/>
</map>
</property>
</bean>
</beans>

现在只仅仅是配置好了,下面看看如何new出这个实体。

package cn.ineeke.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
         MyBean bean = (MyBean)factory.getBean("myBean");
         System.out.println(bean.getIntValue());
}
}

这个主要是为了了解如何使用Spring对实体的属性进行注入,其重点又在于不同数据类型的属性如何注入以及如何使用Spring获得需要的实体。

Spring数据源的灵活配置巧应用

2008.05.26 , , No Comments , 629 浏览

环境:
Java SE 1.5
Spring-2.5.1
mysql-connector-java-5.1.5.zip
Mysql 5.x
为了从数据库中取得数据,我们首先需要获取一个数据库连接。 Spring通过DataSource对象来完成这个工作。 DataSource是JDBC规范的一部分, 它被视为一个通用的数据库连接工厂。通过使用DataSource, Container或Framework可以将连接池以及事务管理的细节从应用代码中分离出来。 作为一个开发人员,在开发和测试产品的过程中,你可能需要知道连接数据库的细节。 但在产品实施时,你不需要知道这些细节。通常数据库管理员会帮你设置好数据源。