最近在跟老狼做一个项目,框架用的是Spring MVC+Hibernate,大多使用注解方式(如:[Annotation方式实现AOP])进行的配置。 阅读全文>>
在Spring的
在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看看吧。
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获得需要的实体。
环境:
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可以将连接池以及事务管理的细节从应用代码中分离出来。 作为一个开发人员,在开发和测试产品的过程中,你可能需要知道连接数据库的细节。 但在产品实施时,你不需要知道这些细节。通常数据库管理员会帮你设置好数据源。
阅读全文>>