在src下新建文件夹lib,并引入jar文件
dao层
HelloDao.java
package www.csdn.spring.dao;
public interface HelloDao {
public void sayHello();
}
HelloDaoImpl.java
package www.csdn.spring.dao;
public class HelloDaoImpl implements HelloDao {
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("hello");
}
}
service层
HelloService.java
package www.csdn.spring.service;
public interface HelloService {
public void sayHello();
}
HelloServiceImpl.java
package www.csdn.spring.service;
import www.csdn.spring.dao.HelloDao;
public class HelloServiceImpl implements HelloService {
HelloDao helloDao;
public void setHelloDao(HelloDao helloDao) {
System.out
.println("控制反转:应用程序本身不在负责创建helloDao对象,而是由spring容器负责创建、管理、维护,这样控制权转移,称为反转。"
+ "可以通过依赖注入方式 注入该HelloDao对象");
this.helloDao = helloDao;
}
@Override
public void sayHello() {
helloDao.sayHello();
}
}
spring的配置文件
该配置模版可以从spring的参考手册或spring的例子中得到。
文件可以存放在任何目录下,但考虑到通用性,一般放在类路径下。
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<import resource="spring_dao.xml" />
<import resource="spring_service.xml" />
</beans>
spring_dao.xml
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><beansxsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"><!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 bean对象 --><bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl"><property name="helloDao" ref="helloDaoImpl"></property></bean></beans>
<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.xsd">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 -->
<bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl"
scope="prototype">
</bean>
</beans>
spring_service.xml
name:HelloServiceImpl里的配置
ref:spring_dao里的
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><beansxsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"><!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 bean对象 --><bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl"><property name="helloDao" ref="helloDaoImpl"></property></bean></beans>
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 bean对象 -->
<bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl">
<property name="helloDao" ref="helloDaoImpl"></property>
</bean>
</beans>
注意: 编写spring配置文件时,不能出现帮助信息
由于spring的schema文件位于网络上,如果机器不能连接到网络,那么在编写配置信息时候就无法出现提示信息,解决方法有两种:
1。让机器上网,eclipse会自动从网络上下载schema文件并缓存在硬盘上。
2。手动添加schema文件,方法如下:
windwos->preferences->myeclipse->files and editors->xml->xmlcatalog
点"add",在出现的窗口中的Key Type中选择URI,在location中选"File system",然后在spring解压目录的dist/resources目录中选择spring-beans-2.5.xsd,回到设置窗口的时候不要急着关闭窗口,应把窗口中的Key Type改为Schema location,Key改为http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Test类测试
HelloTest.java
package www.csdn.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import www.csdn.spring.dao.HelloDao;
import www.csdn.spring.dao.HelloDaoImpl;
import www.csdn.spring.service.HelloService;
public class HelloTest {
@Test
public void test() {
// 容器创建 实例化容器
//
// 读取 classes 路径下面的文件 参数 动态参数、单个参数、数组 等
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring.xml");
// 方法1
// HelloDao helloDao = (HelloDao) context.getBean("helloDaoImpl");
// helloDao.sayHello();
// 方法2,面向接口,所以一般用dao
// HelloDao helloDao =context.getBean("helloDaoImpl", HelloDao.class);
// helloDao.sayHello();
/*
* HelloDaoImpl helloDaoImpl =context.getBean("helloDaoImpl",
* HelloDaoImpl.class); helloDao.sayHello();
*/
// 方法3,常用
HelloService helloService = context.getBean("helloServiceImpl",
HelloService.class);
helloService.sayHello();
}
}
运行结果:
<?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.xsd">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 -->
<bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl"
scope="prototype" lazy-init="false">
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?><beansxsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"><!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 bean对象 --><bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl"><property name="helloDao" ref="helloDaoImpl"></property></bean></beans>