http://smartzjp.javaeye.com/blog/477547
Spring提供了两种装配Bean的容器,一是BeanFactoy,另一个是ApplicationContext
ApplicationContext与BeanFactory都是接口,ApplicationContext是由BeanFactory接口扩展而来,它增强了BeanFactory的功能。
Bean容器能够加载的对象并不一定是严格遵循JavaBeans规范的Java类,任何可实例化的类都可以通过Spring Bean容器加载进来。通常称这些类为POJO。
要记住,BeanFactory不仅仅只具备实例化Bean的功能,它还知道所有的Bean,可以配置和管理它们
------------------------------------------BeanFactory-----------------------------------------------------------
Spring给出一些BeanFactory的实现类,其中最为常用的是XmlBeanFactory。
1、通过文件系统
Resource res = new FileSystemResource("beans.xml"); (相当于MyEclipse中自动生成applicationscontext.xml)
XmlBeanFactory factory = new XmlBeanFactory(res);
2、通过类路径
ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
public class ClassPathResource extends AbstractResource
public abstract class AbstractResource implements Resource
public interface Resource extends InputStreamSource
public interface InputStreamSource
3、通过ApplicationContext加载
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
BeanFactory factory = (BeanFactory) appContext;
创建XmlBeanFactory时需要以Resource作为构造函数的参数。
Resource接口位于org.springframework.core.io 包内,通过XmlBeanFactory创建BeanFactory时都要使用到这个接口。
Resource代表了所有可以访问的底层资源,如文件系统、类路径、URL等等。
------------------------------------------resource----------------------------------------
实现了Resource接口的类包括:
AbstractResource,
ByteArrayResource, --byte[]
ClassPathResource, --String path
DescriptiveResource, --String description不实际指向资源,只包含描述信息
FileSystemResource, --File,or, String path
InputStreamResource, --InputStream
ServletContextResource, --ServletContext, String
UrlResource --URL,or,String path
Resource中定义了以下方法:
Resource createRelative(String relativePath)
boolean exists()
String getDescription()
File getFile()
String getFilename()
URL getURL()
boolean isOpen()
-----------------------------------Bean属性----------------------------
设置成singleton=true 则表示是单例模式
bean的lazy-init属性,如果设置为true,则在第一次加载该bean时初始化;否则在初始化容器时就加载所有bean。
延迟加载仅在bean单例模式下起作用
----------------------------目标对象代理----------------------------
Spring使用两种机制实现AOP技术,一是使用java的动态代理,即java.lang.reflect.Proxy类创建代理。
二是使用CGLIB库自动生成目标对象的子类,同时织入通知。
动态代理要求目标对象必须要实现接口(只有这样才能创建代理),而CGLIB则没有这种限制。
-----------------------------Spring数据源问题--------------------------------------
使用JDBC方法处理DAO,必须要知道数据源DataSource。
Spring DAO层中,获取数据源方法有三种:
通过JNDI
使用第三方的连接池
使用DriverManagerDataSource
为了方便测试,Spring提供了简便易用的DataSource实现,即DriverManagerDataSource,可直接创建:
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName( “……");
dataSource.setUrl( “……");
dataSource.setUsername( “……");
dataSource.setPassword( “……");
也可以通过IOC机制注入:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hibernate"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
最常使用的第三方连接池是Apache的jakarta common dbcp项目。这里主要使用的是org.apache.commons.dbcp.BasicDataSource
由于BasicDataSource 的属性均通过setter方法暴露出来,因此可以使用IOC机制注入。
例如: <bean id="newDs" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/hibernate" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
通过jndi方式
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/DataSource"/>
</bean>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
注意,JdbcTemplate中的dataSource是DataSource类型,但设置的却是JndiObjectFactoryBean
Spring提供了一些简便的查询方法:
1. temp.queryForList("select * from building");
返回List,内部元素为Map,键对象为列名,值对象为列值。
2. temp.queryForMap("select * from building where id=3");
返回Map,键对象为列名,值对象为列值,所以要求必须返回单一行。
3. temp.queryForObject("select building_name from building where id=1", String.class)
返回对象,依据指定的类型决定返回对象的类型,仅针对单行单列
4. 此外还有queryForInt及queryForLong等等