欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring入门(2)---第一个spring案例

程序员文章站 2024-03-18 16:30:46
...

直接上图:

spring入门(2)---第一个spring案例

源码:

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{
	public HelloDaoImpl() {
		System.out.println("HelloDaoImpl实例化.......");
	}
	@Override
	public void sayHello() {
		System.out.println("say:hello");
	}

}


DemoTest.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 DemoTest {
	@Test
	public void test(){
		HelloDao helloDao = new HelloDaoImpl();
		helloDao.sayHello();
		/*// 容器创建 实例化容器
		
				// 读取 classes 路径下面的文件  参数 动态参数、单个参数、数组 等
				ApplicationContext context = new ClassPathXmlApplicationContext(
						"spring.xml");
				
			//	HelloDao helloDao = (HelloDao) context.getBean("helloDaoImpl");
			//HelloDao helloDao =context.getBean("helloDaoImpl", HelloDao.class);
				
				HelloDaoImpl helloDaoImpl =context.getBean("helloDaoImpl", HelloDaoImpl.class);
				   
				
				//helloDao.sayHello();
				
				//helloSerivceImpl
				HelloService helloService = context.getBean("helloServiceImpl", HelloService.class);
			
			    helloService.sayHello();*/
		
	}
}	


spring-dao.xml

<?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">


   <!-- spring容器 就是负责创建、管理、维护Bean 并且能够依赖注入到相应组件上 -->
   <bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl" scope="singleton" lazy-init="default"></bean>
</beans>


spring-service.xml

<?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">

   <bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl" scope="singleton" lazy-init="false">
      <property name="helloDao" ref="helloDaoImpl"/>
   </bean>
</beans>


spring.xml

<?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">
      

   <import resource="spring-dao.xml"/>
   <import resource="spring-service.xml"/>
   
</beans>


执行测试类之后控制台输出

HelloDaoImpl实例化.......
say:hello