beans.xml:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/hibernate</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>zk19921027</value>
</property>
</bean>
Userdaoimpl:
@Component("userdao") //这里指定加入userservice中的userdao的名称(当在xml文件中没有写bean的时候,这时才在这里写上component,否则就不要写)
public class UserdaoImpl implements Userdao {
private DataSource dataSource;
public void add(User user){
try {
Connection conn=dataSource.getConnection();
conn.createStatement().executeUpdate("insert into user values('zhangsan',null)");
conn.close();
System.out.println("User saved");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public DataSource getDataSource() {
return dataSource;
}
@Resource//这里对应的就是bean.xml中的那个bean,那个bean里面的class只是这里datasource的一个实现,同时那里将这个类实现的许多属性也注入了值
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
Testservice:
@Test
public void testadd() throws Exception{
//userservice.add(user); 这里不像之前手机项目直接实例化了,这里必须先实例化才能调用userservice的add方法
BeanFactory beanfactory=new ClassPathXmlApplicationContext("beans.xml");
Userservice userservice1=(Userservice)beanfactory.getBean("userservice");
//Userdao userdao=(Userdao)beanfactory.getBean("u"); //用容器来管理
//userservice.setUserdao(userdao);
System.out.println(userservice1.getClass());
User user=new User();
user.setName(123);
user.setPassword(123);
userservice1.add(user);
}
注:在这之前,我们已经实现了将userdao注入到userservice中去(使用annotation具体的做法是在beans.xml文件中加入下面的代码)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
1.这里主要是为了在你写代码的时候Myeclipse能够给你提示,这里配置的技巧是先从
spring-framework-2.5.6-with-docs\spring-framework-2.5.6\dist\resources
这个目录下面将对应的xsd文件打开,然后比如将
xmlns:aop="http://www.springframework.org/schema/aop /
考到xml文件的开始的地方,然后自己加上下面的两个栏目
http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/aop
,然后到preferrence中输入xml catalog ,将location改成xsd所在的文件,key就是
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
,然后就会提示了.
2.下面的context就是为了减少减少xml文件的配置本来userdaoimpl中要实现save方法,所以要先将datasorce的实现给注入到userdaoimpl中去,这时才能调用save方法,而datasource又只是一个接口,所以我们先将datasource的实现注入到datasouce中,这时就实现了UserdaoImpl中的代码,而bean.xml中的datasource的实现中又有那些url的属性,要注入的东西都标注为@component,而注入到的地方写为@Resource
BeanFactory beanfactory=new ClassPathXmlApplicationContext("beans.xml");
Userservice userservice1=(Userservice)beanfactory.getBean("userservice");
这里就不用getbean("userdao")了
<context:annotation-config/> <!-- 这个是用annotation所需要的 -->
<context:component-scan base-package="com.zk"></context:component-scan> <!-- 这是为了去除那些bean设置的,扫描整个包 -->