创建一个studentJavaBean
public class Student{
private String id;
private String name;
}
创建一个studentDao{
createStudent()
Student get(id);
}
如果要使用datasource那么需要把下面的两个包引入项目中
1. commons-dbcp.jar
org.apache.commons.BasicDataSource
关注下面的四个属性
分别是driverClassName
url
username
password
把spring提供的这个类配置到applicationContext.xml文件中注入这个四个属性
StudentM--->StudentDaoImpl---->ds
2.BeanFactory (具有生产bean的功能) interface
BeanFactory的实现类XmlBeanFactory
BeanFactory bean=new XmlBeanFactory(new ClassPathResource("com/xxx/xxxx/xx.xml"));
类路径的方式读入一个资源,如果是把ApplicationContext.xml放入/WEB-INF/下面的话
我们需要用的是ResourceAsStream();
<bean id="caozuo" class="shileilei">
<property name="dao">
<ref bean="xx"/>
</property>
</bean>
<bean id="dao" class="daoImpl">
<property name="datasource">
<ref bean="datas"/>
</property>
</bean>
<bean id="datas" class="org.apache.commons.dpcp.BasicDataSource">
<property name="driverClassName">
<value></value>
</property>
<property name="url">
<value></value>
</property>
<property name="username">
<value></value>
</property>
<property name="password">
<value></value>
</property>
</bean>
创建bean的方式
java.util.Calendar cal= java.util.Calendar.getInstance();
<bean id="cal" class="java.util.Calendar"
factory-method="getInstance()"/>
只要是静态方法就行了
double i=Math.sin(1.58);
<bean id="i" class="java.lang.Math"
factory-method ="sin">
构造工厂方法的参数
<constructor-arg>
<value>1.58</value>
</constructor-arg>
public interface Car{}
public interface CarFactory{
Car make(Properties props);
}
public class Audi4 implements Car{}
public class FirstCarFactory implements CarFactory{
}
CarFactory cf=new FirstCarFactory();
Car audi4=cf.make();
<bean id="cf" class="FirstCarFactory"/>
<bean id="audiA4" factory-bean="cf"
factory -method="make">
用工厂模式的设计工厂方法来创建对象
通过
<construtor-arg>
<props>
</props>
</construtor-arg>
通过工厂 实现工厂类型的方法首先我们去创建一个factoryBean然后再去创建我们的产品,那么产品的话就是通过工厂类型的工厂方法就可以做到了
首先在getBean的时候那么需要先去创建Factory然后调用这个工厂的工厂方法把结果返回过来。
Configuration cfg=new Configuration();
Cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
调用中无效的参数主要的原因就是在写数据访问层的时候
在查询数据库的过程中 如果用到了preparedStatement记着不要填写真是的id用问号代替
一种类型创建多种方式我们就可以用继承的方法来做
区别就在于我们的配置文件中怎么去写
<haofeng:bean id="helloHaoAreYou" abstract="true" class="com.cn.haofeng.Hello">
<haofeng:property name="caca" ref="xxx" ></haofeng:property>
</haofeng:bean>
<haofeng:bean id="helloHaoAreYou" parent="helloHaoAreYou">
<haofeng:property name="user" value="world"></haofeng:property>
</haofeng:bean>
<haofeng:bean id="helloHaoAreYou" parent="helloHaoAreYou" >
<haofeng:property name="user" value="zhang" ></haofeng:property>
</haofeng:bean>
上述代码也就是Hello类型两个对象 共同的对象先定义成抽象的 然后再下面继承(parent)
加上你自己的属性 或者是覆盖掉已经注入了的东西
分享到:
相关推荐
spring的aop简单例子spring的aop简单例子spring的aop简单例子spring的aop简单例子
springCloud例子
spring 连接数据库的简单例子工程适合初学者
spring spring 简单学习例子
NULL 博文链接:https://306963591.iteye.com/blog/1129837
spring mvc简单例子
Spring最简单工程例子 适合初学的 最少的jar包
spring aop简单例子,入门学习的好资料
一个spring quartz的定时任务,放到tomcat下就可以看到后台打印了
spring RMI 简单例子
SpringmvcDemo、
spring 结合 Redis 例子,简单入门例子http://blog.csdn.net/sunny243788557/article/details/34431357
spring mvc 简单例子 spring mvc 简单例子 源码
一个简单的spring例子,欢迎下载,使用
简单的Spring入门例子一个最简单的spring示例,使用spring的MVC,并应用了spring的依赖注入,实现简单应用,索性放在这里供还没入门的spring爱好者参考,初步感受一下spring应用(spring高手就不必看了,这里并没有...
Spring boot 简单例子
简单的 springSecurity3例子代码
spring mvc简单的例子和教程spring mvc简单的例子和教程spring mvc简单的例子和教程
Spring MVC 的登陆注册例子,适合初学者看看。整合hibernate3 页面比较简单,导入即可用。
使用Spring MVC创建REST服务简单例子