`
haofenglemon
  • 浏览: 240643 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring简单的下例子

阅读更多
创建一个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)

加上你自己的属性  或者是覆盖掉已经注入了的东西






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics